Version 2 by fc
on Feb 13, 2008 11:19.

compared with
Current by fc
on Feb 13, 2008 11:19.

(show comment)
Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (45)

View Page History
h3. Real-life Example
<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>
* I <li>I needed a way to test Zend_Import with different file formats and using different protocols.</li>
* I wanted to minimize coupling between the components.
<li>I wanted to minimize coupling between the components.</li>
* I <li>I wanted other developers to know the components Zend_Import was using by looking at the config file or API. API.</li>
* I <li>I wanted to minimize the risk of having hidden dependencies. dependencies.</li>
</ul>

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.
<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>

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>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>

*Zend_Import package:*
<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>
{code}
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
Zend/
Import/
Ftp.php
Http.php
{code}
]]></ac:plain-text-body></ac:macro>

*Specification file*
<p><strong>Specification file</strong></p>

{code:php}
<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
$components = array(
'Zend_Import_Csv' => array(
);
}
{code}
]]></ac:plain-text-body></ac:macro>

*Prototype of the Zend_Import_Format_Csv class:*
<p><strong>Prototype of the Zend_Import_Format_Csv class:</strong></p>

{code:php}
<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
{
}
}
{code}
]]></ac:plain-text-body></ac:macro>

*Usage:* <p><strong>Usage:</strong></p>

{code:php}
<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
$dbConnection = array(
'host' => 'localhost',
echo '[Server: ' . $_SERVER['SERVER_ADDR'] . '] ' . $e->getMessage() . $trace;
}
{code}
]]></ac:plain-text-body></ac:macro>

This will output:
<p>This will output:</p>

{code}
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
Zend_Import_Dependency Object
(

)
{code}
]]></ac:plain-text-body></ac:macro>