Zend Framework dependency manager

Open Source PHP Zend Framework November 2nd, 2010 by Eran Galperin

Reusing Zend Framework based code can be a bit of a pain - you do not want to include the entire framework just for a few classes, yet tracking down the dependencies needed to run your code can be time consuming and it's hard to tell if you aren't missing anything.

To overcome those issues, I wrote a utility class to automatically resolve Zend Framework dependencies by fetching them from the online SVN repository when they are needed (and then storing them locally). The class is available for download from Binpress under the MIT license.

How does it work:

  • An autoload function is prepended to the spl autoload stack
  • The auotload function catches requests for Zend Framework classes
  • If the class is not available locally, the dependency manager downloads it from the SVN repository
  • If the class has additional dependencies handled by internal class managers in the ZF (such as plugins and helpers), those are fetched as well
  • All fetched class files are stored locally in a predefined location (such as /library/Zend as is the norm)
  • The class is then loaded

Usage:

  • Include the Zdm class (Zdm.php) at the beginning of the application (in the bootstrap, if you are using one)
  • Run the static start() function
  • The start() function accepts several optional parameters
    • 'libraryPath' - The local path to store fetched ZF classes. The default is the same directory as the Zdm class file (expecting it to be under /library)
    • 'zfVersion' - The Zend Framework version to fetch. Default is 1.10.8
    • 'prependStack' - By default the autoload function is prepended to the stack. If this interferes with any existing autoload functions, you can set this value to false and it will be appended to the stack instead.
    • 'repository' - Change the location of the repository to fetch ZF classes from. You can set this value to a local installation of the framework for better performance.
  • The first time the application is run with the dependency manager, it might take some  time to download all the dependencies. To avoid the script timing out, you should set the time limit to '0' using set_time_limit()

Notes - you need to remove 'require' and 'require_once' statements that load Zend Framework classes. Those statements produce a fatal error when the file is not found and cannot be trapped by the autoload function. I tried to recover from the warning sent before the fatal error, however it could not prevent the fatal error from happening.

This class can be used to minimize the needed framework build for current projects as well. Rename your Zend Framework library directory, and allow the dependency manager to build the library from classes that are actually used in the application. This usually results in a much smaller framework footprint. I've successfully used this class in several active projects to recreate the needed framework library that runs the entire application.

It is likely there are some dependencies that are not mapped yet in the class. If you encounter such dependencies, please let me so I could add it to the class.

Enter your email address to receive notification about new posts.

If you liked this article you should follow me on Twitter and/or share below:
  • Pingback: This Week’s Link List (November 5, 2010) : Brian Swan's Blog : The Official Microsoft IIS Site

  • Israel

    Great work!! I was wondering how this might be done and just found your utility. I was using Zend Framework as a library for my own MVC model and everything works fine.

    Just a few days ago I started using ZF MVC model and tried to use your code with a completely new project created with Zend_Tool and just found that your code can’t download everythings that’s needed. For example, it can’t handle the Zend_Application_Bootstrap_Bootstrap dependency that the default Bootstrap class has.

    Seems like there’s too much of “class exists” checking on all ZF code, so there are lots of exceptions thrown before your library starts working, specially on MVC’s classes.

    As your code comments all “require_once” lines, the setBootstrap method of Zend_Aplication class becomes useless, as you may see in lines 319-324 from v1.10.8 or v1.11.0 which I tested with.

    Now I can’t stop at finding the problematic classes, but as soon as I have some time I may help you find them, since I’m very interested in using your code for my future projects.

  • http://www.binpress.com Eran Galperin

    @israel
    You’re right, it does have problems with the new bootstrap model (I’m referring in the post to the old bootstrap file that was used in the ZF until recently). I will go over it when I have some time and try to make it work with the new model.