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

<channel>
	<title>blog.farly &#187; PHP</title>
	<atom:link href="http://blog.farly.de/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.farly.de</link>
	<description>distorted noise</description>
	<lastBuildDate>Tue, 24 Mar 2015 12:42:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>A modular approach (pt. 1)</title>
		<link>http://blog.farly.de/modular-approach-pt-1/</link>
		<comments>http://blog.farly.de/modular-approach-pt-1/#comments</comments>
		<pubDate>Tue, 20 Jan 2015 16:26:55 +0000</pubDate>
		<dc:creator><![CDATA[jan]]></dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.farly.de/?p=48</guid>
		<description><![CDATA[<p>Okay, I admit: I love php. People apparently don&#8217;t like hate it but I kinda grew up with that language. I just watched The Social Network http://t.co/kzMW1ch6EF&#10;&#10;No wonder Facebook is awful, he did it all in PHP &#38; Emacs. &#8212; I Am Devloper (@iamdevloper) January 4, 2015 Let&#8217;s see what we can do for a change. The recent versions &#8230; <a href="http://blog.farly.de/modular-approach-pt-1/" class="more-link">Continue reading <span class="screen-reader-text">A modular approach (pt. 1)</span></a></p>
<p>The post <a rel="nofollow" href="http://blog.farly.de/modular-approach-pt-1/">A modular approach (pt. 1)</a> appeared first on <a rel="nofollow" href="http://blog.farly.de">blog.farly</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Okay, I admit: I love php. People apparently <del>don&#8217;t like</del> hate it but I kinda grew up with that language.</p>
<blockquote class="twitter-tweet" width="550"><p>I just watched The Social Network <a href="http://t.co/kzMW1ch6EF">http://t.co/kzMW1ch6EF</a>&#10;&#10;No wonder Facebook is awful, he did it all in PHP &amp; Emacs.</p>
<p>&mdash; I Am Devloper (@iamdevloper) <a href="https://twitter.com/iamdevloper/status/551871751047880705">January 4, 2015</a></p></blockquote>
<p><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script></p>
<p>Let&#8217;s see what we can do for a change. The recent versions of php gained some beautiful features as well as some syntactic sugar. Let&#8217;s build a slim framework, that allows any kind of server application to be fragmented into modules. In this part we will look into how we want our modules to look and feel. Here are some design goals:</p>
<ul>
<li>modularity</li>
<li>dependency injection</li>
<li>event based</li>
</ul>
<p><em>Note: This code requires at least php 5.4.</em></p>
<p>Each module is a standard php class. A singleton of that class is kept inside the main module loader. The module loader is also the factory for all modules. Everything is sitting nicely inside folders using and is included using a <a href="https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md" target="_blank">PSR-0 autoloader</a>. Every time a module depends on another module the module loader kicks in and sees if that module instance already exists. If not it <code>init()</code>-ializes it. Either way, the module will be there &#8211; ready to use. That process is called dependency injection. To define dependencies we will just have the required modules names as parameters.</p>
<p><script src="https://gist.github.com/djfarly/3a69d55bd2a304030323.js"></script></p>
<p>Let&#8217;s not worry how the module loader would handle dependency injection for now. But what are we going to do if we need parameters for that function? Yes you guessed right: Returning a function!</p>
<p><script src="https://gist.github.com/djfarly/4b0c3d6f370b36a6a733.js"></script></p>
<p>By returning a function we have the ability to sneak variables into it via the <code>use</code> keyword.  Let&#8217;s take this one more step further before we dig into the module loader.</p>
<p>Our module system is supposed to be event based. Here is our convention for events. If an event is triggered the module loader finds all registered modules, that respond to that type of event. A module responds to an event type if it implements an <code>on&lt;EventType&gt;()</code>-method. There is one problem though. Some modules need to do work before other can.</p>
<p><strong>Example</strong>: Two modules are registered for the <code>request</code>-event. Authentication and FileSystem. Authentication checks if we are allowed to do what we want to do. FileSystem simply responds with the requested file. Neither of those modules knows what the other one does. Authentication can opt out of the chain simply be throwing an Exception. But what if for some reason the module loader choose to execute FileSystems event handler first?</p>
<p>Event handlers need to be prioritized. And again you guessed correctly: We return an object that has a function and a priority build in. What else?</p>
<p><script src="https://gist.github.com/djfarly/c9d42f0982a64b23930c.js"></script></p>
<p>This is a way simplified implementation of what I currently use inside my Authentication module. <code>(object)[]</code> cast the returned array into an stdClass object. This is simply for looks. The the value of that objects link parameter is a function. And it is the same old function we already used earlier. The priority parameter holds a number. The way this syntax looks made me call the returned object &#8220;link object&#8221;.</p>
<p>The module loader will execute all available event handlers for all registered modules. It&#8217;ll then sort the link objects by priority. Higher priority will execute first. (At the moment no-priority defaults to 100, but I guess this is to be discussed.) Then all link functions are executed in order.</p>
<p>Here is a complete module before we discuss how the module loader works internally in the next post. (Notice: <strong>calm</strong> is the name of my current application.)</p>
<p><script src="https://gist.github.com/djfarly/8a8dd17d5f2489d8ae89.js"></script></p>
<p>Pew. The <code></code><code>init()</code> method is executed as soon as the module is first loaded. It is used because <code>__construct()</code> always returns <code>$this</code>. For dependency injection to work, we need to be able to return our link object. Therefore this additional step is necessary to set up stuff that requires stuff from other modules. Yay!</p>
<p>Part 2 will cover the internal workings of the module loader.</p>
<p>The post <a rel="nofollow" href="http://blog.farly.de/modular-approach-pt-1/">A modular approach (pt. 1)</a> appeared first on <a rel="nofollow" href="http://blog.farly.de">blog.farly</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.farly.de/modular-approach-pt-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
