Real-life Example
Zend_Import is a component I use to import SQL, XML and CSV files into the database. I created the Zend_Di component because:
- I needed a way to test Zend_Import with different file formats and using different protocols.
- I wanted to minimize coupling between the components.
- I wanted other developers to know the components Zend_Import was using by looking at the config file or API.
- I wanted to minimize the risk of having hidden dependencies.
The CSV files are retrieved using FTP, and the XML files using HTTP. Because the script is run by a Cron job, I also added logging capabilities to Zend_Import.
The problem I faced when designing Zend_Import was the amount of dependencies the component had: Zend_Import_Protocol, Zend_Db, Zend_Log, Zend_Log_Writer and Zend_Mail (recently added, and not included in this example). So I decided to use the dependency injection pattern to solve this problem.
Zend_Di has been tested with Zend_Import in a staging server. Below is a prototype of the Zend_Import package, and a brief example of how Zend_Import takes full advantage of the DI pattern.
Zend_Import package:
Specification file
Prototype of the Zend_Import_Format_Csv class:
Usage:
This will output: