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
- 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.
To know when the next article is published, please subscribe to new articles using your Email below or follow me on Twitter.