<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Not My Idea &#187; php-en</title>
	<atom:link href="http://www.notmyidea.org/article/category/php/php-en/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.notmyidea.org</link>
	<description>Carnets Web d&#039;Alexis Métaireau</description>
	<lastBuildDate>Mon, 16 Aug 2010 19:09:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dependency Injection &#8211; Using SpiralDi Container</title>
		<link>http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/</link>
		<comments>http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 09:10:26 +0000</pubDate>
		<dc:creator>Alexis Metaireau</dc:creator>
				<category><![CDATA[Architecture logicielle]]></category>
		<category><![CDATA[php-en]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spiral]]></category>

		<guid isPermaLink="false">http://www.notmyidea.org/?p=226</guid>
		<description><![CDATA[Please note that this article is also available in French. A big thanks to Frédéric Sureau for his translation work on the English version.
Since my previous article (FR), I have kept on working on the dependencies injector of Spiral, wich I just published in a standalone version.
Here is an overview of SpiralDi functionnalities, and some [...]]]></description>
			<content:encoded><![CDATA[<p class="info">Please note that this article is also <a href="http://www.notmyidea.org/article/fr-introduction-to-spiral-dependency-injection-container-in-php">available in French</a>. A big thanks to Frédéric Sureau for his translation work on the English version.</p>
<p>Since my <a href="http://www.notmyidea.org/article/dependency-injection-singleton-design-pattern-ioc-dendencies-dependences/">previous article (FR)</a>, I have kept on working on the dependencies injector of <a href="http://bitbucket.org/ametaireau/spiral/wiki/Home">Spiral</a>, wich I just published in a <a href="http://bitbucket.org/ametaireau/spiraldi/"><em>standalone</em> version</a>.</p>
<p>Here is an overview of SpiralDi functionnalities, and some examples on the use.<br />
<span id="more-226"></span></p>
<h2>Table des matières</h2>
<div class="toc">
<ol>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-overview">Overview</a></li>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-principles">Principles</a>
<ol>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-services-scope">Services scope</a></li>
</ol>
</li>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-advanced-use">Advanced use</a>
<ol>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-using-references-and-methodfactory">Using references and <em>MethodFactory</em></a></li>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-inheriting-and-overloading-services">Inheriting and overloading services</a></li>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-container-aware">Container Aware</a></li>
</ol>
</li>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-factories">Factories</a></li>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-download-it">Download it ! </a>
<ol>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-via-the-mercurial-repository">Via the mercurial repository</a></li>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-via-the-archives">Via the archives</a></li>
</ol>
</li>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-interesting-readings">Interesting readings</a></li>
<li><a href="http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/#toc-going-further">Going further ?</a></li>
</ol>
</div>
<h2 id="toc-overview">Overview</h2>
<p>SpiralDi comes with a lot of new features, some of them inspired by the excellent Java framework : Spring. It is now possible to :</p>
<ul>
<li>Use <em>factories</em> to set the parameters values (useful for configuration)</li>
<li>Use an inheritance system to make configuration file more clear</li>
<li>Use <em>factories</em> for the construction of the services</li>
<li>Directly inject the container in <em>ContainerAware</em> services</li>
</ul>
<p>But, maybe, it doesn&#8217;t mean anything for you ? I will explain the details further later.</p>
<h2 id="toc-principles">Principles</h2>
<p>If you don&#8217;t know much about the inversion of control, I advise you to read my <a href="http://www.notmyidea.org/article/dependency-injection-singleton-design-pattern-ioc-dendencies-dependences/">introduction on the dependencies injection (FR)</a>.</p>
<p>The use of a lightweight container is done in two steps :</p>
<ul>
<li>The description of the <em>schema</em> of all the services that will be used later</li>
<li>The call (so, the construction) of these services via the <em>container</em></li>
</ul>
<p>The SpiralDi library let you describe the <em>Schema</em> in different ways. The sexiest is probably the XML description file, but it is also possible to define this <em>Schema</em> using the PHP language if you think it&#8217;s more convenient. The following examples are in XML. Here is a quite simple definition file, that describe two services :</p>
<div class="codecolorer-container xml notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;container<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;db&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;Database&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;root&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/constructor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;user&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;User&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;setDb&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;service&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;db&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/container<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Once this file is written, you have to transform it in a SpiralDi understandable format : the Schema.</p>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$builder</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SpiralDi_SchemaBuilder_Xml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFileName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;schema.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$schema</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">buildSchema</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>You can now exploit the built Schema. The Schema is first used to resolve objects. We will see later that it has much more aims. Now, pass this <em>Schema</em> to the <em>Container</em> :</p>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// construction of the container</span><br />
<span style="color: #000088;">$container</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SpiralDi_Container_Default<span style="color: #009900;">&#40;</span><span style="color: #000088;">$schema</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// getting the desired service</span><br />
<span style="color: #000088;">$myUser</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$container</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user</span><span style="color: #339933;">;</span></div></div>
<p>This looks sexy, isn&#8217;t it ? The container has done everything ! Resolution of the object&#8217;s dependencies and injection of these dependencies when needed.</p>
<h3 id="toc-services-scope">Services scope</h3>
<p>All services constructed by SpiralDi have a controlled life time.<br />
In the default behavior, the container will return the same instance of the object, each time the service is needed. This is the <em>singleton</em> scope.<br />
All services are by default configured to have the <em>singleton</em> scope.</p>
<p>You can also use the <em>prototype</em> scope to make the container create a new instance each time the service is needed.</p>
<p>To change the scope of the service, you can use the property</p>
<div class="codecolorer-container text notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">scope=&quot;prototype&quot;</div></div>
<p>in the XML file.</p>
<h2 id="toc-advanced-use">Advanced use</h2>
<p>Now, you know how to describe &laquo;&nbsp;basic&raquo;&nbsp; services and their dependencies. Let&#8217;s see now, more complex features.</p>
<h3 id="toc-using-references-and-methodfactory">Using references and <em>MethodFactory</em></h3>
<p>Sometimes, you will need to inject parameters in a dynamic manner. Imagine that you don&#8217;t know the value of a parameter that you want to inject because it is stored in a configuration object for example.</p>
<p>So, we want the container to construct our services using information from a <em>Config</em> object. The container is able to generate automatically the following code for us :</p>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Config<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$service</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>For this, we have to use the system of references :</p>
<div class="codecolorer-container xml notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;db&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;Database&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;config&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;host&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;config&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;user&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;config&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/constructor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>It is also possible to specify the method that will be used to get arguments. For example</p>
<div class="codecolorer-container text notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">getParam()</div></div>
<p>will produce something like this :</p>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Config<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$service</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;host&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The corresponding XML code will be :</p>
<div class="codecolorer-container xml notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;config&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;host&quot;</span> <span style="color: #000066;">factoryMethod</span>=<span style="color: #ff0000;">&quot;getParam&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></div>
<h3 id="toc-inheriting-and-overloading-services">Inheriting and overloading services</h3>
<p>Quite often, some services are similar. It is possible to use the inheritance system to make the services description lighter.<br />
This is not an actual inheritance between classes, but only the inheritance principle. Thus, you can extend a service description, which make the description file more readable and easier to understand.</p>
<div class="codecolorer-container xml notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;extendedService&quot;</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">&quot;config&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;method2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;value&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<h3 id="toc-container-aware">Container Aware</h3>
<p>In some cases, services have to know the existence of the container, and need to access to it. It is easy to inject the container in a service by using the &laquo;&nbsp;container&raquo;&nbsp; type or the facility &laquo;&nbsp;containerAware&raquo;&nbsp; :</p>
<div class="codecolorer-container xml notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;containerAwareService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;Service&quot;</span> <span style="color: #000066;">containerAware</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #808080; font-style: italic;">&lt;!-- is exactly the same as --&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;containerAwareService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;Service&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;setDiContainer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;container&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>From the class&#8217; point of view, you only have to implement the <em>SpiralDi_ContainerAware</em> interface. In this way, during the construction of the service, the <em>setDiContainer</em> method of your class will be automatically called.</p>
<p>As well, in this case, there is no need to explicitly name your service as &laquo;&nbsp;ContainerAware&raquo;&nbsp; in the description file, the container will automatically detect it</p>
<h2 id="toc-factories">Factories</h2>
<p>SpiralDi also provides a convenient way to use Factories from the DI. This helps generating behaviors like :</p>
<div class="codecolorer-container php notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$service</span> <span style="color: #339933;">=</span> MyServiceFactory<span style="color: #339933;">::</span><span style="color: #004000;">createService</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The corresponding XML code is :</p>
<div class="codecolorer-container xml notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;serviceFactory&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;factory&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;MyServiceFactory&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;createService&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>This should be nice if you want to reuse your old Singleton implementations, among other things&#8230;</p>
<div class="codecolorer-container xml notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;singleton&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;factory&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;Mysingleton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;getInstance&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<h2 id="toc-download-it">Download it ! </h2>
<p>Convinced ? To get the latest version of SpiralDi, you have many solutions :</p>
<h3 id="toc-via-the-mercurial-repository">Via the mercurial repository</h3>
<div class="codecolorer-container bash notmyidea" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">hg clone https:<span style="color: #000000; font-weight: bold;">//</span>ametaireau<span style="color: #000000; font-weight: bold;">@</span>bitbucket.org<span style="color: #000000; font-weight: bold;">/</span>ametaireau<span style="color: #000000; font-weight: bold;">/</span>spiraldi<span style="color: #000000; font-weight: bold;">/</span></div></div>
<h3 id="toc-via-the-archives">Via the archives</h3>
<p>The code is available in the following formats : <a href="http://bitbucket.org/ametaireau/spiraldi/get/tip.zip">zip</a>, <a href="http://bitbucket.org/ametaireau/spiraldi/get/tip.gz">gz</a> ou <a href="http://bitbucket.org/ametaireau/spiraldi/get/tip.bz2">bz2</a></p>
<p>You can also <a href="http://bitbucket.org/ametaireau/spiraldi/">directly check the repository</a>.</p>
<p>That&#8217;s all ! You can now have fun using it ! For functionnality requests, bug report, etc. <a href="http://bitbucket.org/ametaireau/spiraldi/issues/new/">Please</a> !</p>
<h2 id="toc-interesting-readings">Interesting readings</h2>
<p>If you want to know more about dependencies injection, here are some interesting readings on the subject :</p>
<ul>
<li><a href="http://www.martinfowler.com/articles/injection.html">Martin Fowler&#8217;s article on dependencies injection</a></li>
<li>Fabien potencier&#8217;s suite of articles (<a href="http://fabien.potencier.org/article/11/what-is-dependency-injection">1</a>,<a href="http://fabien.potencier.org/article/12/do-you-need-a-dependency-injection-container">2</a>,<a href="http://fabien.potencier.org/article/13/introduction-to-the-symfony-service-container">3</a>,<a href="http://fabien.potencier.org/article/14/symfony-service-container-using-a-builder-to-create-services">4</a>,<a href="http://github.com/fabpot/Pimple/tree/master">5</a>)</li>
<li>Another one from Padraic Brady (<a href="http://blog.astrumfutura.com/archives/394-The-Case-For-Dependency-Injection-Part-1.html">1</a>,<a href="http://blog.astrumfutura.com/archives/395-The-Case-For-Dependency-Injection-Part-2.html">2</a>) </li>
<li>Fabien Potencier has also published a di container <a href="http://twittee.org/">that feet in a tweet</a>. to see, just for fun <img src='http://www.notmyidea.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<h2 id="toc-going-further">Going further ?</h2>
<p>SpiralDi is of course not the only PHP lightweight container. Recently, a lot of container are flowering on the web. Frameworks like Symfony and Flow3<sup>1</sup> are giving a good place to DI. So much the better !</p>
<ul>
<li><a href="http://svn.symfony-project.com/components/dependency_injection/">Symfony&#8217;s services container</a></li>
<li><a href="http://github.com/beberlei/yadif/tree/master">Yadim</a></li>
<li><a href="http://flow3.typo3.org/documentation/reference/object/">Flow3 Objects</a></li>
<li><a href="http://www.beberlei.de/sphicy/">Sphicy</a></li>
<li><a href="http://www.sourceforge.net/projects/phemto">Phemto</a></li>
<li><a href="http://www.seasar.org/en/php5/DIContainer.html">Seasar</a></li>
<li><a href="http://garden.tigris.org/">Garden</a></li>
<li><a href="http://en.wikipedia.org/wiki/Xyster_Framework">Xyster</a></li>
<li><a href="http://www.lionframework.org/">Lion</a></li>
</ul>
<ol class="footnotes"><li id="footnote_0_226" class="footnote">It is new, beautiful, written for PHP 5.3, using AOP, <em>Domain Model</em> persistence&#8230; and of course dependencies injection ! Keep an eye on it : <a href="http://flow3.typo3.org/">Flow3 </a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.notmyidea.org/article/en-introduction-to-spiral-dependency-injection-container-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
