9

I have a folder of PHP scripts, they are mostly utility scripts. How to share those scripts among different PHP applications so that reuse and deployment are easy?

I would have to package my app into an installer, and let the user install it.

I could put the lib and hardcode the include path, but that means I haven to change the PHP code every time i deploy the web application to a new customer. This is not desirable.

Another route I consider is to copy the lib to other apps, but still, since the lib is constantly updating, that means that I need to constantly do the copying, and this will introduce a lot of problems. I want an automated way to do this.

Edit: Some of the applications are Symfony, some are not.

7 Answers 7

5
+50

You could create a PEAR package.

See Easy PEAR Package Creation for more information on how to do this.

This assumes that when you say anyone, you mean outside your immediate organisation.

Updated: You do not need to upload to a website to install the PEAR package. Just extract your archive into the pear folder to use in a PHP application.

Added: Why not create a new SVN repository for your library? Lets say you create a library called FOO. Inside the repostory you could use the folder heirachy of trunk\lib\foo. Your modules could then go into trunk\lib\foo\modules and have a file called trunk\lib\foo\libfoo.php. Now libfoo.php can include once or require once all the modules as required.

1
  • Best method for sure. Following Pear standards in coding and package creation will also allow it to be portable to frameworks like Zend and EuropaPHP out of the box while making it accessible and understandable to anyone who knows about Pear. Also, in following those standards, your code is more modular because it's easier to follow the functional-cohesion/low-coupling best-practices.
    – Tres
    Jul 10, 2009 at 5:57
4

PHP now supports Phar archives. There's full documentation on php.net. There's a complete tutorial on IBM website as well.

One neat thing you can do with Phar archives is package an entire application and distribute it that way.

http://php.net/phar

http://www.ibm.com/developerworks/opensource/library/os-php-5.3new4/index.html

1

Ahh, libraries...

There are two conflicting purposes here:

  1. Sanity when updating scripts (ie. not breaking 10 other apps).
  2. Keeping things in one organized logical place for developer efficiency.

I suggest you take a close look at git and git submodules

We use git submodules extensively for this very purpose. It allows the best of both worlds because shared scripts can be upgraded at will in any project, and then that change can be moved to the other projects (deliberately) when you have time to do so and test correctly.

Of course, you need to be using git to take advantage of submodules, but if you are not using git, and you start, you'll eventually wonder how you ever lived without it.


Edit: Since the original poster is using svn, consider using SVN Externals.

0
1

UPDATED:

you just have to put the lib in some place reachable by your apps (in a place where you can reach it via http or ftp or https or something else) and include it.

If you have to update it often you can package your library in a single phar file and you can then provide your client a function to pull the library from some remote path and update a parameter in their local configuration accordingly, like:

 function   updateLocalLibary(){   
     //read the remote library in a variable  
     $file= file_get_content($remoteLibraryRepository.$libraryPharFile);   
     //give it a unique name  
     $newLibraryName=$libraryPharFile."_".date('Ymdhsi');  
     //store the library it on a local file  
     file_put_content($localLibraryPath.$newLibraryName,$file);  
     //update the configuration, letting your app point to the new library  
     updateLatestLibraryPathInConfig($newLibraryName);  
     //possibly delete the old lib  
 }  

In your include path then you don't have necesasrily to hardcode a path, you can include a parameter based on your config, like:

 include( getLatestLibraryPathFromConfig() )  

(you are responsible to secure the retrieval in order to let only your clients see the library)

Your conf can be in a db, so that when you call updateLibraryPathInConfig() you can perform an atomical operation and you are sure not to have client read dirty data.

The clients can then update their library as needed. They may even schedule regular updates.

4
  • I am aware of both options. But if I were to put the lib and hardcode the include path, that means I haven to change the PHP code every time i deploy the web application to a new customer. Of course I could copy the lib to other apps, but still, the lib is constantly updating, does that mean that I have to recopy to every app every time?
    – Graviton
    Jul 9, 2009 at 1:10
  • I'm afraid I don't understand completely your problem: -you don't want to copy your updates to your client site, instead you want them to pull the updated library in their application from time to time,is that right? -do your different clients have access to some common network address? can your library be shared publicly over the internet, or on a password protected ftp ? Note: -in your include you don't have necesasrily to hardcode a path, you can include a parameter based on your config, like: include( getLatestLibraryPathFromConfig() )
    – mic.sca
    Jul 9, 2009 at 6:47
  • You can as well package your library in a phar and provide your client a function to pull the library from some remote path and update a parameter, like: function updateLocalLibary(){ $file= file_get_content($remoteLibraryRepository); $newLibraryName=$libraryName.date('Ymdhsi'); file_put_content($localLibraryPath.$newLibraryName); updateLibraryPathInConfig($newLibraryName); //possibly delete the old lib }
    – mic.sca
    Jul 9, 2009 at 7:05
  • @Ngu Soon Hui: I've uptaded my answer as you see ..but I can't see how can you think of something more automated.With that you can have the php application check your repository for updates every say "1hour" and automatically update the local working copy.
    – mic.sca
    Jul 10, 2009 at 7:09
0

There are a lot of options:

  • tar + ftp/scp
  • PEAR (see above @Wayne)
  • SVN
  • rsync
  • NFS

I recommend to use a continuous integration software (Atlassian Bamboo, CruiseControl); check out your repository, build a package, and then use rsync. Automatically.

0

You should also look into using namespace in order to avoid conflicts with other libraries you might use. pear is probably a good idea for the delivery method, however, you can just place it in the standard path /usr/share/php/, or any other place that is set as the include path in your php settings file.

0

Good question, and probably one that doesn't have a definite answer. You can basically pick between two different strategies for distributing your code: Either you put commonly used code in one place and let individual applications load from the same shared place, or you use a source-control-system to synchronise between local copies. They aren't mutually exclusive, so you'll often see both patterns in use at the same time.

Using the file system to share code

You can layer the include_path to create varying scopes of inclusion. The most obvious application of this pattern is a globally maintained PEAR repository and a local application. If your it-system consists of multiple applications that share a common set of libraries, you can add a layer in between these (a framework layer). If you structure the include_path such that the local paths come before the global paths, you can use this to make local overrides of files. This is a rather crude way to extend code, since it works per-file, but it can be useful in some cases.

Use source-control

Another strategy is to make a lot of local checkouts of a single shared repository. Some benefits over the layered-include-pattern is that you can make more fine grained local changes. It can be a bit of a challenge to manage the separation between application layers (infrastructure, framework, application). svn:externals can work, but has some limitations. It's also slightly more complicated to propagate global changes to all applications. An automated deployment process can help with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.