<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">

<channel>
	<title>Pádraic Brady &#187; PHP General</title>
	<atom:link href="http://blog.astrumfutura.com/category/PHP-General/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.astrumfutura.com</link>
	<description>PHP, Zend Framework and Other Crazy Stuff</description>
	<lastBuildDate>Wed, 25 Jan 2012 11:12:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/3.0/</creativeCommons:license>		<item>
		<title>Mockery 0.7.2 Released (And On Packagist.org!)</title>
		<link>http://blog.astrumfutura.com/2012/01/mockery-0-7-2-released-and-on-packagist-org/</link>
		<comments>http://blog.astrumfutura.com/2012/01/mockery-0-7-2-released-and-on-packagist-org/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 11:07:07 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[PHP Security]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[mockery]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=700</guid>
		<description><![CDATA[Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a framework for creating test doubles like mock objects through the use of a simple and succint API capable of clearly defining all possible object operations]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2012%2F01%2Fmockery-0-7-2-released-and-on-packagist-org%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2012%2F01%2Fmockery-0-7-2-released-and-on-packagist-org%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=mockery&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="https://github.com/padraic/mockery">Mockery</a> is a simple yet flexible PHP mock object framework for use in unit testing with <a class="zem_slink" title="PHPUnit" rel="homepage" href="http://www.phpunit.de">PHPUnit</a>, <a href="http://www.phpspec.net/">PHPSpec</a> or any other testing framework. Its core goal is to offer a framework for creating test doubles like mock objects through the use of a simple and succint API capable of clearly defining all possible object operations and interactions using a human readable <a class="zem_slink" title="Domain-specific language" rel="wikipedia" href="http://en.wikipedia.org/wiki/Domain-specific_language">Domain Specific Language</a> (DSL). Designed as a drop in alternative to PHPUnit&#8217;s <a href="https://github.com/sebastianbergmann/phpunit-mock-objects">phpunit-mock-objects</a> library, Mockery is easy to integrate with PHPUnit and can happily operate alongside phpunit-mock-objects.</p>
<p>Today, I am pleased to announce the release of Mockery 0.7.2, a maintenance release fixing a small number of bugs and annoyances. A special thanks to all those who forked the Github project at and submitted pull requests! Leaving a developer with hardly any work to do other than a quick test and merge is always appreciated! You can install or upgrade to the new version from the <a href="http://pear.survivethedeepend.com">survivethedeepend.com PEAR channel</a>.</p>
<p>Another piece of news is that Mockery is now available on <a href="http://packagist.org/packages/mockery/mockery">Packagist.org</a> for users of <a href="http://packagist.org/about-composer">Composer</a>. Composer is a tool to help you manage your own projects&#8217; or librarys&#8217; dependencies and it can handle and mix dependencies from Composer compatible repositories like <a href="http://packagist.org">Packagist.org</a>, any git repository using tags, and any PEAR channel. I do this of my own free will and not because Luis Cordova and Benjamin Eberlei are standing behind me with pitchforks <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>The more pertinant fixes include:</p>
<ol>
<li>Fixed a problem in resolving methods chains which abuse the <a title="Law of Demeter" rel="wikipedia" href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a> (thanks to the wizardly Robert Basic).</li>
<li>Fixed unexpected static calls to an alias mock which were causing fatal errors (thanks to Luis Cordova).</li>
<li>Fixed a crash present since PHP 5.3.6 due to a referenced $this variable entering a closure (thanks to Martin Sadovy).</li>
<li>Added support for PHP_CodeCoverage 1.1 whose filter class is no longer a singleton (thanks to Matthew Vivian).</li>
<li>Added non-halting exception handling (for Mockery exceptions) to the PHPUnit TestListener (thanks to Adrian Slade).</li>
<li>Added boolean $prepend (defaults to FALSE) parameter to  \Mockery\Loader::register() to allow for registering Mockery&#8217;s  autoloader to the top of the autoloader stack even after other  autoloaders have been registered (thanks to Hermann Kosselowski).</li>
<li>Updated documentation/tests for the release of Hamcrest 1.0.0 several  days ago (thanks to me, me, me &#8211; who finally got to do something nobody  else had a pull request for!).</li>
<li>Added new \Mockery::self() static method to make retrieving the current  mock object simpler and more readable while setting expectations without  the need to refer back to past variable assignments.</li>
</ol>
<p>Users should also note that <a href="http://code.google.com/p/hamcrest/downloads/list">Hamcrest 1.0.0</a>, which includes a small filename change (hamcrest.php was capitalised to Hamcrest.php), was released several days ago. If you use Hamcrest matchers with Mockery, you should ensure that both libraries are updated on your system.</p>
<p>As always, please report any bugs or potential improvements to the Github issue tracker using the relevant label or, even more appreciated, send me a pull request.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=bca20a55-a102-4f9e-87be-350304e0b374" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2012/01/mockery-0-7-2-released-and-on-packagist-org/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Storing Session Data In Cookies: Problems And Security Concerns To Be Aware Of</title>
		<link>http://blog.astrumfutura.com/2012/01/storing-session-data-in-cookies-problems-and-security-concerns-to-be-aware-of/</link>
		<comments>http://blog.astrumfutura.com/2012/01/storing-session-data-in-cookies-problems-and-security-concerns-to-be-aware-of/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 21:20:12 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[PHP Security]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=688</guid>
		<description><![CDATA[Back from my extended leave of absence, I&#8217;ll re-open the dusty cobwebbed depths of this blog to echo the sentiments of Paul Reinheimer in his recent article &#8220;Cookies don&#8217;t replace Sessions&#8220;. The topic is actually an old one since Ruby On Rails has adopted the strategy of storing application session data in cookies by default]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2012%2F01%2Fstoring-session-data-in-cookies-problems-and-security-concerns-to-be-aware-of%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2012%2F01%2Fstoring-session-data-in-cookies-problems-and-security-concerns-to-be-aware-of%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=php&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="wp-caption alignright" style="width: 310px"><a href="http://commons.wikipedia.org/wiki/File:ChocolateChipSmile.jpg"><img class="zemanta-img-inserted zemanta-img-configured" title="English: Peanut butter cookie with a chocolate..." src="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ee/ChocolateChipSmile.jpg/300px-ChocolateChipSmile.jpg" alt="English: Peanut butter cookie with a chocolate..." width="300" height="301" /></a><p class="wp-caption-text">Image via Wikipedia</p></div>
<p>Back from my extended leave of absence, I&#8217;ll re-open the dusty cobwebbed depths of this blog to echo the sentiments of Paul Reinheimer in his recent article &#8220;<a href="http://blog.preinheimer.com/index.php?/archives/373-Cookies-dont-replace-Sessions.html">Cookies don&#8217;t replace Sessions</a>&#8220;. The topic is actually an old one since Ruby On Rails has adopted the strategy of storing application session data in cookies by default (take note, performance hounds). The purposes of storing sessions in userland cookies rather than the conventional &#8220;stick-it-on-the-filesystem/database&#8221; used by many apps is one of performance and a little obscuration. Cookie data can be accessed faster than hitting the filesystem/database plus it has the dubious ability to disguise the session-targeted programming language. Really though, PHP is assumed to be on all web servers so hiding its existence is a bit like trying to hide an elephant in a zoo. Hide it all you want &#8211; we still know there has to be one in there!</p>
<p>In exchange for speeding up session reading, storing session data in cookies has some fairly uncomfortable costs.</p>
<p>Now, developers are not unaware of the problems of storing potentially sensitive application data in plain text files on the user&#8217;s PC which users can manipulate, copy, and mangle to their (or the hacker&#8217;s currently fiddling with the user&#8217;s PC) heart&#8217;s content. It&#8217;s dangerous depending on just how much you rely on session data to drive other security rules or restrictions on business logic within the application. Technically, the reliance placed on sessions should be close to nothing &#8211; session data should drive the application towards other storage solutions for the really essential stuff and just stay around as a minimal identifier/stash of basic ID info. Such minimal information can be dumped, corrupted, or overwritten with the only cost being to perhaps require a user to login again when that happens. Stuffing a bank balance into a session, on the other hand, is one (very exaggerated!) example of the kind of data you should be shot for relying on a session for.</p>
<p>Programmers being programmers, it&#8217;s not rare to see sessions become a more intrinsically important storage location than it should be. In those cases, being able to manipulate the session data can become a problem and may give rise to exploitation scenarios where tampering with the stored data leads to some benefit for the manipulator. Obviously we want to make sure that that can&#8217;t happen even in scenarios where programmers may be a bit loose with where they store data. We don&#8217;t build frameworks and libraries for Gurus, we build them for all programmers &#8211; even the sometimes ignorant and under trained ones. This cookie stored session data is often coupled with the ability to encrypt that data. However&#8230;</p>
<p>As Paul Rainheimer remarks <a href="http://blog.preinheimer.com/index.php?/archives/373-Cookies-dont-replace-Sessions.html">in his article</a>, &#8220;Encryption is often viewed as a panacea for security problems, you sprinkle a little encryption dust around, and your problems dissolve&#8221;. This is an absolute truth in programming &#8211; programmers often view encryption as a solution without regard for one teeny tiny problem. If you encrypt a set of data for any purpose, even though it&#8217;s encrypted, the user (or the hacker hacking the user&#8217;s account) still has the data in some usable form!</p>
<p>With perfectly intact data, and even through it&#8217;s hidden by encryption, that data can be recycled simply by copying it to another machine. Depending on the data that is stored (which admittedly may require the hacker/user to figure out by doing actual work like finding your open source app on Github or breaking a developer&#8217;s fingers until they spill the beans), you can restore past data just by copying over a backup of a prior cookie or repeat a past transaction by continually reusing the original cookie it required. Paul offers a few trivial examples in his article.</p>
<p>Such reuse of data is known as a <a class="zem_slink" title="Replay attack" rel="wikipedia" href="http://en.wikipedia.org/wiki/Replay_attack">replay attack</a>. A scenario where even encrypted data can be constantly reused to give rise to a positive result &#8211; all without any need whatsoever to break the encryption. The antidote to this vulnerability is to ensure that all data sets are unique and can be used only once, i.e. you include a single-use nonce (some generated set of characters or bits) in the data which is updated whenever that data is used. This continually forces the update of the relevant digital HMAC signature and/or encryption result (even for the exact same data otherwise) in order to prevent any reuse of old data in a replay attack. Once a nonce is used, it&#8217;s discarded, and the old data can no longer be accepted by your application. Of course, the downside is that since the nonce must be single-use, you need to keep track of all <a class="zem_slink" title="Cryptographic nonce" rel="wikipedia" href="http://en.wikipedia.org/wiki/Cryptographic_nonce">nonces</a> to ensure they are not accidentally used again. You will need a database, possibly using a nonce-included timestamp as a time limit so your storage requirements aren&#8217;t completed insane, which obviously means that just using the traditional database storage for sessions in the first place would have been a much better and simpler choice.</p>
<p>So, in summary, encryption prevents the reading of data but it does not prevent the reuse of existing data. For that to be prevented you need a nonce implementation. And, due to the complexity of using and tracking nonces, practically no cookie stored session solutions will actually offer nonce support because it would eliminate their speed advantage. Which means they are susceptible to replay attacks, which means they are dangerous tools to be swinging around blindly, which means that the old local session storage strategies are still far superior from a security perspective, which all means that you should avoid cookie stores like the damned plague and stick to the old, traditional but secure session storage strategies we already have unless you a) are crazy or b) trust your colleagues (and yourself) not to screw it up.</p>
<p>Even without the security concerns, there is also another less critical downside to storing sessions in cookies which is that cookies have a storage limit of around 4KB. No other storage solution for session data should have that problem but you need to be aware of it anyway as using encryption may push you there sooner than the base data size might suggest (encrypted data size is usually larger than the original data). While noting this, you should never really hit that limit unless you are storing data there that you likely shouldn&#8217;t be anyway!</p>
<p>So, cookie based session storage: It&#8217;s very fast but lethally insecure if you store the wrong type of data. If you&#8217;re going to use it, make sure you keep a tight rein on what data is being stored.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=a2ed3479-04a9-40fd-8d36-625c94f308fb" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2012/01/storing-session-data-in-cookies-problems-and-security-concerns-to-be-aware-of/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Interfacing The PHP World Would Be Good</title>
		<link>http://blog.astrumfutura.com/2011/10/interfacing-the-php-world-would-be-good/</link>
		<comments>http://blog.astrumfutura.com/2011/10/interfacing-the-php-world-would-be-good/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 15:52:19 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=669</guid>
		<description><![CDATA[With some precious free time today, I sat down to read Lukas Smith&#8217;s Interfacing The PHP World. It was good timing since last night I heard someone complain, again, about tight coupling in Zend Framework so I was in a good frame of mind to digest the blog post. Before we start, tight coupling exists]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F10%2Finterfacing-the-php-world-would-be-good%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F10%2Finterfacing-the-php-world-would-be-good%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=php&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>With some precious free time today, I sat down to read Lukas Smith&#8217;s <a href="http://pooteeweet.org/blog/2008">Interfacing The PHP World</a>. It was good timing since last night I heard someone complain, again, about tight coupling in Zend Framework so I was in a good frame of mind to digest the blog post.</p>
<p>Before we start, tight coupling exists in a scenario where any one class relies upon another concrete class type (usually enforced using type hinting). Because the coupling is between two concrete classes, the only way to bypass it is through monkey patching the dependent class. Monkey patching has long been the lazy option for fixing stuff in PHP and we&#8217;re trying to get away from it. Loose coupling, on the other hand, exists when a class is dependent on an interface. Because we can write any class that adheres to that interface, we can inject any class we can imagine so long as it implements that interface. This is a far simple and maintainable situation since a) we are favouring composition over inheritance and b) there&#8217;s no fracking monkey patching!</p>
<p>The common denominator in <a class="zem_slink" title="Loose coupling" rel="wikipedia" href="http://en.wikipedia.org/wiki/Loose_coupling">loose coupling</a> is therefore ensuring your dependent classes accept any dependency matching an agreed interface. Question: Whose interface are we agreeing on?</p>
<p>Every PHP framework has it&#8217;s own unique set of interfaces for common operations such as logging, caching, http clients, filtering, validation, etc. This creates a situation where a framework tends to be loosely coupled but only within the scope of its own interfaces. Thus, Symfony 2 components using HTTP can&#8217;t simply swap the existing client for Zend\Http\Client. Symfony 2 and Zend Framework 2 do not abide by an agreed interface &#8211; they have two distinct and incompatible ones.</p>
<p>Loose coupling is therefore a bad joke. It is a narrowly defined concept usually described within the scope of one particular application. We never really apply the concept across multiple applications written with different frameworks because, at that point, the disparate interfaces of both frameworks would immediately make loose coupling unobtainable.</p>
<p>That is the crux of Lukas&#8217; idea &#8211; and it&#8217;s a really good idea. More interestingly, it&#8217;s almost an even better idea for Zend Framework 2 than Symfony 2. Zend Framework would benefit even more because we also distribute scores of component libraries and the interfaces relied upon make using Zend Framework components almost certainly contingent on the use of many other Zend Framework components to meet dependencies.</p>
<p>A simple example is Zend\Feed\Reader. For want of an agreed HTTP Client interface, we&#8217;re stuck with using…Zend\Http\Client. You could use Symfony 2&#8242;s client but then you&#8217;d need to create an abstract class to mediate between that client&#8217;s methods and the mismatched interface implemented by Zend\Http\Client. The result is therefore obvious &#8211; it requires more work and you&#8217;ll probably end up using both HTTP Clients rather than taking the hard road. Throw in a few more framework odds and ends, and you can be putting a lot of duplicated functionality to work just because they won&#8217;t speak the same language.</p>
<p>This is actually a bad deal for PHP programmers. Instead of one <a class="zem_slink" title="Common Interface" rel="wikipedia" href="http://en.wikipedia.org/wiki/Common_Interface">common interface</a> for HTTP Clients, you have dozens. They won&#8217;t interoperate, they can&#8217;t be swapped for each other, and they directly encourage framework specific implementations instead of interface specific implementations (i.e. with common interfaces the need to duplicate functionality because of NIH Syndrome might be significantly reduced). You&#8217;re also forgetting those libraries who feel compelled to internally and eternally duplicate HTTP client functions rather than simply depending on a preferred client using a common interface. Not to mention half those mini-clients are poorly written and tend to disable SSL certificate verification because they can&#8217;t be arsed about handling the errors from invalid certs even if it does amount to putting your users&#8217; private data at a real risk of being compromised.</p>
<p>The <a href="http://www.hermanradtke.com/blog/please-do-not-interface-the-php-world/">detractors from Lukas&#8217; proposal</a> may point to Java (since it&#8217;s the antithesis to everything PHP except the PHP OOP syntax <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ). While a worthy scapegoat, Java lives in a whole other environment. The scary Enterprise world. There, common interfaces are not merely programming conveniences but a business necessity. Competing products can gain a competitive advantage if they can replace a competitor&#8217;s product, service or middleware with a minimum of fuss. One way to help achieve that is to fund, support and advocate common interfaces for a variety of purposes. Not to mention it still benefits start ups since they already have the template for what to implement. It&#8217;s no wonder you see these debates stocked with members from competing companies actually cooperating so they have the future opportunity to back stab each other with one less barrier <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Those detractors are not necessarily wrong either. On the web, history has favoured programming languages which are practical and flexible. PHP is an unstoppable force of nature for all that other language users criticise its sense of source code aesthetics. Ruby survives primarily because it has a popular framework built using the language. Javascript has proven invaluable for client side execution, and is even moving to the server side with developments like node.js. Python is, well, Python. How can you not like it (weird indentation aside)? Java, on the other hand, has declined in the web space and it&#8217;s sole remaining hope for a revival is greater adoption of the JVM for deploying the results of other languages boiled down into <a class="zem_slink" title="Java bytecode" rel="wikipedia" href="http://en.wikipedia.org/wiki/Java_bytecode">Java bytecode</a>. Flexibility in setting interfaces would be important but that is a bit nonsensical when the adopters are frameworks who feel the competitive pressure to continually evolve…rapidly…and eek more sense out of PHP ugliness.</p>
<p>Then again, do I really want my baby, Zend\Feed\Reader, injected with Symfony 2 classes? It even sounds dirty. Filthy Symfony 2 classes (Zend Framework, my precious! Gollum! Gollum!). Yet, that&#8217;s the only real reason not to want common interfaces. By making your classes more accessible to the competitions&#8217;, you risk being commodotised as programmers mix and match from a selection of notable libraries instead of being hogtied to just a handful of sort-of-loosely coupled frameworks. Then again, HTTP Clients are already a dime a dozen. The real goal of competitiveness is having a better overall implementation in terms of features and all the other important stuff that meets the needs of the users you are targeting (this doesn&#8217;t include useless benchmarks though those do make a great butt for jokes).</p>
<p>So yes, common interfaces would benefit PHP and would make framework libraries more interoperable and thus usable within competing frameworks. Hey, if you can&#8217;t beat them at least make sure you can inject your classes into them. Hmm, still sounds dirty.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=d2b72ffa-65e4-41b3-be32-d85564f1e1de" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2011/10/interfacing-the-php-world-would-be-good/feed/</wfw:commentRss>
		<slash:comments>62</slash:comments>
		</item>
		<item>
		<title>Zend Framework 2.0: Dependency Injection (Part 2)</title>
		<link>http://blog.astrumfutura.com/2011/10/zend-framework-2-0-dependency-injection-part-2/</link>
		<comments>http://blog.astrumfutura.com/2011/10/zend-framework-2-0-dependency-injection-part-2/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 14:20:20 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[Dependency Injection Container]]></category>
		<category><![CDATA[di]]></category>
		<category><![CDATA[dic]]></category>
		<category><![CDATA[pimple]]></category>
		<category><![CDATA[Service Locator]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=631</guid>
		<description><![CDATA[In Part 1 of this miniseries, I expounded (it&#8217;s better than exploding) about Dependency Injection (DI) and Dependency Injection Containers (DICs). To summarise, DI is an obvious and ubiquitous design pattern used daily by most programmers to allow objects accept their dependencies from an external agent (e.g. a unit test which needs to inject mock]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F10%2Fzend-framework-2-0-dependency-injection-part-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F10%2Fzend-framework-2-0-dependency-injection-part-2%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=dependency+injection,Dependency+Injection+Container,di,dic,pimple,Service+Locator&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="zemanta-img" style="margin: 1em; display: block;">
<div class="wp-caption alignright" style="width: 310px"><a href="http://en.wikipedia.org/wiki/File:Touched_by_His_Noodly_Appendage.jpg"><img title="Touched by His Noodly Appendage, a parody of T..." src="http://upload.wikimedia.org/wikipedia/en/thumb/6/6e/Touched_by_His_Noodly_Appendage.jpg/300px-Touched_by_His_Noodly_Appendage.jpg" alt="Touched by His Noodly Appendage, a parody of T..." width="300" height="155" /></a><p class="wp-caption-text">Image via Wikipedia</p></div>
</div>
<p>In Part 1 of this miniseries, I expounded (it&#8217;s better than exploding) about <a class="zem_slink" title="Dependency injection" rel="wikipedia" href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection</a> (DI) and Dependency Injection Containers (DICs). To summarise, DI is an obvious and ubiquitous design pattern used daily by most programmers to allow objects accept their dependencies from an external agent (e.g. a unit test which needs to inject mock objects). In an application, the ideal external agent is some container that can assemble objects on demand and create the necessary <a class="zem_slink" title="Object graph" rel="wikipedia" href="http://en.wikipedia.org/wiki/Object_graph">object graph</a> from scratch outside of the application&#8217;s control flow. It is this object assembly function that can be fulfilled by a DIC.</p>
<p>For Part 2, we&#8217;re going to dig more into what a DIC is and isn&#8217;t. I&#8217;ve already noted one very simple DIC called Pimple which will continue as one of my reference points since it best illustrates just how simple a DIC can be. In Part 3, we&#8217;ll (finally) turn our attention to some actual source code. Baby steps. Parts 1 and 2 should get you thinking so that ZF 2.0&#8242;s DIC is a lot easier to understand and critique. We don&#8217;t want anyone panicking just by throwing them into the deep end <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Make sure to read Part 1 if you haven&#8217;t already! <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<h4>Things Which Are Not A Dependency Injection Container (DIC)</h4>
<p>Now, in explaining a DIC it&#8217;s worth noting there are related solutions which you should find very familiar.</p>
<p>You could use lots of Factories (classes or methods on a class in which the logic necessary to create an object is packaged for reuse). If you followed Part 1, you&#8217;d soon realise that the Pimple DIC, as simple as it is, looks very much like a collection of Factory Methods (as Closures). Zend_Application also appears to use Factory Methods or Classes to generate its resources. Along this line of thinking, a DIC is a container of executable Factories. These tend to be the simplest kind of DICs since they rely on source code instructions which can be combined in sequence to assemble a final dependent object and its injected dependencies.</p>
<p>The differences however all come back to the concept of an &#8220;external agent&#8221;. Factories are traditionally executed within and by an application object whereas a DIC operates from outside an application. Since this is a simple inversion of control, DICs following this mechanism can be written over a coffee break since it&#8217;s just a matter of aggregating and mixing Factories &#8211; or you can just standardise on Pimple <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Another possible solution is to use a Service Locator. This is often interpreted as an object which can create and retrieve objects. The Service Locator is injected into dependent objects as needed so they can lookup their own dependencies. For example, in our earlier Leprechaun class example from Part 1, we could have created a Service Locator capable of creating Pot classes, injected it into Leprechaun, and allowed the Leprechaun class to lookup whatever Pot it needs.</p>
<p>This too looks very similar to how a DIC appears to operate. It&#8217;s also similar to a <a class="zem_slink" title="Factory (software concept)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Factory_%28software_concept%29">Factory Class</a> except it can construct many kinds of objects instead of just one particular type. A Pimple container, for example, can be passed into other objects which in turn can ask it to retrieve necessary objects or dependencies. The same holds true of Zend_Application&#8217;s resulting bootstrap object. So, in yet another line of thinking, a DIC is always a potential Service Locator &#8211; the difference is that we generally don&#8217;t inject DICs into dependent objects but allow the DIC create the dependent object from the outside (i.e. it&#8217;s an external agent).</p>
<p>In summary, a Dependency Injection Container is not simply a standalone invention &#8211; it&#8217;s a combination of a few well known patterns we use in PHP that, when combined, create something greater than its individual (and obvious) parts when acting solely as an external agent. That helps explain why most DICs you look at feel a bit too complex. You might see the simple task it performs but not quite grasp why it needs to be so complex until you realise it&#8217;s a combination of patterns. We&#8217;re used to seeing the constituent patterns isolated and scattered across our application &#8211; not brought together in one single entity.</p>
<p>The primary differentiating factor from the simpler patterns, especially Service Locators, lies in one simple concept: Is our DIC truly an external agent? All other solutions tend to require the container to be an internal agent, i.e. a dependency of other objects. Service Locators are injected into dependent objects, and Factories are called from dependent objects.</p>
<h4>External Agents See The Bigger Picture</h4>
<p>The ideal DIC is an independent external agent. I use the term &#8220;external agent&#8221; a lot because it&#8217;s a good description that&#8217;s easy to grasp. The idea is that the DIC creates all other objects, and their dependencies, and will inject the correct dependencies into the right dependent objects. In other words, it&#8217;s a master manipulator orbiting our application but not actually embedded in it. In a framework, it would be used to create almost everything without the rest of the framework even being aware of its existence. Nearly all other possible solutions can&#8217;t operate in this fashion. A Service Locator must be embedded into other classes and Factories are all called from within other classes too, i.e. they are internal agents…not external.</p>
<p>Now, programmers have two fundamental questions when it comes to basic OOP:</p>
<p>1. Where do I create objects?<br />
2. How do I transport objects across application layers?</p>
<p>DICs answer the first question. You can create objects using a DIC which is independent of the application. It&#8217;s our external agent. They also answer the second question. In applying Dependency Injection, your DIC knows how to inject dependencies into the objects needing them, even if those objects come from different layers of the application. This should render the need for Service Locators, Factories, and the always popular Registry pattern almost defunct.</p>
<p>This is what makes the concept of Dependency Injection and DICs useful in frameworks. If you&#8217;ve ever used Zend Framework before the arrival of Zend_Application you&#8217;ll be familiar with the two questions from above. Creating and transporting objects was an unanswered question at the time, with users running in all directions using Registries, Service Locators, In-Controller instantiation and bootstrap instantiation (mixed with non-static Registries and the handy <a class="zem_slink" title="Front Controller pattern" rel="wikipedia" href="http://en.wikipedia.org/wiki/Front_Controller_pattern">FrontController</a> parameter transport) &#8211; and that&#8217;s just the generic groups. In reality, people developed dozens of varying implementations on these themes. This lack of consistency was an irksome problem. By implementing good DICs, both Symfony 2 and Zend Framework 2.0 have settled on one consistent direction.</p>
<p>However, using a DIC in its designed role requires something of a leap of faith. PHP programmers use Service Locators, Factories and Registries all the time. We&#8217;re comfortable with those patterns and many of us will always crave their simple natures even when we understand why Dependency Injection is a better solution. This craving can end up corrupting the idea of DI by implementing a common antipattern: turning the DIC external agent into an internal agent.</p>
<h4>DICs As Dependencies Are Evil: They Are Not Service Locators</h4>
<p>For example, bearing in mind my earlier creation/transportation question duo. Let&#8217;s say we create a NewsletterController in a Zend Framework 1.x application using a DIC like Pimple. Our Controller requires an instance of Zend_Mail for its emailAction method. How do you get the Zend_Mail object into the Controller? Well, using Dependency Injection the answer is very obvious &#8211; you define either a setter, a constructor parameter or a public property on the Controller class. Then you can program Pimple to a) create the Zend_Mail instance, b) create the Controller and c) inject the Zend_Mail instance into the Controller (e.g. using a setter method). Creation and transportation are neatly solved. The best part is that none of the participants are aware of the DIC, the acid test being that anything a DIC can do, you could have done it by hand without a DIC (hint: a DIC can replace a lot of what ZF users would normally call bootstrapping).</p>
<p>Both Zend Framework 2.0 and Symfony 2 optionally allow another possibility. When we create the NewsController, we can inject the DIC into the Controller itself. This would allow the Controller to lookup resources from the DIC instead of the DIC injecting them from outside the Controller, i.e. our external agent just became an internal agent.</p>
<p>The switch may appear very convenient and comforting. Instead of all this running around with a DIC magically creating Controllers, you could have a typical Dispatch/Execute cycle and add the code for object creation/retrieval into your Controller actions. This has benefits &#8211; object creation is clearly visible in all your controllers. However, your intuition is slightly off base.</p>
<p>Firstly, this isn&#8217;t Dependency Injection. Our dependent objects have now internalised dependency creation. Since it&#8217;s not DI, a DIC is obviously a misplaced tool. So instead of a DIC, you are actually mutating it into a Service Locator. The side effects of eliminating DI are to make it harder to understand dependencies (not easier as you might suspect!). A Service Locator just needs a name to lookup &#8211; there are no setters or constructor parameters with typehinting or useful Docblocks to refer to. So your intuition was wrong &#8211; it might make your life easier, but everyone else who lacks your familiarity with the source code you&#8217;re writing will spend a lot of time dissecting your DIC to figure out what the concrete dependencies really are. Yes, it&#8217;s the age old justification for many practices these days &#8211; ensuring the long term costs of change are minimised.</p>
<p>Secondly, it creates objects which are useless without the specific DIC interface it depends on. Given an application tends to use only one DIC, importing classes from other sources which need a completely different DIC in order to work is a PITA. Classes which don&#8217;t need to be framework specific MUST NEVER be framework specific (i.e. frameworks all use different DICs and depending on one excludes using your classes with another). If classes are DIC specific &#8211; you have done something horribly wrong in practicing OOP (and by extension DI).</p>
<p>This is very similar to my past arguments as to why my idea of Zend Framework Modules does not include Libraries/Components. Apples and oranges. Putting a generic library into any form of framework specific packaging or tying it to a framework specific import/DIC mechanism is just plain wrong if it restricts reuse outside of your preferred framework. It gets a lot worse should such dependencies expand from your Controllers (where they could be restricted in practice) to your service/model layer which shouldn&#8217;t be aware of the framework at all!</p>
<p>Thirdly, DICs are really bad Service Locators. Since your average DIC likely knows almost everything about objects in the application across multiple application layers (by design since it&#8217;s an external agent with that specific purpose), any object into which a DIC is injected might now have access to any other object the DIC knows about. This is crazy. If every object can access every other object, it will result in the same thing as having every function able to access every other function (which we endured before PHP got a good OOP model): Spaghetti Code.</p>
<p>Then again, the Flying Spaghetti Monster might be offended by good OOP&#8230;</p>
<p>The potential for enabling Spaghetti Coding, DIC/framework specific dependencies, and elimination of Dependency Injection practices are all serious issues &#8211; which is why injecting DICs into any other object outside a controlling bootstrap mechanism used to initiate the DIC and get your MVC framework prepped is referred to as an anti-pattern by some. It&#8217;s a Bad Thing.</p>
<h4>Why Do Frameworks Enable Bad Practice?</h4>
<p>Why is water wet? Frameworks operate to a specific set of needs: a compromise between the ideals of the developers and the needs of the users. The goal of a framework is not to hold your hand 100% of the time but to offer an opinionated (all of them are to some degree!) framework which developers hope is capable of meeting a broad set of user needs so that you can get your application off the ground on the cheap and focus on developing your application&#8217;s model. Since many PHP programmers will very quickly want the DIC-Injection anti-pattern, frameworks will inevitably offer it. It may not be considered good practice, but it&#8217;s one that nearly all PHP programmers have used to some degree. The trick is knowing whether the cost is worth it and, if not, how to opt-out of (or avoid opting-in to!)  the anti-pattern.</p>
<p>If it makes you feel any better, framework developers spend an inordinate amount of time debating similar topics. We&#8217;re not out to hang you but compromise and education are often a better solution to being overly restrictive. In reading this topic, I hope this little slice of education will inform you on future decisions around how to recognise, implement and use DICs. Even if you wrote it over your coffee break.</p>
<p>In any case, in Part 3 we&#8217;ll meet Zend\Di. Code at last <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> .</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=c3fb9b9e-cafc-4363-a8a2-bc35c4a0ff63" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2011/10/zend-framework-2-0-dependency-injection-part-2/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Zend Framework 2.0: Dependency Injection (Part 1)</title>
		<link>http://blog.astrumfutura.com/2011/10/zend-framework-2-0-dependency-injection-part-1/</link>
		<comments>http://blog.astrumfutura.com/2011/10/zend-framework-2-0-dependency-injection-part-1/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 14:05:17 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[PHP Security]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[di]]></category>
		<category><![CDATA[dic]]></category>
		<category><![CDATA[pimple]]></category>
		<category><![CDATA[zend\di]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=627</guid>
		<description><![CDATA[If you&#8217;ve been watching the PHP weather vane (we call it Twitter for short), you may have noticed a shift in Symfony and Zend Framework. Version 2.0 of both web application frameworks feature Dependency Injection Containers (DICs) as the primary means of creating the objects (and even Controllers) your application will use. This is an]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F10%2Fzend-framework-2-0-dependency-injection-part-1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F10%2Fzend-framework-2-0-dependency-injection-part-1%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=dependency+injection,di,dic,pimple,zend%5Cdi&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="zemanta-img" style="margin: 1em; display: block;">
<div class="wp-caption alignright" style="width: 247px"><a href="http://commons.wikipedia.org/wiki/File:Codex_Gigas_devil.jpg"><img title="Illustration of the devil, page 577. Legend ha..." src="http://upload.wikimedia.org/wikipedia/commons/2/27/Codex_Gigas_devil.jpg" alt="Illustration of the devil, page 577. Legend ha..." width="237" height="398" /></a><p class="wp-caption-text">Image via Wikipedia</p></div>
</div>
<p>If you&#8217;ve been watching the PHP weather vane (we call it Twitter for short), you may have noticed a shift in Symfony and Zend Framework. Version 2.0 of both web application frameworks feature Dependency Injection Containers (DICs) as the primary means of creating the objects (and even Controllers) your application will use. This is an interesting shift in a programming language that often stubbornly evaded adopting DICs to any great extent. In this mini-series of articles, I&#8217;ll take a look at the marvellous world of Dependency Injection as we run up to an examination of Zend Framework 2.0&#8242;s Zend\Di component in the next part.</p>
<h2>What is Dependency Injection (DI)?</h2>
<p>The short answer to this question is that Dependency Injection is a design pattern where, instead of dependent objects creating their dependencies internally, they instead define setters, constructor parameters or public properties which allow a user to &#8220;inject&#8221; dependencies from the outside into the dependent object and where such dependencies adhere to an expected interface.</p>
<p>If the definition sounds familiar, it&#8217;s because Dependency Injection is an obvious design pattern. As a programmer who knows how to use PHPUnit, you probably use the pattern every time you open an editor. So let&#8217;s quickly look at why the pattern is both obvious and ubiquitous.</p>
<p>Imagine a class implementation called Leprechaun. In writing the class, we realise we have a dependency on another class called PotOfGold. A naïve implementation would start out very simply with the Leprechaun object creating an instance of PotOfGold for use.</p>
<p>If you think this through, you may notice the problems. What if we want our Leprechaun to instead have a PotOfRareEarthElementsFromChina? What if we need to replace PotOfGold with a mock object during unit testing? What if another users locates a bug in PotOfGold and needs to replace it without editing the original class (since it&#8217;s under 3rd party version control)?</p>
<p>The answer to all these questions is to allow external parties to inject dependencies instead of relying on the object to create them internally. Based on our ridiculous example from above, we would define a setter called setPot(), and allow it to accept any object which implements a new Pot interface. Using an interface merely ensures the dependency that is set obeys some interface the dependent object is expecting.</p>
<p>That, in a nutshell, is why Dependency Injection is obvious. It&#8217;s a simple shuffling of creational responsibilities from within an object to some external agent which makes the dependent object more flexible, testable and amenable to the wisdom that Composition is preferred over Inheritance (i.e. injecting objects beats monkey patching!).</p>
<h2>Some External Agent</h2>
<p>In applying Dependency Injection, we eventually reach a state where all objects in a system are created by a mysterious external agent. What is this entity?</p>
<p>One possible candidate is whatever passes for a Controller in your framework based application. In Zend Framework, this would be an instance of Zend_Controller_Action. Our Controller, in this instance, would define an action method which would perform a necessary application task and create all the objects needed to perform that task. This makes a lot of immediate sense to programmers since allowing you to write Controllers with as little fuss as possible is a fundamental goal of any framework.</p>
<p>However, Controllers are objects! If you had a NewsletterController defining an emailAction method, you might expect that creating an instance of Zend_Mail inside that action is obvious (which it is). Think again! In Dependency Injection parlance, your Controller is a dependent object and an instance of Zend_Mail is one of its dependencies. This is no different from our Leprechaun example. If we create the Zend_Mail instance inside the Controller we get the same irritatingly stubborn question. How do we replace the Zend_Mail instance with an alternative, test double or monkey patched version containing an emergency bug fix?</p>
<p>Controllers, alas, are not the external agent we&#8217;re looking for to create objects. And yes, you really should be testing your Controllers <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>The next entity a level above Controllers can be loosely termed the Bootstrap. In Zend Framework 1, this started out as a relatively simple script to do just enough that you could start the FrontController and dispatch a request. In other words, Zend Framework traditionally did not offer a final external agent as needed for Dependency Injection. It left it to individual users to create something of their own or, as became inevitable, to just create objects in the Controllers themselves.</p>
<p>More recent Zend Framework versions offer Zend_Application, a method of bootstrapping that allowed users to define Resources, i.e. using a method or class which created an object (and injected its dependencies) and returned it on demand when it was needed by a Controller. This was the first consistent approach to handling object creation in ZF which effectively involved defining any number of Factory classes or methods in one location and passing the managing object (the Bootstrap) around the application wherever specific objects needed to be retrieved. In effect, this was a Dependency Injection Container. So, surprise, users of Zend Framework already have a DIC. An even lesser surprise: Zend Framework 2.0 will be no different.</p>
<h2>Dependency Injection Containers Are The Devil</h2>
<p>The concept of a Dependency Injection Container (DIC) is to act as a programmable object assembler. You take your DIC, tell it how to construct objects (including how to construct and inject their dependencies), pass the DIC to wherever it&#8217;s needed, and eventually ask it to create an object it knows about. This is not rocket science. DICs are simple animals to understand, however the devilish suspicion that PHP developers have for DICs is not rooted in what they do but how they do it and whether they make a developer&#8217;s life easier.</p>
<p>There&#8217;s a widely known belief that the Ruby language doesn&#8217;t need a DIC. I&#8217;ll use Ruby as an example because it has a few features PHP programmers can salivate over (like how it uses a new method for classes vs PHP&#8217;s new keyword making class subsitutions stupidly easy). One investigator of Dependency Injection from the Ruby world is Jamis Buck. For Ruby he wrote two DICs: Copland (a port of Java&#8217;s HiveMind) and Needle (it&#8217;s like Pimple on steroids which…defeats the purpose). After fighting Ruby for a few years, he finally gave up on trying to write a Ruby DIC and documented his thoughts on his blog in &#8220;<a href="http://weblog.jamisbuck.org/2008/11/9/legos-play-doh-and-programming">LEGOs, Play-Doh, and Programming</a>&#8220;.</p>
<p>The core lesson from the article holds true even in PHP &#8211; by and large, complex DICs are a complete waste of time in most scenarios. Indeed, if you ever use a DIC and discover it requires just as many (if not more) lines of DIC code and configuration as it would to do the same thing in plain old PHP, you should start asking where the fabulous benefits have vanished to because it&#8217;s not delaying the onset of cramped finger muscles as advertised.</p>
<p>Most PHP developers understand this instinctively. Unlike Jamis, most PHP programmers probably won&#8217;t have a strong Java background. As a programming group, we&#8217;re less inclined to assume we need a special DIC blessed by the PHP Gods so we fall back to whatever strikes us as a simpler solution.</p>
<p>But here&#8217;s the rub &#8211; the simplest solution is itself a DIC.</p>
<p>In referring to Dependency Injection Containers as the devil, cursing their name, and blaming them as Java imports designed to make life more complex than needed, it&#8217;s easy to lose sight of the fact that such criticism is about the implementation of DICs and not their actual function. There is nothing wrong with having object assemblers &#8211; we use them all the time and call them Service Locators, or Factory Classes, or Zend_Application (Resources), or any of a dozen terms slightly different and probably not entirely accurate. Most of the time we&#8217;re trying to create a DIC without being aware of the term.</p>
<h2>Needles and Pimples (It&#8217;s Not What You Imagine)</h2>
<p>Jamis Buck hit the nail on the head back in 2004 with the creation of his Needle DIC Ruby. Instead of creating something inspired by Java that relied on static configuration and too many features, he realised that Ruby excelled (as does PHP to a growing degree &#8211; thank Closures) in expressing logic through a Domain Specific Language (DSL). The result was a DIC captured by a simple DSL &#8211; well, until he went and overcomplicated it (read his article).</p>
<p>You can see the exact same fundamental simplicity that a DIC is capable of in PHP. It&#8217;s a small so-tiny-you-won&#8217;t-believe-it DIC called <a href="http://pimple.sensiolabs.org/">Pimple</a>. Try calling that complex, hard, stupid or any other adjective you might instinctively think of when faced with the term &#8220;Dependency Injection&#8221;.</p>
<p>The core of Pimple is that you define object creations as closures. This immediately resolves a few traditional DIC problems. There&#8217;s no static configuration, you hand code all creation logic exactly once, and objects are named services you can recall and inject into other objects from your closure bodies. It basically takes everything you&#8217;d do in creating objects by hand and captures it all in one container. Other than the fact I hate arrays (my version uses object properties instead &#8211; it&#8217;s 50 lines; nobody was killed during its 5 minute development period), Pimple is like Dependency Injection itself &#8211; so blindingly obvious you may kick yourself.</p>
<p>Pimple proves that DICs are not the devil &#8211; they can be incredibly simple and useful tools if you can tame the urge to complicate it&#8217;s implementation.</p>
<h2>Then There Were Frameworks</h2>
<p>As you can probably see, making a strong case for DICs is not hard. Dependency Injection is obvious and omnipresent in PHP. Dependency Injection Containers can be a simple 50 line class you can write over a coffee break. The going gets tough when the simple notions we desperately want to cling to meet the complexity of PHP&#8217;s now standard tool: the application framework.</p>
<h2>Frameworks: Not Written By Monkeys</h2>
<p>As we&#8217;ve already covered, Zend Framework 1.0 covered off the external agent problem in Dependency Injection by creating Zend_Application. As Zend Framework 2.0 moves towards beta, it also needs a Dependency Injection Container to do similar heavy lifting. This time around, we called a spade a spade and the O&#8217;Phinney/Schindler hive mind wrote Zend\Di\DependencyInjector.</p>
<p>The DICs used by Symfony and Zend Framework are not like Pimple. Symfony&#8217;s DIC is driven by static configuration (preferably YAML for brevity). Zend Framework 2.0&#8242;s DIC is driven by a PHP API (no static configuration). Both have their own set of performance boosting measures to minimise any overhead in using a more complex DIC.</p>
<p>In the next part this mini series, we&#8217;ll take a deeper look at Zend\Di and see how it fares compared to Pimple or Symfony 2. In the meantime, I hope I&#8217;ve busted a few apprehensions you might have about using a DIC <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=fa9ed110-fe7b-48f2-98ea-faed2f39d902" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2011/10/zend-framework-2-0-dependency-injection-part-1/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Zend Framework Contributors Mailing-List Summary; Edition #2 (July 2011)</title>
		<link>http://blog.astrumfutura.com/2011/08/zend-framework-contributors-mailing-list-summary-edition-2-july-2011/</link>
		<comments>http://blog.astrumfutura.com/2011/08/zend-framework-contributors-mailing-list-summary-edition-2-july-2011/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 13:08:16 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[PHP Security]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[ZF-Summary]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=622</guid>
		<description><![CDATA[It&#8217;s been a busy month in Zend Framework land which I&#8217;ll blog about shortly so, after a few weeks of delay, here&#8217;s the July 2011 Summary of the zf-contributor&#8217;s mailing list. ZF2 Feedback Late June kicked off with this topic from Robert Basic with a set of notes on his experiences in getting started with]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F08%2Fzend-framework-contributors-mailing-list-summary-edition-2-july-2011%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F08%2Fzend-framework-contributors-mailing-list-summary-edition-2-july-2011%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=ZF-Summary&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="zemanta-img" style="margin: 1em; display: block;">
<div class="wp-caption alignright" style="width: 310px"><a href="http://commons.wikipedia.org/wiki/File:ZendFramework-Logo.png"><img title="Zend Framework logo." src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/ZendFramework-Logo.png/300px-ZendFramework-Logo.png" alt="Zend Framework logo." width="300" height="79" /></a><p class="wp-caption-text">Image via Wikipedia</p></div>
</div>
<p>It&#8217;s been a busy month in Zend Framework land which I&#8217;ll blog about shortly so, after a few weeks of delay, here&#8217;s the July 2011 Summary of the zf-contributor&#8217;s mailing list.</p>
<h3>ZF2 Feedback</h3>
<p>Late June kicked off with this topic from Robert Basic with a set of notes on his experiences in getting started with ZF2 by migrating a ZF1 application. Adam Lundrigan noted, correctly, that a lot of &#8220;bleeding edge&#8221; code is not included in the main repository at this time and is distributed across contributor Github forks. He also raised the suggestion for a ZF2 Status Page. Derek Miranda voiced his agreement with Adam. Robert also agreed noting the difficulty in assessing the state of components.</p>
<p>Summary: ZF2 is scattered across multiple forks &#8211; be prepared to rely on notes such as Robert&#8217;s if jumping in at the deep end.</p>
<h3>Creating a 1.11.9 Hotfix Release</h3>
<p>A short note from Matthew Weier O&#8217;Phinney announced that a 1.11.9 hotfix release would be made to fix a number of backwards compatibility breaks introduced in 1.11.8. Issue tickets involved were ZF-11548, ZF-11550, ZF-10991 and ZF-10725.</p>
<p>Summary: It&#8217;s a maintenance release. It fixes stuff.</p>
<h3>Zend\Http and MVC Developments</h3>
<p>Ralph Schindler presented a document outlining a requirement list and the overall architecture of classes and interfaces for Zend\Http, noting work would commence on a prototype once any outstanding items suggested were cleared. Rob Zienart commented that the document indicated interfaces for Zend\Http Client and Server components and mentioned they needed proposals. Matthew responded that Zend\Http&#8217;s Server would deal with classes extending Zend\Service\Abstract such as SOAP and AMF but would not comprise a HTTP Server given it was covered by PHP 5.4. Anthony Shireman asked whether there were any Zend\Http Server plans or whether it was a &#8220;time will tell&#8221; situation. Matthew confirmed that that was the case given PHP 5.4 would include a HTTP Server and ZF2 could piggy back that implementation in offering a development server environment.</p>
<p>Summary: HTTP work continues. We&#8217;ll need it to communicate with all those big tubes connecting PCs.</p>
<h3>[Proposal] ActiveRecord Proposal</h3>
<p>Artur Bodera raised the proposal and offered to implement an ActiveRecord solution noting its benefits compared to Zend\Db. The proposal was published at <a href="http://framework.zend.com/wiki/display/ZFDEV2/ActiveRecord+-+Arthur+Bodera">http://framework.zend.com/wiki/display/ZFDEV2/ActiveRecord+-+Arthur+Bodera</a> with a working branch at <a href="https://github.com/Thinkscape/zf2/branches/ActiveRecord">https://github.com/Thinkscape/zf2/branches/ActiveRecord</a>.</p>
<p>Nicolas Bérard-Nault asked why it was necessary to reinvent the wheel instead of integrating with other existing and mature implementations. Artur responded that other solutions did not integrate with Zend Framework noting his proposal is built on Zend\Db from ZF2 and he wondered what was the point of Zend\Db\Table otherwise in the face of Doctrine or Propel. Peter Kokx responded to note that Zend\Db\Table implements the Table Data and <a class="zem_slink" title="Row Data Gateway" rel="wikipedia" href="http://en.wikipedia.org/wiki/Row_Data_Gateway">Row Data Gateway</a> patterns as distinct from ActiveRecord and that users shouldn&#8217;t interpret MVC as referring solely to ActiveRecord. Artur conceded that this was a good point but pressed his point that ActiveRecord was one tool which did on impose on any others available to Zend Framework using Zend\Db. Tomáš Fejfar voiced his support for adding ActiveRecord noting its value in simple use cases to get things done fast.</p>
<p>Ralph Schindler leaped to the rescue by noting that ActiveRecord is indeed planned for ZF2 and noting the significant work done to date on Zend\Db in his own feature branch. Artur Bodera welcomed the progress stating he would migrate his ActiveRecord solution over to the improved Zend\Db once complete.</p>
<p>Summary: We&#8217;re getting an ActiveRecord implementation for ZF2.</p>
<h3>ZF2 Docbook Sources Converted to <a class="zem_slink" title="DocBook" rel="wikipedia" href="http://en.wikipedia.org/wiki/DocBook">DocBook</a> 5</h3>
<p>Another short note from Matthew Weier O&#8217;Phinney informed the community that ZF2&#8242;s docbook formatted manual files had been migrated to Docbook 5. The community silently admired the completion of this task (nobody responded but I assume they silently admired all the same!). Matthew noted the README for manual generation would be updated and that Docbook 5 made certain tasks a lot easier.</p>
<p>Summary: ZF2 Manual will be written in Docbook 5, those using a visual XML editor may celebrate.</p>
<p>ZF2 Zend\Mail: To strip/validate or not to strip/validate (email adresses)</p>
<h3>Status of the Test Suite (ZF2)</h3>
<p>Keith Pope asked after the status of the Test Suite mentioning that phpunit.xml was mostly commented out, Zend\Di was not using the @group annotation for the test runner, and TestConfiguration.php was nearing 800 lines. He suggested that the configuration be spread into a conf.d setup (i.e. each configuration segment split into a separate file and all combined at runtime). Matthew responded noting the ease with which ZF2 tests could be run by passing the necessary directory to phpunit from the main /tests directory, and noted configuration may be pushed into phpunit.xml instead of the current PHP file. While expressing an interest in a conf.d setup, Matthew noted this would depend on support in PHPUnit.</p>
<p>Summary: Ignore runtests.sh and just use the stock phpunit commands for ZF2.</p>
<h3>Serious Question about Mcrypt</h3>
<p>Artur Ejsmont observed that the mcrypt filter calls srand() with a limited range of potential seeds thus suggesting it would impact on the security of the filter. Enrico Zimuel replied that the srand() is only used in limited circumstances (where a better source of randomness is not available) and that it&#8217;s not a serious problem since the encryption security is not wholly based on the initialisation vector (IV) that uses srand() on some platforms. Nevertheless, he did note that some improvements could be made.</p>
<p>Artur responded with a general query on the efficacy of using srand() and rand() to avoid collisions. Pádraic Brady responded that rand() was particularly bad noting you could create collision in a matter of minutes. Pádraic also noted that mt_rand() was far more effective but also not entirely random (as a graph of its output would prove) suggesting that it was advisable to use better random sources such as /dev/random and /dev/urandom where feasible. Enrico also noted the availability of openssl_random_pseudo_bytes().</p>
<p>Summary: Getting random bytes is a tricky business.</p>
<h3>ZF2 Zend\Code Bugfix</h3>
<p>Nick Belhomme mentioned he had been looking at Zend\Code which is used heavily by Zend\Di. He noted his first impressions that it should work well by being token based but also referred to his opinion that it was quite error prone and the unit tests were not satisfactory.</p>
<p>To explain his case, he used an example of a method signature accepting four type hinted object parameters noting this could fail to be analysed correctly due to the whitespace in the parameter list (after each comma) not being handled correct by the ParameterScanner. Nick noted he&#8217;d committed a fix using a short trim function to his own git fork.</p>
<p>Regarding the unit tests, Nick explained why the current unit tests were insufficient in testing parameters and suggested rectifying the test doubles to account for whitespace.</p>
<p>Summary: Zend\Code needs to build up a fuller test suite accounting for different coding styles.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=8e7d32eb-d621-4c1e-aa48-1b04fbf5d9ca" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2011/08/zend-framework-contributors-mailing-list-summary-edition-2-july-2011/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>What is Mutation Testing?</title>
		<link>http://blog.astrumfutura.com/2011/08/what-is-mutation-testing/</link>
		<comments>http://blog.astrumfutura.com/2011/08/what-is-mutation-testing/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 16:35:20 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[PHP Security]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Code Coverage]]></category>
		<category><![CDATA[Mutagenesis]]></category>
		<category><![CDATA[mutation testing]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[Test-driven development]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=618</guid>
		<description><![CDATA[Some time ago, in between working on Zend Framework, I booted up a couple of libraries that I really wanted to integrate into my workflow. Recently, I&#8217;ve been being putting these through the grindmill so they can be properly released and supported for public consumption across PEAR. Just as Mockery fell out of older work]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F08%2Fwhat-is-mutation-testing%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F08%2Fwhat-is-mutation-testing%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=Code+Coverage,Mutagenesis,mutation+testing,phpunit,Test-driven+development,unit+testing&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="zemanta-img" style="margin: 1em; display: block;">
<div class="wp-caption alignright" style="width: 310px"><a href="http://en.wikipedia.org/wiki/File:Mutant_Phase.jpg"><img title="The Mutant Phase" src="http://upload.wikimedia.org/wikipedia/en/thumb/6/64/Mutant_Phase.jpg/300px-Mutant_Phase.jpg" alt="The Mutant Phase" width="300" height="297" /></a><p class="wp-caption-text">Image via Wikipedia</p></div>
</div>
<p>Some time ago, in between working on Zend Framework, I booted up a couple of libraries that I really wanted to integrate into my workflow. Recently, I&#8217;ve been being putting these through the grindmill so they can be properly released and supported for public consumption across PEAR. Just as Mockery fell out of older work on PHPMock, Mutagenesis will fall out of another project called MutateMe. This is a short introductory article as to what Mutagenesis will do and why. In other words, what the heck is Mutation Testing?</p>
<p>First, some background.</p>
<p>The most common means of measuring confidence in a test suite is the Code Coverage metric. Code Coverage essentially checks, on a per class basis, how many of the lines of code in the class are executed by a test suite and expresses this as a percentage. For example, a Code Coverage of 85% means 85% of the lines of code in a class was executed and 15% were not. The greater the number of lines of code executed, the more confidence one can presumably have that a test suite is doing its job, i.e. verifying class behaviour, preventing the introduction of bugs, supporting refactoring, and so on.</p>
<p>I have a huge and insurmountable problem with Code Coverage. For starters, my average Code Coverage is closer to 80% than the 90% expected of projects such as Zend Framework. The gap is explained by me not testing what I call &#8220;braindead&#8221; functions, i.e. methods which are either ridiculously simple, where a malfunction would quickly become self-evident, or which are marginalised (on the borders of deprecation). So Code Coverage actually increases the amount of work I need to do for very little gain and a lot of frustration.</p>
<p>Secondly, Code Coverage is easy to spoof or misinterpret. Since it’s a metric measuring the execution of source code, you need only…well…execute the source code. It&#8217;s a simple matter to construct a series of wonderfully useless tests to do just that and obtain a high Code Coverage result &#8211; it&#8217;s done all the time in my experience once someone&#8217;s patience in writing quality unit test runs out. It is particularly evident in cases where unit tests are written after the source code is completed &#8211; a still too common practice in PHP. The less villainous flipside is that certain nuggets of source code are fundamentally difficult to test. For example, a complex algorithm suffering from poor documentation may make composing a suitable unit test near impossible. The rollout of OAuth was filled with such examples.</p>
<p>This leads into my opinion of Code Coverage. I view the venerable Code Coverage metric as a near pointless exercise. While it may tell how much source code a test suite exercises, it tells you nothing about the actual quality of those unit tests. They could be good tests, sort-of-good tests or absolutely horrendous tests &#8211; Code Coverage will never tell you either way. I say near pointless because there are precious few alternatives. We need something to give us a reason to trust and have confidence in test suites and Code Coverage is easy to implement and has been a part of PHPUnit since forever. So, by and large, we make do. We measure Code Coverage just to make certain some kind of unit testing was performed.</p>
<p>Is there nothing better?</p>
<p>A good unit test serves a simple purpose. It verifies a behaviour of an object. In PHP, we&#8217;re more likely to verify umpteen million behaviours in a single test (count your assertions!) but we&#8217;ll let that slide. Since a test verifies behaviour, it follows that a test should fail when that behaviour is changed. If a test does not fail when class behaviour is changed, it also follows that the original behaviour was not fully tested, i.e. there is a gaping hole in our test suite whether due to a flawed or missing test that could allow bugs entry into our application. So, to really stick unit tests under a microscope to assess their quality and our confidence in them, we need to introduce changes into the source code under test and see if the unit test suite can or cannot detect them.</p>
<p>This process is known as Mutation Testing. Mutagenesis is a Mutation Testing framework for PHP 5.3+.</p>
<p>Mutation Testing, as you have probably surmised, is not a super-complex activity. You take a set of source code and compile a list of possible &#8220;mutations&#8221; that are likely to break the behaviour of the source code. Then, you apply one mutation to that source to create a &#8220;mutant&#8221;, i.e. a copy of the source code with the mutation change applied. Next, you run the source code&#8217;s test suite against the mutant and see if any tests fail. If a test fails, celebrate &#8211; the mutation was detected so your tests were, in this instance, adequate. If no test fails, curse the Gods &#8211; the mutation was not detected and you&#8217;ll need to figure out whether a new test is needed or an old one modified/corrected. Rinse and repeat the above for each mutation you&#8217;ve compiled.</p>
<p>Mutations are typically quite simple such as replacing operators, booleans, strings and other scalar values with either an opposing form or a random value. Expressions might also be reversed or driven to zero to give an opposing boolean or zero value. Making such minor changes seems like a minor irritation but behind every serious flaw in an application is one or more smaller contributing errors. If your test cases can detect the potentially contributing errors, then there&#8217;s an excellent chance it would detect the bigger ones anyway. This is known as the Coupling Effect in Mutation Testing.</p>
<p>Some of you will be vaguely aware of Mutation Testing. In terms of implementations, Ruby has heckler, Python has Pester, and Java has Jumbler, Jester and a couple of others. Those who prefer Microsoft&#8217;s technologies can use Nester. There&#8217;s a running ryhme apparent since so much is inspired by the original Jester framework for Java. To my knowledge, Mutagenesis will be the only Mutation Testing framework for PHP (though I sincerely wish I was wrong).</p>
<p>Examining those libraries, you eventually realize a few problems with Mutation Testing which explain its lack of popularity until relatively recently: performance is a concern and Mutation Testing requires a Human Brain to complete the process.</p>
<p>Performance is a concern because each mutation requires a test suite to be executed. Imagine a set of classes from which you extract 100 possible mutations, coupled with a test suite that takes 5 minutes to run. A basic Mutation Testing framework (e.g. Ruby&#8217;s heckler) would therefore take 500 minutes to complete a Mutation Testing session. That&#8217;s 8.3 hours of continuous Mutation Testing. Mutation Testing for Zend Framework would be very interesting <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Similar to Jumbler for Java, Mutagenesis will utilise a few heuristics (shortcuts) to significantly improve performance without compromising results. We only need one single test to fail in order to rule that a mutation was detected and killed, so we can do a few things to boost performance:</p>
<p>1. Terminate the test suite on first failure/error or exception.<br />
2. Execute test cases in order of execution time ascending (fastest first; slowest last).<br />
3. Prioritise execution of last test case to detect a mutant to take advantage of same-class detection.<br />
4. Log which tests detect which mutations, and prioritise those associations in subsequent runs.</p>
<p>The effect of the above is to speed up Mutation Testing by a significant degree. The final heuristic ensures that for gradually changing source code and tests, the first Mutation Testing process might take a while but subsequent runs will be significantly faster making them far more usable in a Test-Driven Development setting. Mutation Testing is best served with a healthy dose of efficiency.</p>
<p>The second reason for its lack of popularity is that Mutation Testing can&#8217;t analyse the logic of the source code under test. For example, an expression might accept any integer less than 10 to evaluate to TRUE. If the input from another class were 7, and a mutation were generated to swap this for a 9, then the associated unit test would still pass (the mutation of switching 7 for 9 still allows the &lt;10 expression evaluate to TRUE). If you recall, if a mutant passes a test suite than we assume either the presence of a flawed test or the lack of a suitable test. Obviously, as the above suggests, this isn&#8217;t always the case. Mutation Testing can and often will report false positives.</p>
<p>Ruling out false positives, coupled with the need to improve test suites to detect more mutations, makes Mutation Testing a source of extra work. Who likes extra work least? Programmers, especially the lazy kind <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Mutation Testing is not a far fetched idea. The principles are sound and it beats the pants off Code Coverage when it comes to measuring what confidence we can have in our testing suites. It is still hampered, as a methodology, by the lack of good implementations in other programming languages. Mutagenesis, by adopting implementation heuristics from Java&#8217;s Jumbler, should avoid that fate and offer a decent framework in PHP that performs as well as can be expected.</p>
<p>Once it&#8217;s released…of course <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . Mutagenesis is in development but should see a fresh release in a couple of weeks alongside Mockery. I&#8217;ll be looking forward to seeing how people perceive it. Mutation Testing has zero presence in PHP to date but having something to complement Code Coverage can&#8217;t do any harm!</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=fa73b631-bedb-4ef0-ac5e-aa59b21aca6a" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2011/08/what-is-mutation-testing/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Out With The Old, In With The New: Original MySQL Extension Heading For Retirement?</title>
		<link>http://blog.astrumfutura.com/2011/07/out-with-the-old-in-with-the-new-original-mysql-extension-heading-for-retirement/</link>
		<comments>http://blog.astrumfutura.com/2011/07/out-with-the-old-in-with-the-new-original-mysql-extension-heading-for-retirement/#comments</comments>
		<pubDate>Sat, 16 Jul 2011 12:48:05 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[PHP Security]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=596</guid>
		<description><![CDATA[When we use the term PHP, we are often silently associating it with the abbreviation LAMP (that&#8217;s Linux, Apache, MySQL and PHP just in case you don&#8217;t recall). MySQL has been our bread and butter in PHP for over a decade; an old friend, accomplice and partner in crime. This was made possible with the]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F07%2Fout-with-the-old-in-with-the-new-original-mysql-extension-heading-for-retirement%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F07%2Fout-with-the-old-in-with-the-new-original-mysql-extension-heading-for-retirement%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=MySQL&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="zemanta-img" style="margin: 1em; display: block;">
<div class="wp-caption alignright" style="width: 310px"><a href="http://en.wikipedia.org/wiki/File:MySQL.svg"><img title="MySQL" src="http://upload.wikimedia.org/wikipedia/en/thumb/6/62/MySQL.svg/300px-MySQL.svg.png" alt="MySQL" width="300" height="155" /></a><p class="wp-caption-text">Image via Wikipedia</p></div>
</div>
<p>When we use the term PHP, we are often silently associating it with the abbreviation <a class="zem_slink" title="LAMP (software bundle)" rel="wikipedia" href="http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29">LAMP</a> (that&#8217;s Linux, Apache, <a class="zem_slink" title="MySQL" rel="homepage" href="http://www.mysql.com">MySQL</a> and PHP just in case you don&#8217;t recall). MySQL has been our bread and butter in PHP for over a decade; an old friend, accomplice and partner in crime. This was made possible with the MySQL extension. Indeed, you can scarcely find a basic nuts and bolts PHP tutorial that doesn&#8217;t use MySQL. Which is probably why it&#8217;s a good idea to give it a huge going away bash (and make sure it finds the exit afterwards and catches a cab to oblivion!). We&#8217;ve since seen replacements like the <a href="http://ie.php.net/mysqli">MySQL Improved extension</a> (mysqli) and <a class="zem_slink" title="PHP" rel="homepage" href="http://www.php.net/">PHP Data Objects</a> (PDO). These are simply better from the additional features each adds to their integration in higher level libraries such as <a class="zem_slink" title="Doctrine (PHP)" rel="homepage" href="http://www.doctrine-project.org">Doctrine</a>.</p>
<p>But, as with any basic change to a successful formula, there was bound to be some controversy at <a href="http://news.php.net/php.internals/53966">the mere suggestion of deprecating our old friend</a> (even if preceded by an extended period of educating users on the well established replacements). <a href="http://www.phpclasses.org/blog/post/153-The-Plot-to-Kill-PHP-MySQL-Extension.html">Manuel Limos</a> and <a href="http://www.prodigyproductionsllc.com/articles/programming/php/no-more-php-mysql-extension/">Lucas Darnell</a> have both written blog posts indicating what a bad idea this could be. Their issues are understandable. Once the E_DEPRECATION notices start flying applications that have existed for years (and years) will appear to implode leaving behind a long line of irritated people who may need to hire a PHP programmer to fix stuff. This obviously imposes a cash cost across thousands (probably an underestimation <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) of businesses. This may lead to hosting services deferring adoption of the PHP version carrying the deprecation by months if not years. Lucas also raised an interesting point that with so much literature, including books, carrying example after example of (often insecure in my opinion) MySQL extension use, user adoption and education may suffer a great deal.</p>
<p>In a riposte to Manual Lemos, <a href="http://neurobashing.com/2011/07/15/theres-a-reason-php-lives-in-the-past/">Gregg Thomason perhaps illustrates best</a> why even the feared disadvantages may be worth the cost. MySQL is a historical relic from a past PHP is trying to leave behind. It&#8217;s old, doesn&#8217;t do a lot to support security and it needs to go. I agree. Gregg says &#8220;&#8230;this is a forward-thinking business and our job is to invent the future.&#8221; Let&#8217;s go invent and improve that future &#8211; if nothing else it might make Anonymous&#8217; job finding SQL injections at every company they squint at a little harder <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> .</p>
<p>PHP is not a weirdo stagnant programming language used by amateurs who don&#8217;t have sufficient brain cells to learn Java, Ruby or Python. That&#8217;s the common misconception based largely on two obvious factors: PHP is so amazingly popular and easy to learn that any innocently ignorant person with half a brain cell can write a fabulously insecure application (the examples just keep coming and coming) and, secondly, PHP is a bit on the ugly side and not a &#8220;true object oriented language&#8221; because it uses functions instead of methods. PHP is actually used by hardcore professionals who build great secure applications and that community has left the original MySQL extension by the wayside in favour of object oriented solutions where MySQL related functions are buried deep behind a wall of classes in their preferred database interaction solution, such as PDO or Doctrine. It&#8217;s about time we brought everyone else up to speed with that reality.</p>
<p>While &#8220;deprecation&#8221; may attract all the attention, let&#8217;s remember that pushing the alternatives by any possible means is a great idea. <a href="http://news.php.net/php.internals/53966">Philip Olson&#8217;s proposal</a> on how to encourage users to move away from the original MySQL extension has a lot of merit and is well worth persuing. We need to let go of the past eventually to keep PHP moving into the future.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=e95d0856-c5e4-4f2f-80db-9e06dba29ca9" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2011/07/out-with-the-old-in-with-the-new-original-mysql-extension-heading-for-retirement/feed/</wfw:commentRss>
		<slash:comments>94</slash:comments>
		</item>
		<item>
		<title>Zend Framework Contributors Mailing-List Summary; Edition #1 (June 2011)</title>
		<link>http://blog.astrumfutura.com/2011/06/zend-framework-contributors-mailing-list-summary-edition-1-june-2011/</link>
		<comments>http://blog.astrumfutura.com/2011/06/zend-framework-contributors-mailing-list-summary-edition-1-june-2011/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 19:12:03 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[ZF-Summary]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=588</guid>
		<description><![CDATA[What&#8217;s this nonsense then? Well, a few weeks ago I shot myself in the foot (I was aiming for the cat who spilled coffee all over my desk) and before my sanity returned to normal, I found myself hoodwinked on IRC into writing up weekly summaries of what is discussed in Zend Framework land. The]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F06%2Fzend-framework-contributors-mailing-list-summary-edition-1-june-2011%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F06%2Fzend-framework-contributors-mailing-list-summary-edition-1-june-2011%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=pear,zend+framework,ZF-Summary&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="zemanta-img" style="margin: 1em; display: block;">
<div class="wp-caption alignright" style="width: 310px"><a href="http://commons.wikipedia.org/wiki/File:ZendFramework-Logo.png"><img title="Zend Framework logo." src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/ZendFramework-Logo.png/300px-ZendFramework-Logo.png" alt="Zend Framework logo." width="300" height="79" /></a><p class="wp-caption-text">Image via Wikipedia</p></div>
</div>
<p>What&#8217;s this nonsense then? Well, a few weeks ago I shot myself in the foot (I was aiming for the cat who spilled coffee all over my desk) and before my sanity returned to normal, I found myself hoodwinked on IRC into writing up weekly summaries of what is discussed in Zend Framework land. The moral of the story is that the attempted murder of any ungrateful coffee-spilling animals sharing your home never ends well.</p>
<p>Let&#8217;s see how good a verbose meandering writer can be at summarising things. I decided to refer to myself by name throughout to avoid confusion.</p>
<h3>Discussion Time: In ZF2, where do things go?</h3>
<p>Ralph Schindler sprang this topic on us back in April and it has stubbornly continued on ever since. Ralph&#8217;s initial question boiled down to where should we put resource files, i.e. files utilised by PHP class files but not written in PHP themselves. The two options presented were to store them relative to the class files inside the library directory or store them in a completely separate parallel directory specifically for resources.</p>
<p>Opinions varied quite a bit and Mike Willbanks opined that we should follow PEAR standards rather doing our own thing and seek to limit include_path performance issues. Matthew Weier O&#8217;Phinney noted that include_path performance concerns should be minimal using ZF2&#8242;s autoloader solution which he has researched, and the intention was to use PEAR or Pyrus. Pádraic Brady (I know that name from somewhere!) chipped in that any decision ought to be made independent of the packaging used, referencing possible weaknesses in how PEAR handles installation, unit tests and documentation viewing. Ralph responded to clarify possible workings of a separate resource directly using simple constants and allowing users to selectively override this noting the existence of the Assetic project (used by Symfony 2). Kevin McArthur added a vote to avoiding PEAR citing the need for multi-version installation support in a final solution and suggested the PHAR format for consideration.</p>
<p>Short version: Someone will make a choice…eventually <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<h3>How to Package ZF2</h3>
<p>Pádraic spawned a new thread from the above earlier topic outlining the options available for packaging source code including PEAR, Pyrus, Git and a Symfony related project (now known as Composer). He also reiterated concerns previously raised regarding PEAR/Pyrus. There followed a side discussion on how individuals were actually deploying applications and managing QA and patches. Matthew raised an objection to the concept of centralised multi-version installs of Zend Framework citing alternative solutions such as deploying applications already containing the Zend Framework version required as easing maintenance and uncertainty. He also asked Kevin McArthur to clarify the use of PHAR. Kevin responded to offer an answer as to why centralised multi-installs were useful citing benefits in minimising the APC cache memory (centralised libraries offering minimal chances of having identical copies being cached), and offering an example bootstrap script for such an architecture to manage version selection.</p>
<p>Matthew also posted responses to points brought up in respect of Pyrus noting, among other things, that it was closer to stable than suspected, that centralised multi-versioning was possibly not as popular as believed, that git support may be possible to add independently, and that XML package definitions had a number of advantages. The debate over centralised multi-version installations of Zend Frameworks continues for a large number of emails without resolution (too much to summarise other than to note each side is firmly divided by the benefits their particular approach and multi-versioning proponents seem more numerous than expected). No concensus was reached over the method of installation with the best summarisations of the respective opinions being emailed in by Matthew and Kevin McArthur. Pádraic chimed in briefly to prompt adoption of PEAR in preference to Pyrus on the basis PEAR is already widely adopted, understood and is easily manipulated at present. This was seconded but there remained a lack of concensus. The topic ends with a suggestive note that adoption of Pyrus may be accepted recognising the absence of another realistic solution at the current time.</p>
<p>Short version: ZF2 may be distributed using Pyrus. Additional needs beyond that may be proposed to PEAR for Pyrus or via another tool. It&#8217;s clear Pyrus will be crop up again in a future discussion.</p>
<h3>ZF2&#8242;s View: Some thoughts for discussion</h3>
<p>Pádraic Brady dropped an email offering his thoughts on the direction of ZF2&#8242;s View which hadn&#8217;t seen huge feedback on the Wiki. The short version was that Zend_View was a God Class, View Helpers were confusing, integration needed improvement and templates needed additional control over layouts/placeholders. He suggested a couple of steps including elimination of the ViewRenderer helper, the replacement of View Helpers with a Controller oriented entity referred to as a &#8220;Cell&#8221;, ensuring the base template of a View had greater control over the rendering process and reiterated previously agreed changes. Marc Bennewitz added several additional concerns and posted a discussion he had with Matthew on the Zend\View\Variables class. Matthew responded with a number of points including keeping the barrier to entry low, recognising all Views are not HTML, and other areas for consideration. Nice to see everything in one place for discussion.</p>
<p>Short version: Not much in the way of disagreement. Seems like a topic that just needs sufficient code for someone to run off and write some proposals.</p>
<h3>Proposal: Don&#8217;t implement BC requirement until ZF 2.1</h3>
<p>Rob Allen emailed a proposal suggesting that backwards compatibility be deferred as a requirement until ZF 2.1. His reasoning focuses on the experience with ZF 1.0 where the frozen compatibility hurt ZF 1.x more than it helped. The proposal was quickly seconded by Ryan Mauger, Anthony Shireman, Rob Zienert (on condition of communicating this clearly to users), and H. Hatfield. Opposing views were aired by Till Klampaeckel on the grounds of keeping migrations between versions simpler. Tomáš Fejfar commented on this being a psychological proposal to increase adoption and raise feedback before the API is finally frozen. Matthew Weier O&#8217;Phinney noted his agreement that bigger features were required to increase early adoption.</p>
<p>Bradley Holt took the opportunity to propose alternative version/release strategies setting the context for the rest of the debate to date. His two points were to a) utilise an odd/even version system where odd numbered minor releases were considered betas and even numbered considered stable, similar to how the Apache HTTP server does things, and b) increase the pace of major releases to shorten the period between allowable compatibility breaks and speed up rolling out such improvements. The debate suggested Rob Allen would agree to faster major releases.</p>
<p>Short version: Implementing BC may be necessary. Might be better to shorten the release cycle and roll out compatibility breaking changes more regularly.</p>
<h3>Proposal: Shorter Release Cycle for Major Versions</h3>
<p>On the back of the previous topic, Bradley Holt elaborated on a proposal for shortening the release cycle for major versions. Pádraic Brady responded in agreement noting that by the time ZF2 was released, there was a possibility that PHP 5.4 with potentially advantageous features would be well on the way to a 2012 release. Based on this he suggested that ZF3 development could be executed quickly with a release date no later than end of 2012 (i.e. 18 months away) with a maximum allowed period of 2 years. Kevin McArthur inquired into a reasonable minimum period before major releases but this seems to the number needing more discussion. There has been no input from the Zend guys to date so this remains up in the air.</p>
<p>Short version: We want ZF3 relatively quickly and not in 4-5 years time.</p>
<h3>Encouraging Usage of ZF 2.0 Beta</h3>
<p>Another discussion opener from Bradley Holt. Bradley suggested an extended beta period, a communication campaign, treating all betas as regular GA releases and highlight applications build on ZF2 to encourage uptake. Kevin McArthur reiterated the need to maintain current versioning and noted his agreement to shortening the major version release cycle and having an extended alpha/beta period. Alessandro Pellizzari emailed in his thoughts from the perspective of a user and the difficulties that currently exist with checking the status of any one ZF2 component. Derek Miranda voiced his agreement with Alessandro&#8217;s thoughts.</p>
<p>Short version: Maybe we need a beta first?</p>
<h3>New dev snapshot released</h3>
<p>On the back of the work going into Zend\DI, Matthew announced the release of a new development snapshot for testing and feedback. Ralph Schindler subsequently posted links to Zend\DI examples. Feedback is ongoing. Anyone is free to check it out and offer some opinion!</p>
<p>Short version: Isn&#8217;t that short enough?</p>
<p>For those of you wondering where to go and track the inner thoughts of the Zend Framework developers, you can join us on the zf-contributors mailing list (available on Nabble <a href="http://zend-framework-community.634137.n4.nabble.com/ZF-Contributor-f680267.html">here</a>) or on IRC channel #zftalk.dev on Freenode.net. Until next time, remember, coffee + cat = bad.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=6b4c675a-0066-4ece-be42-5422b5a14314" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2011/06/zend-framework-contributors-mailing-list-summary-edition-1-june-2011/feed/</wfw:commentRss>
		<slash:comments>88</slash:comments>
		</item>
		<item>
		<title>How Would You Engineer A PEAR2/Pyrus Distribution Architecture?</title>
		<link>http://blog.astrumfutura.com/2011/06/how-would-you-engineer-a-pear2pyrus-distribution-architecture/</link>
		<comments>http://blog.astrumfutura.com/2011/06/how-would-you-engineer-a-pear2pyrus-distribution-architecture/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 13:44:57 +0000</pubDate>
		<dc:creator>padraic</dc:creator>
				<category><![CDATA[PHP General]]></category>
		<category><![CDATA[PHP Security]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[Pyrus]]></category>

		<guid isPermaLink="false">http://blog.astrumfutura.com/?p=581</guid>
		<description><![CDATA[I was recently accused on the Zend Framework Contributors mailing list of having &#8220;strong feelings&#8221; towards Pyrus (i.e. the PEAR Group&#8217;s Installer/Packager for PEAR2) and not in a positive way. It&#8217;s a fair description. PEAR is, putting it lightly, a very old architecture which makes it very resistant to change. With the idea of PEAR2]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F06%2Fhow-would-you-engineer-a-pear2pyrus-distribution-architecture%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.astrumfutura.com%2F2011%2F06%2Fhow-would-you-engineer-a-pear2pyrus-distribution-architecture%2F&amp;source=padraicb&amp;style=normal&amp;service=bit.ly&amp;service_api=padraic%3AR_94101570b7e190f3de921bc15bb9438d&amp;hashtags=pear,Pyrus&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="zemanta-img" style="margin: 1em; display: block;">
<div class="wp-caption alignright" style="width: 240px"><a href="http://commons.wikipedia.org/wiki/File:202clapps.jpg"><img title="Clapps Favorite pear, British Colombia, Canada..." src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/f1/202clapps.jpg/300px-202clapps.jpg" alt="Clapps Favorite pear, British Colombia, Canada..." width="230" height="200" /></a><p class="wp-caption-text">Image via Wikipedia</p></div>
</div>
<p>I was recently accused on the <a class="zem_slink" title="Zend Framework" rel="homepage" href="http://framework.zend.com/">Zend Framework</a> Contributors mailing list of having &#8220;strong feelings&#8221; towards Pyrus (i.e. the PEAR Group&#8217;s Installer/Packager for PEAR2) and not in a positive way. It&#8217;s a fair description. PEAR is, putting it lightly, a very old architecture which makes it very resistant to change. With the idea of PEAR2 and Pyrus, I had hoped to see a renewal &#8211; the advancement of a PEAR architecture for the 21st Century. Instead, and this is just my opinion, PEAR2/Pyrus were a relatively simple iteration on a very old theme.</p>
<h2>A Ranting We Shall Go</h2>
<p>Now, I may be biased since I gave up on PEAR becoming PHP&#8217;s core distribution mechanism after I found myself using alternative strategies for hosting and deployment. This is not to say PEAR is not useful for everyone. It is &#8211; just not in my specific case when developing/testing/deploying applications. It still remains a good distribution means regardless by virtue of its ubiquitous installation with PHP.</p>
<p>I surprised even myself, however, with my vehement outcry over the idea of adopting Pyrus as Zend Framework 2&#8242;s package distribution method, lambasting both it and the PEAR concept of distribution in equal measures while piling up questions on Pyrus&#8217; status (currently released in alpha) and suitability in the near term. That thread showed a fairly divided sentiment. Once I jokingly threatened to mow down my zombified colleagues with a minigun, I figured it was time to go forth and rant (miniguns are too expensive for these recessionary times).</p>
<p>If the PEAR ecosystem has a failing, it is one of staggered evolution. Over time it has picked up additional features tacked on top of a base model. The classic example is the use of Channels (to support multiple repositories) that has more recently prompted calls for the use of a Channel Aggregator to avoid the use cost in locally managing a channel registry or even hosting a Channel. This is the way of many PEAR features. They each do something incredibly useful but do it in a way that has many developers looking for a better approach &#8211; usually to discover the better approach requires breaking compatibility.</p>
<p>My vehemence in the afore mentioned mailing list was down to a simple case of disappointment. We all deal with PEAR because we have it, we know it, and have done so for years. Seeing PEAR2 and Pyrus take the incremental improvement route without apparently doing anything to change the core experience seemed…pointless. It improved a lot of what PEAR already did without actually doing very much different. All the same advantages, disadvantages, features and lack thereof were present and accounted for with a handful of nice headline changes (e.g. we now have package signing capability). What exactly was the purpose of rewriting the entire toolchain if not to seize the opportunity to answer the accusations of those who doubt PEAR is even relevant these days &#8211; by making it the single most relevant development in PHP today?</p>
<h2>One Possible Path Forward</h2>
<p>Since this is a brain dump post, as much to gather my own throughts in one place as anything else, feel free to call me bat shit crazy. There are days even I think that. Below I&#8217;ve raised what I perceive as problems in the PEAR/Pyrus system, obviously from a personal perspective, and possible solutions under the categories of Packaging, Distribution, Installation and Usage. I&#8217;ve tried to avoid getting into technical details &#8211; broad strokes will suffice for now. For your sanity, only the Packaging and Distribution areas are presented today. I will add a similar post for Installation and Usage later in the week. First one to mention &#8220;TL:DR&#8221; gets a minigun round to the head (will have to make do with throwing it at you until I can scrape more cash together for the hardware). To avoid any confusion, I use the terms PEAR and Pyrus to refer to the entire workflow from package generation to end usage for each respectively.</p>
<h2>Packaging</h2>
<p>The packaging of source code for PEAR is performed using the PEAR/Pyrus Installer coupled with a Package Definition (i.e. package.xml) to create a distributable <a class="zem_slink" title="Archive file" rel="wikipedia" href="http://en.wikipedia.org/wiki/Archive_file">archive file</a>. Pyrus utilises a slightly more friendly Package Definition by also allowing for some elements of the definition to be defined in files other than package.xml (e.g. for setting up a changelog file or version numbers). The basic goal of this Package Definition is to have at least one XML file which tells PEAR/Pyrus which files to package, while role a file has (code/docs/tests), where each file goes in a relative filesystem, optionally the file&#8217;s MD5 hash, and a set of metadata like the package name, changelog, version, dependencies, etc. Using Pyrus offers the additional feature of being able to cryptographically sign packages, use a larger number of archive formats including PHAR, and bundle certain package dependencies internally.</p>
<h3>Problems:</h3>
<p>The main problem with the current Package Definition is that it often must be generated by a separate tool since it&#8217;s XML (it&#8217;s that thing everyone used before discovering YAML/JSON), and must explicitly list every file and piece of data within that format (with the exception of Pyrus which allows specifically formatted files to carry version and changelog information among other nuggets) optionally with each file&#8217;s digest hash. Even the Pyrus improvements still require specific files using specific formatted text and/or file names. Using XML just ends up imposing extra work to maintain package details unless you are lucky enough to have a small stable enough package.xml that it can be manually maintained rather then persistently needing generation. A minor aesthetic detail is that XML is harder to read.</p>
<p>Secondly, packages are therefore bound to their archiving restraints. Since package.xml generation is tied to a secondary process, installing from source code may not be feasible whether performed on a local git clone or similarly automated from a remote source where the remote package.xml may well be out of sync with the actual source code or where it may not even exist.</p>
<h3>Possible Solutions:</h3>
<p>The one solution that keeps occuring to me is to simply make a Package Definition programmable, i.e. a small consumable low-maintenance PHP script. Using native PHP, one can create ether a generic array, or a newfangled closure, which can be executed through PHP to populate all the necessary data for a Package Definition for consumption by a package installer.</p>
<p>Since I&#8217;ve dabbled a bit, here&#8217;s what such a Package Definition could look like:</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://blog.astrumfutura.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://blog.astrumfutura.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://blog.astrumfutura.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://blog.astrumfutura.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><div class="php" style="font-family:monospace;"><pre class="de1"><span class="kw2">&lt;?php</span>
&nbsp;
<span class="re0">$package</span> <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span><span class="re0">$s</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">name</span> <span class="sy0">=</span> <span class="st_h">'Overlord'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">authors</span> <span class="sy0">=</span> <span class="st_h">'Padraic Brady, Sauron[sauron@mordor.me]'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">version</span> <span class="sy0">=</span> <span class="st_h">'0.0.1-dev'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">api_version</span> <span class="sy0">=</span> <span class="st_h">'0.0.1-dev'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">summary</span> <span class="sy0">=</span> <span class="st_h">'Monitoring library for Hobbit Detector 1.0'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">description</span> <span class="sy0">=</span> <a href="http://www.php.net/file_get_contents"><span class="kw3">file_get_contents</span></a><span class="br0">&#40;</span>__DIR__ <span class="sy0">.</span> <span class="st_h">'/description.txt'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">homepage</span> <span class="sy0">=</span> <span class="st_h">'http://en.wikipedia.org/wiki/Sauron'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">changelog</span> <span class="sy0">=</span> <a href="http://www.php.net/file_get_contents"><span class="kw3">file_get_contents</span></a><span class="br0">&#40;</span>__DIR__ <span class="sy0">.</span> <span class="st_h">'/changelog.txt'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">files</span><span class="br0">&#91;</span><span class="st_h">'php'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'library/**/*.php'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">files</span><span class="br0">&#91;</span><span class="st_h">'tests'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'tests/**/*.*'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">files</span><span class="br0">&#91;</span><span class="st_h">'ignore'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'*.project'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">files</span><span class="br0">&#91;</span><span class="st_h">'bin'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'scripts/overlord.bat'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">include_path</span> <span class="sy0">=</span> <span class="st_h">'Overlord/Monitor/'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">dependencies</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'PHP[&gt;=5.3.1]'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">dependencies</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'Pear[&gt;=1.6.5]'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">dependencies</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'MutateMe[0.5.0]'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">dependencies</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'ext/runkit'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">optional_dependencies</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st_h">'ext/eyeofsauron'</span><span class="sy0">;</span>
    <span class="re0">$s</span><span class="sy0">-&gt;</span><span class="me1">license</span> <span class="sy0">=</span> <span class="st_h">'New BSD'</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span></pre></div></div></div>
<p>I&#8217;ll assume PHP 5.4 will have some sort of short array notation to cut down the array size. Well, let&#8217;s hope so <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . Would be nice to reduce the line count more. Yes, I did indeed borrow the idea from elsewhere <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>This has a few advantages. No XML to maintain. No need to keep an XML Package Definition synced up for every file change in a VCS. No need for secondary XML generation tools or build tool plugins. Supports downloading files from remote herarchical sources and not just archives (including any VCS source). Developers are already used to versioning build scripts from tools like Phing (just not the end products which are usually ignored whereas package.xml is not). Being plain old PHP, it can be just as complex or as minimal as you want and anyone with basic PHP knowledge can write one.</p>
<p>One can still generate signable archive files using this approach &#8211; the point is to increase the kind of installation sources that can be used rather than replace existing ones. In place of signable packages, for those requiring the security, other package files could be limited to download over <a class="zem_slink" title="HTTP Secure" rel="wikipedia" href="http://en.wikipedia.org/wiki/HTTP_Secure">HTTPS</a>. For example, Github offers git read-only access via HTTPS for all repositories as standard.</p>
<h2>Distribution</h2>
<p>In order to distribute source code using PEAR/Pyrus, you need to make use of either a PEAR Channel or a standalone archive download (i.e. a downloadable tarball). A Channel is basically a whole bunch of XML files served up for access as a REST API. Using a Channel, you can upload packages to the Channel host, update the XML files, and publicise your Channel URI so users can discover your Channel and install your packages.</p>
<h3>Problems:</h3>
<p>PEAR (PHP Extension and Application Repository) was originally founded to serve as a central package distribution channel. For various real and imagined reasons, the concept of a central repository did not succeed in PHP and instead developers insisted on using alternative means. This was aggravated even further by the arrival of frameworks like Zend Framework offering discrete components not originally served over a PEAR Channel at all. PEAR Channels were introduced to allow anyone host their own distinct PEAR Channel as one of those means.</p>
<p>The PEAR Installer has only ever shipped with the main PEAR Channels pre-registered. All other Channels needed to be manually located before use &#8211; usually by referring to the packaged library&#8217;s documentation. Since all Channels are independent entities, there is no global lookup point for querying package details, dependencies and availability. There is also no scope for true package name uniqueness (technically this is accomplished by requiring all packages (except core-PEAR ones) are prefixed with a Channel alias term, e.g. mychannel/MyPackage).</p>
<p>The generation of the REST API, which was the backbone of a Channel, was also complex (the release of Pirum by Fabien Potencier has gone a long way towards simplifying this). Obviously, Channels are also tied to the concept of archive packages and cannot operate directly with a VCS like git. There is a workaround possible for Github using Github Pages to host the REST API.</p>
<p>As alluded to, the REST API is itself a complex graph of XML files that requires a generation tool to manage initial setup and package updates.</p>
<h3>Possible Solutions:</h3>
<p>The best concept to gain early traction was that of a Channel Aggregator expressed by Stuart Herbert. Sadly, I haven&#8217;t seen much more action on that front. In commenting on that idea, I considered it a move towards a decentralised distributed Channel mechanism (mouthful of gibberish, I know!). Here&#8217;s a couple of thoughts on how this could work:</p>
<p>The players would include a Package Authority, a Channel Aggregator (any number of them), and Channels (optional).</p>
<p>The Package Authority would be a centralised location basically for reserving package names and ensuring there is a point of reference and authority to prevent package name duplication and to manage ownership of such. It&#8217;s possible this could also be developed with additional purposes but let&#8217;s keep it simple. This would help, primarily, in removing the need for Channel prefixes on package names and preventing package name confusion. For security reasons, the Package Authority would associate a package name to a specific URI representing a download source (e.g. a PEAR Channel or Git URI)</p>
<p>Channel Aggregators are the more complex beasts. They may be utilised by Channel operators to distribute Package metadata to end-users on demand. The Aggregator would track available packages at source, their basic details, their available versions, and information on the location of host Channels, version control systems, and Package URIs and so forth. In effect, the Aggregator might well replace Channels for many purposes &#8211; and potentially eliminate one more source of work in distributing source code using PEAR/Pyrus.</p>
<p>The ideal scenario here is that any PEAR/Pyrus Installer would pre-register a couple of well-maintained Aggregators saving the users and package distributors the annoyance of dealing with Channels altogether. Hence, we&#8217;re back to a core Channel of sorts but with control of package/source hosting decentralised to individual developers. Again, Aggregators could easily repurpose themselves as package hosters if they wish (such as Pearfarm are doing) though this would be entirely optional.</p>
<p>Channels, as suggested, could well be optional. Use an Aggregator instead and register either a package URI, git repository, or anything else so long as it lets you download the package files (and the PHP programmable Package Definition <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ). Painless hosting? Maybe.</p>
<p>I will point out this would require at least one authentication in the system. You&#8217;d need a Package Authority account to allow for reserving a package name and perhaps transferring it between maintainers. The Aggregator may operate without authentication since it acts much like any aggregator based on your source data (and one would hope a few simple crosschecks with the Package Authority to ensure it&#8217;s not unwittingly aggregating false data from the hackers <img src='http://blog.astrumfutura.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . Package/source hosters could ping the Aggregator as a hint to update its date in a more timely manner.</p>
<p>I won&#8217;t touch the issue of who gets to reserve the package name &#8220;DB&#8221;. The Package Authority may need to enforce specific rules against overly generic names on a common sense basis.</p>
<p>I think that&#8217;s enough for a Monday read (you&#8217;ll all need enough brain capacity to finish out the week!). Feedback is, as usual, welcome. If anyone has a pre-existing solution or one in planning along these or similar lines, drop a comment!</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=cae7e9b5-25ab-4fba-85ae-8e7173884dbb" alt="Enhanced by Zemanta" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.astrumfutura.com/2011/06/how-would-you-engineer-a-pear2pyrus-distribution-architecture/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
	</channel>
</rss>

