<h3>Real-life Example</h3>
<p>Zend_Import is a component I use to import SQL, XML and CSV files into the database. I created the Zend_Di component because:</p>
<ul>
<li>I needed a way to test Zend_Import with different file formats and using different protocols.</li>
<li>I wanted to minimize coupling between the components.</li>
<li>I wanted other developers to know the components Zend_Import was using by looking at the config file or API.</li>
<li>I wanted to minimize the risk of having hidden dependencies.</li>
</ul>
<p>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. </p>
<p>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. </p>
<p>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.</p>
<p><strong>Zend_Import package:</strong></p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
Zend/
Import/
Assembler/
Spec.php
Format/
Abstract.php
Csv.php
Sql.php
Xml.php
Protocol/
Abstract.php
Ftp.php
Http.php
]]></ac:plain-text-body></ac:macro>
<p><strong>Specification file</strong></p>
<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
$components = array(
'Zend_Import_Csv' => array(
'class' => 'Zend_Import_Format_Csv',
'instanceof' => 'Zend_Import_Format_Abstract',
'arguments' => array(
'__construct' => 'Zend_Db',
'setProtocol' => 'Zend_Import_Protocol_Ftp',
'setLog' => 'Zend_Log',
),
),
'Zend_Import_Xml' => array(
'class' => 'Zend_Import_Format_Xml',
'instanceof' => 'Zend_Import_Format_Abstract',
'arguments' => array(
'__construct' => 'Zend_Db',
'setProtocol' => 'Zend_Import_Protocol_Http',
'setLog' => 'Zend_Log',
),
),
'Zend_Import_Protocol_Http' => array(
'class' => 'Zend_Import_Protocol_Http',
'instanceof' => 'Zend_Import_Protocol_Abstract',
),
'Zend_Import_Protocol_Ftp' => array(
'class' => 'Zend_Import_Protocol_Ftp',
'instanceof' => 'Zend_Import_Protocol_Abstract',
),
'Zend_Log' => array(
'class' => 'Zend_Log',
'arguments' => array(
'__construct' => 'Zend_Log_Writer',
),
),
'Zend_Log_Writer' => array(
'class' => 'Zend_Log_Writer_Stream',
'instanceof' => 'Zend_Log_Writer_Abstract',
'arguments' => array(
'__construct' => ':pathToLogFile',
),
),
'Zend_Db' => array(
'class' => 'Zend_Db_Adapter_Pdo_Mysql',
'instanceof' => 'Zend_Db_Adapter_Pdo_Abstract',
'arguments' => array(
'__construct' => ':dbConnection',
),
),
);
}
]]></ac:plain-text-body></ac:macro>
<p><strong>Prototype of the Zend_Import_Format_Csv class:</strong></p>
<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
class Zend_Import_Format_Csv extends Zend_Import_Format_Abstract
{
protected $_db = null;
protected $_protocol = null;
protected $_log = null;
public function __construct(Zend_Db_Adapter_Pdo_Abstract $db)
public function setProtocol(Zend_Import_Protocol_Abstract $protocol)
public function setLog(Zend_Log $log)
}
]]></ac:plain-text-body></ac:macro>
<p><strong>Usage:</strong></p>
<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
$dbConnection = array(
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'dbname'
);
$pathToLogFile = '/var/log/import/csv-error.log';
try
catch (Zend_Di_Exception $e) {
$trace = str_replace('#', '<br />', $e->getTraceAsString());
echo '[Server: ' . $_SERVER['SERVER_ADDR'] . '] ' . $e->getMessage() . $trace;
}
]]></ac:plain-text-body></ac:macro>
<p>This will output:</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
Zend_Import_Dependency Object
(
[\_config:protected] => Array
(
[Zend_Import_Csv] => Array
(
[class] => Zend_Import_Format_Csv
[instanceof] => Zend_Import_Format_Abstract
[arguments] => Array
(
[__construct] => Zend_Db
[setProtocol] => Zend_Import_Protocol_Ftp
[setLog] => Zend_Log
)
)
[Zend_Import_Xml] => Array
(
[class] => Zend_Import_Format_Xml
[instanceof] => Zend_Import_Format_Abstract
[arguments] => Array
(
[__construct] => Zend_Db
[setProtocol] => Zend_Import_Protocol_Http
[setLog] => Zend_Log
)
)
[Zend_Import_Protocol_Http] => Array
(
[class] => Zend_Import_Protocol_Http
[instanceof] => Zend_Import_Protocol_Abstract
)
[Zend_Import_Protocol_Ftp] => Array
(
[class] => Zend_Import_Protocol_Ftp
[instanceof] => Zend_Import_Protocol_Abstract
)
[Zend_Log] => Array
(
[class] => Zend_Log
[arguments] => Array
(
[__construct] => Zend_Log_Writer
)
)
[Zend_Log_Writer] => Array
(
[class] => Zend_Log_Writer_Stream
[instanceof] => Zend_Log_Writer_Abstract
[arguments] => Array
(
[__construct] => :pathToLogFile
)
)
[Zend_Db] => Array
(
[class] => Zend_Db_Adapter_Pdo_Mysql
[instanceof] => Zend_Db_Adapter_Pdo_Abstract
[arguments] => Array
(
[__construct] => :dbConnection
)
)
)
[\_manager:protected] => Zend_Di_Container_Manager Object
(
[\_components:protected] => Array
(
[0] => Zend_Import_Csv
[1] => Zend_Import_Xml
[2] => Zend_Import_Protocol_Http
[3] => Zend_Import_Protocol_Ftp
[4] => Zend_Log
[5] => Zend_Log_Writer
[6] => Zend_Db
)
[\_containerObject:protected] =>
[\_instances:protected] => Array
(
[Zend_Db] => Zend_Db_Adapter_Pdo_Mysql Object
(
[\_pdoType:protected] => mysql
[\_numericDataTypes:protected] => Array
(
[0] => 0
[1] => 1
[2] => 2
[INT] => 0
[INTEGER] => 0
[MEDIUMINT] => 0
[SMALLINT] => 0
[TINYINT] => 0
[BIGINT] => 1
[SERIAL] => 1
[DEC] => 2
[DECIMAL] => 2
[DOUBLE] => 2
[DOUBLE PRECISION] => 2
[FIXED] => 2
[FLOAT] => 2
)
[\_config:protected] => Array
(
[host] => localhost
[username] => username
[password] => password
[dbname] => dbname
[options] => Array
(
[caseFolding] => 0
[autoQuoteIdentifiers] => 1
)
[driver_options] => Array
(
)
)
[\_fetchMode:protected] => 2
[\_profiler:protected] => Zend_Db_Profiler Object
(
[\_queryProfiles:protected] => Array
(
)
[\_enabled:protected] =>
[\_filterElapsedSecs:protected] =>
[\_filterTypes:protected] =>
)
[\_connection:protected] =>
[\_caseFolding:protected] => 0
[\_autoQuoteIdentifiers:protected] => 1
)
[Zend_Import_Protocol_Ftp] => Zend_Import_Protocol_Ftp Object
(
)
[Zend_Log_Writer] => Zend_Log_Writer_Stream Object
(
[\_stream:protected] => Resource id #44
[\_filters:protected] => Array
(
)
[\_formatter:protected] => Zend_Log_Formatter_Simple Object
(
[\_format:protected] => %timestamp% %priorityName% (%priority%): %message%
)
)
[Zend_Log] => Zend_Log Object
(
[\_priorities:private] => Array
(
[0] => EMERG
[1] => ALERT
[2] => CRIT
[3] => ERR
[4] => WARN
[5] => NOTICE
[6] => INFO
[7] => DEBUG
)
[\_writers:private] => Array
(
[0] => Zend_Log_Writer_Stream Object
(
[\_stream:protected] => Resource id #44
[\_filters:protected] => Array
(
)
[\_formatter:protected] => Zend_Log_Formatter_Simple Object
(
[\_format:protected] => %timestamp% %priorityName% (%priority%): %message%
)
)
)
[\_filters:private] => Array
(
)
[\_extras:private] => Array
(
)
)
[Zend_Import_Csv] => Zend_Import_Format_Csv Object
(
[\_db:protected] => Zend_Db_Adapter_Pdo_Mysql Object
(
[\_pdoType:protected] => mysql
[\_numericDataTypes:protected] => Array
(
[0] => 0
[1] => 1
[2] => 2
[INT] => 0
[INTEGER] => 0
[MEDIUMINT] => 0
[SMALLINT] => 0
[TINYINT] => 0
[BIGINT] => 1
[SERIAL] => 1
[DEC] => 2
[DECIMAL] => 2
[DOUBLE] => 2
[DOUBLE PRECISION] => 2
[FIXED] => 2
[FLOAT] => 2
)
[\_config:protected] => Array
(
[host] => localhost
[username] => username
[password] => password
[dbname] => dbname
[options] => Array
(
[caseFolding] => 0
[autoQuoteIdentifiers] => 1
)
[driver_options] => Array
(
)
)
[\_fetchMode:protected] => 2
[\_profiler:protected] => Zend_Db_Profiler Object
(
[\_queryProfiles:protected] => Array
(
)
[\_enabled:protected] =>
[\_filterElapsedSecs:protected] =>
[\_filterTypes:protected] =>
)
[\_connection:protected] =>
[\_caseFolding:protected] => 0
[\_autoQuoteIdentifiers:protected] => 1
)
[\_protocol:protected] => Zend_Import_Protocol_Ftp Object
(
)
[\_log:protected] =>
)
)
[\_containers:protected] => Array
(
)
[\_activeContainer:protected] =>
)
[\_factory:protected] => Zend_Di_Component_Factory Object
(
[\_includePath:protected] => D:/Workspace/Csv
[\_classesDefined:protected] => Array
(
[Zend_Import_Csv] => Zend_Import_Format_Csv
)
[\_constructorArgs:protected] => Array
(
)
[\_setterArgs:protected] => Array
(
)
)
[\_arguments:protected] => Array
(
)
[\_component:protected] => Zend_Import_Csv
[\_method:protected] => __construct
[\_bindParam:protected] => Array
(
[:pathToLogFile] => Array
(
[dataType] => string
[data] => D:/Workspace/Csv/error.log
)
[:dbConnection] => Array
(
[dataType] => array
[data] => Array
(
[host] => localhost
[username] => username
[password] => password
[dbname] => dbname
)
)
)
)
]]></ac:plain-text-body></ac:macro>