<?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>Tom&#039;s Blog &#187; Programming</title>
	<atom:link href="http://www.encapsulated.org/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.encapsulated.org/blog</link>
	<description>An analog guy in a digital world</description>
	<lastBuildDate>Tue, 15 Dec 2009 20:46:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Unit testing your Javascript: Just Do It</title>
		<link>http://www.encapsulated.org/blog/2009/12/15/unit-testing-your-javascript-just-do-it/</link>
		<comments>http://www.encapsulated.org/blog/2009/12/15/unit-testing-your-javascript-just-do-it/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 20:26:12 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=99</guid>
		<description><![CDATA[Nike&#8217;s slogan is the only one fit for the issue I&#8217;m about to raise in this article: Unit testing. Deep down, everyone knows the benefits from unit testing your code. Unit testing can give you that warm feeling when you go to bed, knowing that the changes you made, didn&#8217;t break previously working code. It [...]]]></description>
			<content:encoded><![CDATA[<p>Nike&#8217;s slogan is the only one fit for the issue I&#8217;m about to raise in this article: Unit testing. Deep down, everyone knows the benefits from unit testing your code. Unit testing can give you that warm feeling when you go to bed, knowing that the changes you made, didn&#8217;t break previously working code. It makes you happy and it gives you confidence.</p>
<p>Yet, a lot of us (including me) don&#8217;t actually start unit testing our code. There are a number of reasons for that, but I&#8217;ll have none of that now. Because now, I&#8217;m going to show you just how easy it is to unit test your Javascript. I&#8217;ll be using <a href="http://docs.jquery.com/QUnit">QUnit</a>, made by John Resig, creator of jQuery. Even though I&#8217;ve just begun using QUnit, the code I presented in my previous three blog posts, has really benefitted greatly from it.</p>
<p><span id="more-99"></span></p>
<h2>Setting up the environment</h2>
<p>To set up your basic unit testing environment, you won&#8217;t need to go through a lot of trouble. It&#8217;s as easy as creating an HTML page which loads the latest jQuery instance, QUnit instance and a stylesheet.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
                    &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;nl&quot; lang=&quot;nl&quot;&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
        &lt;title&gt;Tag library Unit Tests&lt;/title&gt;
        &lt;script src=&quot;http://code.jquery.com/jquery-latest.js&quot;&gt;&lt;/script&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;http://github.com/jquery/qunit/raw/master/qunit/qunit.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;http://github.com/jquery/qunit/raw/master/qunit/qunit.js&quot;&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1 id=&quot;qunit-header&quot;&gt;Tag library Unit Tests&lt;/h1&gt;
        &lt;h2 id=&quot;qunit-banner&quot;&gt;&lt;/h2&gt;
        &lt;h2 id=&quot;qunit-userAgent&quot;&gt;&lt;/h2&gt;
        &lt;ol id=&quot;qunit-tests&quot;&gt;&lt;/ol&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>At this point, nothing much will be shown when you run the HTML file in your browser, as we still have to add our unit tests.</p>
<div class="wp-caption alignnone" style="width: 734px"><img style="border: 0pt none;" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/12/Screen-shot-2009-12-15-at-20.20.34.png" border="0" alt="Empty QUnit page" width="724" height="153" align="left" /><p class="wp-caption-text">Environment without tests</p></div>
<h2>Adding Unit Tests</h2>
<p>Now that you&#8217;ve seen how easy it is to set up your testing environment, it&#8217;s time to actually write a test. I will provide you with some samples from what I&#8217;ve used to test the Tag Javascript library. If you need an API, or you want to know all the possibilities, just head over to the QUnit website.</p>
<p>To keep my stuff organized, I try to reflect the regular structure as much as possible in my tests. This is what I mean:</p>
<div class="wp-caption alignnone" style="width: 231px"><img style="border: 0pt none; display: block;" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/12/Screen-shot-2009-12-15-at-20.29.20.png" border="0" alt="Unit test file structure" width="221" height="251" align="left" /><p class="wp-caption-text">File structure</p></div>
<p>All tests are in separate files, reflecting the original structure. The tests for /tag/tag.pubsub.js are in the file /tests/tag/tag.pubsub.js. I think it&#8217;s a good idea to keep your testing in modules. One big file, or even in the environment HTML file would also work. It&#8217;s just not that easy to work with if you have a lot of tests.</p>
<p>Since I&#8217;m all for the modular way, first thing we add is an indication that a new module is tested</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">/**
     * Define the module:
     */</span>
    module<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag base class&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This goes in the file /tests/tag.js (see image above), and will be referenced in the HTML environment we set up as our first step:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
                    &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;nl&quot; lang=&quot;nl&quot;&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
        &lt;title&gt;Tag library Unit Tests&lt;/title&gt;
        &lt;script src=&quot;http://code.jquery.com/jquery-latest.js&quot;&gt;&lt;/script&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;http://github.com/jquery/qunit/raw/master/qunit/qunit.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;http://github.com/jquery/qunit/raw/master/qunit/qunit.js&quot;&gt;&lt;/script&gt;
&nbsp;
        &lt;!-- add links to unit tests below: --&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;../tag.js&quot;&gt;&lt;/script&gt;&lt;!-- library being tested --&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;tag.js&quot;&gt;&lt;/script&gt;&lt;!-- test code for library --&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1 id=&quot;qunit-header&quot;&gt;Tag library Unit Tests&lt;/h1&gt;
        &lt;h2 id=&quot;qunit-banner&quot;&gt;&lt;/h2&gt;
        &lt;h2 id=&quot;qunit-userAgent&quot;&gt;&lt;/h2&gt;
        &lt;ol id=&quot;qunit-tests&quot;&gt;&lt;/ol&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>In the environment, we first include the file containing the code to be tested. Next we include the file containing the tests for that code. You can add other files for each plugin you want to test.</p>
<p>Ok, our module is defined. The first test I&#8217;m adding is to verify if the file was loaded, and if the Tag namespace is defined in the window scope.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #006600; font-style: italic;">/**
     * Define the module:
     */</span>
    module<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag base class&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * Verify existence of the Tag object:
     */</span>
    test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        expect<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// optional</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying existence of Tag object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">constructor</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying constructor type&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The tests to verify the existence of the Tag object are grouped inside a single test() case. Inside the test case, we can have as many assertions as we want. Here I have added 2 assertions: I verify if the type of window.Tag is an object, and if the constructor is a function. If both these assertions succeed, then that&#8217;s enough proof for me that the Tag base class is loaded and available on the page.</p>
<p>The basic premise of unit testing is that you test the smallest unit that makes up your code. In the case of the Tag library, the smallest unit will be a method from a plugin or the base class. So the next thing we&#8217;re obviously going to do, is test each of the methods inside the Tag object.</p>
<p>As an example, here are the assertions I&#8217;ve written to test the extend() functionality of Tag:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #006600; font-style: italic;">/**
     * Verify working of extend method:
     */</span>
    test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag.extend()&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// an assertion using ok()</span>
        ok<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;Method exists&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// a dummy temporary plugin (also called a mock)</span>
        <span style="color: #003366; font-weight: bold;">var</span> tmp <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            _initCheck<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
            init<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>._initCheck <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Verify reserved namespaces:</span>
        <span style="color: #003366; font-weight: bold;">var</span> reserved <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;extFN&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;extLocalFN&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>reserved<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            ok<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' cannot be overwritten&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// add test plugin</span>
        ok<span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #339933;">,</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Namespace 'test' taken&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ok<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #339933;">,</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Namespace 'test' cannot be taken twice&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">test</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Tag.test plugin exists&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">test</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Test plugin added to FN&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// verify if plugin is extended with extra functionality:</span>
        $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>reserved<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">,</span> ns<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span><span style="color: #009900;">&#91;</span>ns<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
            $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                same<span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> obj<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verified existence of '&quot;</span> <span style="color: #339933;">+</span> key <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// verify if init() was called:</span>
        equals<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">test</span>._initCheck<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Init was run&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The extend() method does a lot of things, naturally, all these things need to be tested. It&#8217;s the only way to be sure that the method does what it promises to do. For complex methods, with lots of execution paths, it will become difficult to test everything. That&#8217;s a reason to keep your methods small, and work with subroutines: they&#8217;re just easier to test (and to understand half a year after you last touched that part of the code).</p>
<h2>Unit testing AJAX calls</h2>
<p>The Tag.core plugin has functionality to asynchronously load javascript files. The problem with AJAX is that it is asynchronous, while unit testing is synchronous. The tests will all be executed in order, no test will run in parallel with another test. Yet we still need to be able to test asynchronous calls. QUnit provides us with the asyncTest() method for this purpose. I haven&#8217;t used that method though, it&#8217;s possible to do it &#8220;manually&#8221;.</p>
<p>We just need to pause the chain of tests before our asynchronous test. Once that test is completed, we can resume the rest of the tests. This is basically just waiting for an asynchronous test to complete, so all things considered, we test an asynchronous test synchronously. Nothing will happen in parallel.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #006600; font-style: italic;">/**
     * Verify working of loadJS method:
     */</span>
    test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag.core.loadJS()&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// verify loading:</span>
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dummy.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// when testing asynchronous functionality, we must temporarily stop the</span>
        <span style="color: #006600; font-style: italic;">// running of the tests:</span>
        <span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">dummy</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying existence of loaded dummy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// start the rest of the tests:</span>
            start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">250</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>First, the chain of tests is paused via the stop() method. As soon as the test is done, the start() method will resume the normal chain of events. Simple but effective.</p>
<p>The asyncTest() method from QUnit does exactly the same as we have done here. It just executes the stop() method for you. You only need to indicate when to resume the tests.</p>
<p><strong>Beware</strong>, an asynchronous test may only contain 1 asynchronous call. E.g. not executing 2 AJAX calls in 1 test but rather just 1. The test itself can contain multiple assertions.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Tag.core.loadJS() multiple&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// verify loading multiple:</span>
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;dummy.js&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;dummy2.js&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">dummy</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying existence of loaded dummy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            equals<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">dummy2</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Verifying existence of loaded second dummy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// start the rest of the tests:</span>
            start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">250</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>The test results</h2>
<p>While writing tests, you will want to see some results. Just save your files, and open the environment HTML file in your browser. The tests will be run, and you&#8217;ll be presented with the results:</p>
<div class="wp-caption alignnone" style="width: 722px"><img style="border: 0pt none;" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/12/Screen-shot-2009-12-15-at-21.09.17.png" border="0" alt="Unit test results" width="712" height="737" align="left" /><p class="wp-caption-text">Complete results, no errors</p></div>
<p>All failed assertions are marked in red. Any test can be clicked to see in a more detailed manner which assertions had what response:</p>
<div class="wp-caption alignnone" style="width: 723px"><img style="border: 0pt none;" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/12/Screen-shot-2009-12-15-at-21.12.39.png" border="0" alt="Failed unit test detail" width="713" height="583" align="left" /><p class="wp-caption-text">Suite with errors and detail</p></div>
<p>If you provided meaningful descriptions in your tests, it should be easy to see where an assertion failed.</p>
<h2>Conclusion</h2>
<p>Unit testing your Javascript functionality couldn&#8217;t be made easier. Getting started shouldn&#8217;t be the threshold, it&#8217;s as easy as creating one HTML file. Writing tests can be a bit of an adventure. At first you&#8217;ll probably make some mistakes, but that&#8217;s okay. The more you write tests, the more efficient you&#8217;ll make them. But you have to start somewhere. Just practice.</p>
<p>Remember, I can only show you the door. You have to walk through it. <img src='http://www.encapsulated.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>All code from previous three blog posts now has a complete coverage from unit tests. You can still see the code at my <a href="http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library">Codaset repository</a>. Please, feel free to download, fork, comment, &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/12/15/unit-testing-your-javascript-just-do-it/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Building your own Javascript library (part 3)</title>
		<link>http://www.encapsulated.org/blog/2009/12/02/building-your-own-javascript-library-part-3/</link>
		<comments>http://www.encapsulated.org/blog/2009/12/02/building-your-own-javascript-library-part-3/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 16:59:34 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=93</guid>
		<description><![CDATA[Goal
In this final part of the series, I&#8217;m going to create a Publish/Subscribe provider for my new framework. With this so-called pubsub provider, it will be possible for any function to create or fire an event. Any other function may then listen for that event, and will get triggered when the event occurs. My main [...]]]></description>
			<content:encoded><![CDATA[<h2>Goal</h2>
<p>In this final part of the series, I&#8217;m going to create a Publish/Subscribe provider for my new framework. With this so-called pubsub provider, it will be possible for any function to create or fire an event. Any other function may then listen for that event, and will get triggered when the event occurs. My main goal is to make sure that this provider is easy to use, not only for plugins, but also for regular functionality. Besides that, I also want to have my code as loosely coupled as possible. For that, I&#8217;m going to use a little bit of Dependency Injection + lazy loading. All this to make sure that plugins are as agnostic as possible about the outside world. If you want to know more, please read on!</p>
<p><span id="more-93"></span><br />
<h2>Updates to the base library</h2>
<p>I&#8217;m going to start with some amends to the functionality I have already created in the <a href="http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/">first part</a> of the series. Don&#8217;t know if you remember, but there I created functionality that is copied inside each plugin that registers itself into the base class. One of these methods is called &#8220;toString&#8221;. During my testing, I have discovered that using that name is not such a good idea. When using <a href="http://getfirebug.com/">Firebug</a>, the &#8220;toString&#8221; method is constantly called when logging a method. That&#8217;s why I decided to rename the method to &#8220;_toString&#8221;.</p>
<p>Besides the name change, I have also added a local cache in the method. This is to make sure that the expensive for loop is executed only once. The result is then put in a local cache. On each subsequent call of &#8220;_toString&#8221;, the local cache is used.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Contains all plugins
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Recursively set values in an object
             *
             * Method allows to set individual settings, without overwriting
             * existing other values in the settings.
             *
             * @param object Object - The object to modify
             * @param data Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  path.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                      <span style="color: #003366; font-weight: bold;">var</span> tmpObject <span style="color: #339933;">=</span> object<span style="color: #339933;">;</span>
                      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span>path.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                          tmpObject <span style="color: #339933;">=</span> tmpObject<span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                      <span style="color: #009900;">&#125;</span>
                      tmpObject<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Tag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Cache object
             */</span>
            _cache<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Set the settings of a plugin
             *
             * @param settings Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            setConfig<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span><span style="color: #339933;">,</span> settings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Automagically determines correct name of a plugin
             *
             * @return String
             */</span>
            _toString<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// Because a loop is costly, we'll only do it once, and cache</span>
                <span style="color: #006600; font-style: italic;">// the result of the loop</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #006600; font-style: italic;">// the name is not cached, so we need to cache it:</span>
                    <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Tag.'</span> <span style="color: #339933;">+</span> k<span style="color: #339933;">;</span>
                            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// notify the user that this is an unknown plugin</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Could not determine the name of the plugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// return the cached name:</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>While I was busy refactoring the &#8220;toString&#8221; method, I added a &#8220;getParent&#8221; method too. This method should be used when you want to refer to the Tag base class from within a plugin, without having to know the name. The advantage is that when Tag gets renamed, the plugins still need very little change in their code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">            <span style="color: #006600; font-style: italic;">//... inside the extFN part of the base object:</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Returns the base object
             *
             * @return Object
             */</span>
            getParent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">return</span> window.<span style="color: #660066;">Tag</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>For such a small method, I&#8217;m not going to repeat the entire base class again. Saves me some space, because this will be a long article <img src='http://www.encapsulated.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p></p>
<h2>Creating the Pubsub plugin</h2>
<p>Now for the new code. We start of with our basic template again:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pubsub <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/**
         * @var object settings - The configuration of this library
         */</span>
        settings<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'pubsub' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pubsub&quot;</span><span style="color: #339933;">,</span>pubsub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here I do need the settings. It will be filled up a bit later. A Pubsub provider will have 3 basic methods. In other implementations, they may have different names than what I&#8217;ll use, but that&#8217;s not very important. The methods I need are these:</p>
<ul>
<li>A method to listen for an event: &#8220;subscribe&#8221;</li>
<li>A method to stop listening for an event: &#8220;unsubscribe&#8221;</li>
<li>A method to fire off an event: &#8220;notify&#8221;</li>
</ul>
<p>I&#8217;ll start with subscribing to an event. The premise here is that a method registers itself as a listener for a particular event. So I need to know: what event? what should happen when that event occurs?</p>
<p>I have to keep track of that information. For this purpose, I&#8217;m going to create 2 properties in the plugin: One for keeping track of what method should be called on what event. And another one to make sure a subscription can easily be undone.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pubsub <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscriptions
         */</span>
        subscriptions<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscription ID's
         */</span>
        subscriptionIDs<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * @var object settings - The configuration of this library
         */</span>
        settings<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
            separator<span style="color: #339933;">:</span><span style="color: #3366CC;">'.'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Create a subscription to a given notification
         *
         * @param plugin String - Name of the plugin that will send out the notification
         * @param evt String - Name of the notifiation event
         * @param fn Function - Callback function to be executed on notification
         *
         * @return integer - The id of the subscription
         */</span>
        subscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._constructPath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                path<span style="color: #339933;">:</span>path<span style="color: #339933;">,</span>
                index<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Construct a path from the arguments
         *
         * @return String
         */</span>
        _constructPath<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> list.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span>.<span style="color: #660066;">separator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'pubsub' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pubsub&quot;</span><span style="color: #339933;">,</span>pubsub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let me walk you through the code. The properties for keeping track are added and I have introduced a setting. This setting will be used in the method &#8220;_constructPath&#8221;. The &#8220;subscribe&#8221; method is added, and takes 3 parameters: the plugin that will fire the event, the event name, and the function that should be executed when that event takes place.</p>
<p>First, I create a &#8216;path&#8217; from the plugin name and the event name. If the plugin is &#8216;Tag.autosuggest&#8217;, and the event is &#8216;gotResponse&#8217;, then the path will be &#8216;Tag.autosuggest.gotResponse&#8217;. I have created a method just to create the path. If I ever find out that the path should change, then I only need to change it in that one method. That&#8217;s called keeping your code <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>.</p>
<p>Second, the properties are filled up with the necessary data. Each path will be stored in the &#8220;subscriptions&#8221; property, along with a list of functions that will be executed when that event occurs. If two listeners subscribe to an event, then the index of that event will have a list of 2 functions.</p>
<p>Third, to make it&#8217;s easy to unsubscribe from an event, a second list is kept. Each subscription gets an entry in that list. The index of that subscription is then returned at the end of the &#8220;subscribe&#8221; method. With this ID, it&#8217;s possible to unsubscribe without having to specify the plugin and event.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pubsub <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscriptions
         */</span>
        subscriptions<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscription ID's
         */</span>
        subscriptionIDs<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * @var object settings - The configuration of this library
         */</span>
        settings<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
            separator<span style="color: #339933;">:</span><span style="color: #3366CC;">'.'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Create a subscription to a given notification
         *
         * @param plugin String - Name of the plugin that will send out the notification
         * @param evt String - Name of the notifiation event
         * @param fn Function - Callback function to be executed on notification
         *
         * @return integer - The id of the subscription
         */</span>
        subscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._constructPath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                path<span style="color: #339933;">:</span>path<span style="color: #339933;">,</span>
                index<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Cancels a subscription to a given notification
         *
         * @param id Integer - ID of the subscription
         *
         * @return Object - Pubsubhub plugin for chainability
         */</span>
        unsubscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// don't remove it with splice, or other id's will be FUBAR</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">path</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Unable to remove this subscription'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Construct a path from the arguments
         *
         * @return String
         */</span>
        _constructPath<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> list.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span>.<span style="color: #660066;">separator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'pubsub' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pubsub&quot;</span><span style="color: #339933;">,</span>pubsub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The &#8220;unsubscribe&#8221; method removes the entries from our lists. Via the given ID, it looks up where the callback function is listed in the &#8220;subscriptions&#8221; property. It is <strong>very</strong> important that we don&#8217;t physically remove the entry from the array. Instead, I just overwrite what was there with &#8220;null&#8221; values.</p>
<p>If we remove the entries via &#8220;Array.splice&#8221;, then the array will be re-indexed. This will invalidate all other ID&#8217;s, so we can&#8217;t use &#8220;Array.splice&#8221;. Using the regular &#8220;delete&#8221; method&#8230;. well, let&#8217;s just say that in this case it would work, but I&#8217;m not too fond of using &#8220;delete&#8221; with arrays. It doesn&#8217;t actually delete the entry, it just writes null values to that index. Yeah, that&#8217;s exactly what I&#8217;m doing manually. But that&#8217;s not called &#8220;deleting an entry&#8221;, so let&#8217;s make sure we don&#8217;t get confused. We don&#8217;t want to end up refactoring &#8220;delete&#8221; with &#8220;Array.splice&#8221;, because we forgot that we don&#8217;t want an actual delete.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pubsub <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscriptions
         */</span>
        subscriptions<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/*
         * @var Object - Contains all subscription ID's
         */</span>
        subscriptionIDs<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * @var object settings - The configuration of this library
         */</span>
        settings<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
            separator<span style="color: #339933;">:</span><span style="color: #3366CC;">'.'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Create a subscription to a given notification
         *
         * @param plugin String - Name of the plugin that will send out the notification
         * @param evt String - Name of the notifiation event
         * @param fn Function - Callback function to be executed on notification
         *
         * @return integer - The id of the subscription
         */</span>
        subscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._constructPath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                path<span style="color: #339933;">:</span>path<span style="color: #339933;">,</span>
                index<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Cancels a subscription to a given notification
         *
         * @param id Integer - ID of the subscription
         *
         * @return Object - Pubsubhub plugin for chainability
         */</span>
        unsubscribe<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// don't remove it with splice, or other id's will be FUBAR</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">path</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptionIDs</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Unable to remove this subscription'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Send out a notification to all subscribers
         *
         * @param plugin String - Name fo the plugin
         * @param evt String - Name of the event
         *
         * @return Object - Pubsubhub plugin for chainability
         */</span>
        notify<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>._constructPath<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>plugin<span style="color: #339933;">,</span> evt<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #006600; font-style: italic;">// make sure we only execute functions, not strings, integers, null's, ...</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Construct a path from the arguments
         *
         * @return String
         */</span>
        _constructPath<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> list.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span>.<span style="color: #660066;">separator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'pubsub' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pubsub&quot;</span><span style="color: #339933;">,</span>pubsub<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>With the &#8220;notify&#8221; method, you fire off an event. There are two arguments: name of the plugin and name of the event. From these two arguments, the path is constructed again. Then I look up in the &#8220;subscriptions&#8221; list if that entry exists. If it does, then all functions listed there will be executed.</p>
<p>The &#8220;notify&#8221; method works as a fire-and-forget system. If there are no listeners for an event, then nothing will happen. There is no communication back to the method that fired the event. That&#8217;s not necessary. It&#8217;s not its task to know if the event will be correctly dispatched.</p>
<p>At the moment, &#8220;notify&#8221; doesn&#8217;t transmit a payload to the subscribers. I&#8217;m sure I will need that functionality eventually. I&#8217;ll add it later, when the need is actually there.</p>
<p></p>
<h2>The problem of scope</h2>
<p>When a method subscribes to an event, and then gets executed, there will be a problem with the scope of the method. The Pubsub plugin doesn&#8217;t know anything about the callback function, other than it is a function. It can either be an anonymous function, or it can be part of an object. Pubsub doesn&#8217;t know. Hence, when the function is called, there won&#8217;t be a scope. The scope is the function itself. For an anonymous function, that&#8217;s good. For a method that&#8217;s part of an object, that&#8217;s not good</p>
<p><strong>Possible solution 1</strong>: Just execute the callback in the scope of the Pubsub provider, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ...</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subscriptions</span><span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// ...</span></pre></div></div>

<p>This is not a good solution, because anonymous functions suddenly have a scope and methods from an object suddenly have a scope that isn&#8217;t theirs. If the method is &#8216;Tag.autosuggest.updateSuggestions&#8217;, then the scope should be &#8216;Tag.autosuggest&#8217;, and not &#8216;Tag.pubsub&#8217;.</p>
<p><strong>Possible solution 2:</strong> Execute the callback in the scope of the notifier. Via some code, it should be easy to determine the scope of the notifier. But again, my same objections apply as with the first solution: Anonymous functions shouldn&#8217;t have a scope, and I don&#8217;t want to change the scope of methods from an existing object</p>
<p><strong>Possible (and accepted) solution 3:</strong> Make sure the callback already has the correct scope before executing it. (see below)</p>
<p></p>
<h2>Hitching a ride</h2>
<p>(I told you this was going to be a long article) The idea comes from &#8220;dojo.hitch&#8221;: Via another method making sure that a given function is executed in a given scope. So I created &#8220;Tag.core.hitch&#8221; for this purpose. The name comes from <a href="http://www.dojotoolkit.org/">Dojo</a>, and the implementation is derived from dojo&#8217;s implementation.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">        <span style="color: #006600; font-style: italic;">// inside the Tag.core plugin:</span>
        <span style="color: #006600; font-style: italic;">/**
         * Runs fn in the given scope
         *
         * @param scope Object - The scope fn should run in
         * @param fn Function|String - The function to run
         *
         * Extra parameters are optional, and will be passed on to fn
         *
         * @return Function
         */</span>
        hitch<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// arguments looks like an Array, but is an Object</span>
            <span style="color: #006600; font-style: italic;">// here we convert it to an array</span>
            <span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #339933;">=</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> scope <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span> <span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">typeof</span> fn <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// both scope &amp; fn should be defined</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Tag.hitch: both scope and function should be defined'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> fn <span style="color: #339933;">===</span> <span style="color: #3366CC;">'string'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> scope<span style="color: #009900;">&#91;</span>fn<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'The function is not defined inside the given scope'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> scope<span style="color: #009900;">&#91;</span>fn<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> args.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> args.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method returns an anonymous function. Inside that anonymous function, the scope of the given function is set to the scope that&#8217;s defined in the arguments. Quite simple actually. But very effective.</p>
<p>With the hitch method, a subscriber is now in charge of making sure that the callback function is executed in the correct scope.</p>
<p></p>
<h2>Dependency Injection</h2>
<p>Before I come to the usage examples, there is one more thing I&#8217;d like to implement. You know how I like keeping my plugins agnostic. Well, instead of letting a plugin use the pubsub provider like this: &#8220;Tag.pubsub.notify(&#8230;)&#8221;, I want to make sure that it&#8217;s not important that the pubsub provider is called &#8220;Tag.pubsub&#8221;. In fact, if someone comes along and writes a better provider, it should be possible to use that provider without having to change the code of the plugins.</p>
<p>This can be accomplished with a little Dependency Injection. (DI via getters and setters to be precise). I&#8217;ll extend core plugin with 2 methods: &#8220;getPubsubProvider&#8221; and &#8220;setPubsubProvider&#8221;. With the getter, the Pubsub provider is returned. Via he setter it&#8217;s possible to specify another Pubsub provider. As the icing on the cake, I&#8217;ll add some lazy loading: if no pubsub provider is specified, then &#8220;getPubsubProvider&#8221; defaults to &#8220;Tag.pubsub&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Contains all plugins
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Recursively set values in an object
             *
             * Method allows to set individual settings, without overwriting
             * existing other values in the settings.
             *
             * @param object Object - The object to modify
             * @param data Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  path.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                      <span style="color: #003366; font-weight: bold;">var</span> tmpObject <span style="color: #339933;">=</span> object<span style="color: #339933;">;</span>
                      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span>path.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                          tmpObject <span style="color: #339933;">=</span> tmpObject<span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                      <span style="color: #009900;">&#125;</span>
                      tmpObject<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Tag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Cache object
             */</span>
            _cache<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/*
             * @var Object - Pubsub provider
             */</span>
            pubsubProvider<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Set the settings of a plugin
             *
             * @param settings Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            setConfig<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span><span style="color: #339933;">,</span> settings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Inject another Pubsub Provider
             *
             * @param obj Object - The new Pubsub Provider
             *
             * @return Object - Current plugin for chainability
             */</span>
            setPubsubProvider<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// verify if necessary functionality is present:</span>
                <span style="color: #003366; font-weight: bold;">var</span> fns <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;subscribe&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;unsubscribe&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;notify&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #003366; font-weight: bold;">var</span> validObject <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>fns<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> obj<span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        validObject <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>validObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pubsubProvider</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Get the Pubsub Provider
             *
             * @return Object - Pubsub Provider
             */</span>
            getPubsubProvider<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// lazy loading:</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pubsubProvider</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pubsubProvider</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">pubsub</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pubsubProvider</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Returns the base object
             *
             * @return Object
             */</span>
            getParent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">return</span> window.<span style="color: #660066;">Tag</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Automagically determines correct name of a plugin
             *
             * @return String
             */</span>
            _toString<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// Because a loop is costly, we'll only do it once, and cache</span>
                <span style="color: #006600; font-style: italic;">// the result of the loop</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #006600; font-style: italic;">// the name is not cached, so we need to cache it:</span>
                    <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Tag.'</span> <span style="color: #339933;">+</span> k<span style="color: #339933;">;</span>
                            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// notify the user that this is an unknown plugin</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Could not determine the name of the plugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// return the cached name:</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._cache<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_toString'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s the entire base class for now. As you can see, &#8220;setPubsubProvider&#8221; verifies if the new provider has all three required methods. I added this so that the plugins don&#8217;t have to be rewritten. If a new Pubsub Provider is introduced, with different names for the methods, then all you have to do is write an adapter for it, and set it via &#8220;setPubsubProvider&#8221;.</p>
<p></p>
<h2>Usage</h2>
<p>A lot of code has been written and added. Here are some examples how all this can be used. For most examples, I&#8217;ll use a fictive &#8220;Tag.autosuggest&#8221; plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// inside the Tag.autosuggest plugin, we throw an event:</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(you see, no &#8216;Tag&#8217; mentioned here. I like!)</p>
<p>Inside another plugin, we listen for the above event:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autosuggest</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Treshold has been reached'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(Still no mention of &#8216;Tag&#8217; here. True, we have to write a lot of code, but it&#8217;s future proof)</p>
<p>Instead of an anonymous function, I want to execute a method of an object. We have to keep scope here:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autosuggest</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">callbackFunction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(&#8220;callbackFunction&#8221; will be executed in the correct scope)</p>
<p>An alternative to the above is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autosuggest</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;callbackFunction&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(Here, &#8220;callbackFunction&#8221; is specified as a string. Other than that, the exact same will happen as the previous example)</p>
<p>Unsubscribe from an event:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autosuggest</span>._toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'thresholdReached'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">core</span>.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">callbackFunction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPubsubProvider</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">unsubscribe</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All above examples implied another plugin. But the code can also be used in regular Javascript, provided that Tag and Tag.pubsub are available on the page.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> doCalculation <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Tag.<span style="color: #660066;">pubspub</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'doCalculation'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'beforeCalculation'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
    Tag.<span style="color: #660066;">pubspub</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'doCalculation'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'afterCalculation'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> Tag.<span style="color: #660066;">pubsub</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'doCalculation'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'afterCalculation'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Calculation complete'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Tag.<span style="color: #660066;">pubsub</span>.<span style="color: #660066;">unsubscribe</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Instead of the plugin name, I provided the function name to the &#8220;notify&#8221; and &#8220;subscribe&#8221; methods.</p>
<p></p>
<h2>Conclusion</h2>
<p>Finally, the end of a lengthy article. With the pubsub plugin, it should be easy to make your plugins event-driven, and loosely coupled. Already, a shortcoming is visible: It should be possible to transmit data from the notifier to the subscriber. In the coming days or maybe next week, I&#8217;ll refactor the code to allow that. Once that feature is added, it will be possible to write your application as a <a href="http://en.wikipedia.org/wiki/Blackboard_system">blackboard system</a>, with all functionality independent from each other.</p>
<p>In these articles, I have introduced some principles from the OO world, like Dependency Injection, modular design, lazy loading, DRY, &#8230; If you want to read them again, the <a href="http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/">first article is available here</a>, and the <a href="http://www.encapsulated.org/blog/2009/12/01/building-your-own-javascript-library-part-2/">second article is available here</a>. The code of this library is under constant development. I&#8217;ve made it available under the MIT license on my Codaset account on <a href="http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library">http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library</a>. Feel free to fork it, add change requests or bug reports. If you want to use the code, and you create plugins for it, please let me know. I&#8217;m curious if and how this will be used by other people.</p>
<p>Thank you for reading my article. Hope you enjoyed it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/12/02/building-your-own-javascript-library-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building your own Javascript library (part 2)</title>
		<link>http://www.encapsulated.org/blog/2009/12/01/building-your-own-javascript-library-part-2/</link>
		<comments>http://www.encapsulated.org/blog/2009/12/01/building-your-own-javascript-library-part-2/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 16:15:53 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=91</guid>
		<description><![CDATA[Goal
In this second installment of my 3-part library-building journey, I&#8217;m going to make the core plugin for the library. This plugin will contain utility methods. Stuff that can be used, either by other plugins, or just by regular Javascript code. So far, I haven&#8217;t needed many utility methods, so this chapter will be rather short. [...]]]></description>
			<content:encoded><![CDATA[<h2>Goal</h2>
<p>In this second installment of my 3-part library-building journey, I&#8217;m going to make the core plugin for the library. This plugin will contain utility methods. Stuff that can be used, either by other plugins, or just by regular Javascript code. So far, I haven&#8217;t needed many utility methods, so this chapter will be rather short. If you&#8217;re ready, read on!</p>
<p><span id="more-91"></span><br />
<h2>Creating the Core plugin</h2>
<p>So far, the only functionality I have needed, was dynamically loading other Javascript files on a page. jQuery is perfectly capable of doing just that. I&#8217;m just going to wrap it up in a method or two.</p>
<p>First, let&#8217;s start off with the base of my plugin. A self-contained, anonymous function that will be executed immediately.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> core <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'core' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;core&quot;</span><span style="color: #339933;">,</span>core<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In <a href="http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/">my previous blog post</a>, there were some extra&#8217;s there: settings and an init method. I won&#8217;t be needing those at this moment, so I left them out.</p>
<p>There are two important things I need when dynamically loading other Javascript files. I want to be able to do something when the file is loaded. And in some cases I want to make sure that a file is loaded only once. The reason for that last feature is that when I want to create self-contained <a href="http://framework.zend.com/manual/en/zend.view.helpers.html">partials</a>, I don&#8217;t want to load the same file over and over again when that partial is put on my page more than once.</p>
<p>That being said, the first method will accept a file URL and optionally a callback. The callback allows me to execute code after the file was loaded.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> core <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/**
         * Loads a JS library via Ajax and execute an optional callback function
         *
         * @param url String - The location of the script
         * @param callback Function - Optional callback to execute after script was loaded
         * @return Object - Core plugin for chainability
         */</span>
        loadJS<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> callback <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                callback <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// load it via Ajax</span>
            $.<span style="color: #660066;">getScript</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'core' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;core&quot;</span><span style="color: #339933;">,</span>core<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, I just used jQuery&#8217;s <a href="http://docs.jquery.com/Ajax/jQuery.getScript#urlcallback">getScript()</a> method, so all restrictions apply. The return value of the method is the current object or scope. This allows you to chain multiple methods, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/first.js'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/second.js'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">someOtherFunctionFromCore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This keeps it all nice and tidy. You can read up on chainability in Javascript here on <a href="http://javascriptant.com/articles/32/chaining-your-javascript-methods">Javascript Ant</a>. Chaining methods is commonly used in PHP as well. I like that principle, because it makes your code shorter and more readable.</p>
<p>Now that I can load 1 file, I want to be able to make sure it&#8217;s loaded only once. For that the plugin needs an extra method, and a list to put the loaded libraries in.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> core <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">/**
         * @var array - List of loaded JS scripts
         */</span>
        libraries<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Loads a JS library via Ajax and execute an optional callback function
         *
         * @param url String - The location of the script
         * @param callback Function - Optional callback to execute after script was loaded
         * @return Object - Core plugin for chainability
         */</span>
        loadJS<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> callback <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                callback <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// load it via Ajax</span>
            $.<span style="color: #660066;">getScript</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">/**
         * Loads a JS library once via Ajax, with optional callback
         *
         * @param url String - The location of the script
         * @param callback Function - Optional callback to execute after script was loaded
         * @return Object - Core plugin for chainability
         */</span>
        loadOnce<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> callback <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                callback <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">inArray</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">libraries</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// load JS:</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// append to list:</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">libraries</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// just execute the callback when the library is already loaded</span>
                callback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Let's put this in the 'core' namespace of TAG:</span>
    Tag.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;core&quot;</span><span style="color: #339933;">,</span>core<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The &#8220;loadOnce&#8221; method takes the same input as the &#8220;loadJS&#8221; method. But as a bonus, it keeps track of the files it has loaded. When some code is trying to load a file for the second time, then there is no penalty. The callback will just be executed, just as if it were the first time the file was loaded.</p>
<p></p>
<h2>Usage</h2>
<p>I&#8217;ll just give you some examples. Simplest case, you just want to load a file:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Load a couple of files:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/anotherfile.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// OR</span>
	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/anotherfile.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Load a file, and then execute some code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'File is loaded'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Load a file after another file was loaded:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/file.js'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/anotherfile.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Load a plugin, and then set the correct settings for it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	Tag.<span style="color: #660066;">core</span>.<span style="color: #660066;">loadJS</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/path/to/myPlugin.js'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Tag.<span style="color: #660066;">myPlugin</span>.<span style="color: #660066;">setConfig</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			url<span style="color: #339933;">:</span><span style="color: #3366CC;">'http://www.example.com/'</span><span style="color: #339933;">,</span>
			selectors<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
				exampleLink<span style="color: #339933;">:</span><span style="color: #3366CC;">'.example'</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The list could go on forever. All these examples are also valid for the &#8220;loadOnce&#8221; method.</p>
<p></p>
<h2>Next</h2>
<p>In the last part of this series, I&#8217;ll create my Pubsub plugin: an event dispatcher. For this, I will have to modify the base class a bit, as I want to make use of <a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection</a>. But that&#8217;s for tomorrow.</p>
<p>All code is still available on my Codaset account here: <a href="http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library">http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library</a></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/12/01/building-your-own-javascript-library-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building your own Javascript library (part 1)</title>
		<link>http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/</link>
		<comments>http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 18:39:56 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=89</guid>
		<description><![CDATA[Goal
There are two programming languages I like: PHP and Javascript. In this mini series, I will explore Javascript and jQuery, and build my own library/framework. I won&#8217;t re-invent the wheel here. I&#8217;ll just make a collection of methods and functionality I need, making use of some functionality in jQuery.
The goal of the first part of [...]]]></description>
			<content:encoded><![CDATA[<h2>Goal</h2>
<p>There are two programming languages I like: PHP and Javascript. In this mini series, I will explore Javascript and <a href="http://jquery.com/">jQuery</a>, and build my own library/framework. I won&#8217;t re-invent the wheel here. I&#8217;ll just make a collection of methods and functionality I need, making use of some functionality in jQuery.</p>
<p>The goal of the first part of my mission is to create a small, lightweight, self-contained, easily extensible base for my framework. Recently I created my own little company, called &#8220;The Analog Guy&#8221;. I&#8217;ll just call my library &#8220;TAG&#8221;.</p>
<p><span id="more-89"></span><br />
<h2>Creating the base</h2>
<p>One thing I have learned from using other libraries, is that I don&#8217;t like initializing an object in Javascript. I just want to include the script on my page, and it has to work out of the box. I&#8217;ll use a <a href="http://javascriptant.com/articles/30/javascript-library-techniques-tips-and-tricks">self-executing anonymous function</a> to encapsulate my class, initialize it, and add some functionality. We start with the blueprint of the self-executing anonymous function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// rest goes here</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>This is an anonymous function that will be executed immediately. As a parameter to this anonymous function, I give the jQuery object. This makes sure that inside my anonymous function, $ refers to the jQuery object. Even if on the rest of the page, another library is used that defines $. That&#8217;s called the <a href="http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3">scope</a> of the object.</p>
<p>So well, next thing I want to do, is reserve my own TAG namespace. So I&#8217;ll add a little code for that:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// rest goes here</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>If another script uses the &#8220;Tag&#8221; namespace in the global scope, then that might indicate that the user has accidentally included the tag.js file twice on the same page. I&#8217;ve made sure that it the namespace only gets defined just once.</p>
<p>In my goal, I said I was going to build this library on top of jQuery. I want to make sure that $ is defined, and that it is jQuery. I don&#8217;t want to support other libraries at this moment.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Now that I know that jQuery is available, the next thing I want to add, is extensibility. I want to keep the base lightweight: only add the functionality that is needed. All the other functionality, that is non-essential, should be added via plugins or extensions. This makes it possible:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Object FN will contain all plugins</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The extend method has two arguments: the name of the namespace the plugin wants to use, and then the plugin itself. First there is a check if that namespace is still available. If it is, the first thing I do, is add some basic functionality to the plugin, that will make life easier for the developer of that plugin. Don&#8217;t worry, we&#8217;ll see immediately where I add that core functionality. A next step is to inject the plugin into the base class. For this, I use the &#8220;this.fn&#8221; object, because that makes it easier to detect which plugins are loaded. As a &#8220;bonus&#8221;, I also inject that same plugin in the base namespace. That makes it more convenient to use the plugin. &#8220;Tag.fn.myPlugin&#8221; can be referred to as &#8220;Tag.myPlugin&#8221;. Lastly, if that plugin needs some automagical setup, it can define the &#8220;init&#8221; method, which will get triggered when loading the plugin.</p>
<p>This provides basic extensibility for my framework. The next method I need, is a utility method. I cannot place it on a plugin, because it is something basic: a method that recursively sets a value inside an object</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Object FN will contain all plugins</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Recursively set values in an object
             *
             * Method allows to set individual settings, without overwriting
             * existing other values in the settings.
             *
             * @param object Object - The object to modify
             * @param data Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  path.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                      <span style="color: #003366; font-weight: bold;">var</span> tmpObject <span style="color: #339933;">=</span> object<span style="color: #339933;">;</span>
                      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span>path.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                          tmpObject <span style="color: #339933;">=</span> tmpObject<span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                      <span style="color: #009900;">&#125;</span>
                      tmpObject<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span></pre></div></div>

<p>I know jQuery.extend() exists. I just don&#8217;t think it can do what I want: set some properties of an object, without touching other existing properties. The method &#8220;setObjectData&#8221; I just created will do just that. It recursively loops through a set of values, and sets these values on the given object. This method will be used later, for setting values in a plugin configuration.</p>
<p>For now, I&#8217;m happy with that functionality. I don&#8217;t have an immediate use for other functionality. So we can initialize our class now. Since this takes only 1 line of code, I&#8217;ll immediately add some more functionality.</p>
<p>There are certain methods, I want to be available in every plugin, without the plugin having to know the name of my base class. I think it&#8217;s a basic need of the plugins, to be as agnostic as possible about the rest of the world. The fewer the dependencies, the better. If I ever decide to rename my company, and I rename my base library. Then the plugin should still work with little or no effort. These methods are &#8220;setConfig&#8221;, to set values in a plugin config, and &#8220;toString&#8221;, to automagically determine the name of the plugin.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> Tag <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// We have a dependency on jQuery:</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $ <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">||</span> $ <span style="color: #339933;">!==</span> window.<span style="color: #660066;">jQuery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please load jQuery library first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Object FN will contain all plugins</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Basic extend functionality. Each new plugin should extend
             * the TAG library, and become part of its namespace
             *
             * @param namespace String - Namespace of the new plugin
             * @param obj Object - The new plugin
             *
             * @return void
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">extend</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #339933;">,</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">// extend each namespace with core functionality</span>
                        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// load the new plugin in the namespaces:</span>
                        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #006600; font-style: italic;">// initialize the new library if necessary</span>
                        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">namespace</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The namespace '&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003366; font-weight: bold;">namespace</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' is already taken...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Recursively set values in an object
             *
             * Method allows to set individual settings, without overwriting
             * existing other values in the settings.
             *
             * @param object Object - The object to modify
             * @param data Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  path.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                      <span style="color: #003366; font-weight: bold;">var</span> tmpObject <span style="color: #339933;">=</span> object<span style="color: #339933;">;</span>
                      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>l<span style="color: #339933;">=</span>path.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>l<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                          tmpObject <span style="color: #339933;">=</span> tmpObject<span style="color: #009900;">&#91;</span>path<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                      <span style="color: #009900;">&#125;</span>
                      tmpObject<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        window.<span style="color: #660066;">Tag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Tag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extFN</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">/**
             * Set the settings of a plugin
             *
             * @param settings Object - New values for settings
             *
             * @return Object - Current plugin for chainability
             */</span>
            setConfig<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>settings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">setObjectData</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">settings</span><span style="color: #339933;">,</span> settings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">/**
             * Automagically determines correct name of a plugin
             *
             * @return String
             */</span>
            toString<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">Tag</span>.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'Tag.'</span> <span style="color: #339933;">+</span> k<span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Could not determine the name of the plugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s it for the moment. I&#8217;ll certainly need more functionality inside the base class, but I will build it was I need it.</p>
<p></p>
<h2>Usage</h2>
<p>Well, it&#8217;s a basic class really, there is no real usage for now. The strength will come from the plugins. To include the library in your files, simply include script tags which point to the correct JS file.</p>
<p></p>
<h2>Writing your own plugins</h2>
<p>Since I&#8217;m emphasizing plugins, I&#8217;ll give you the basic structure of how a plugin should be built. In future parts, I&#8217;ll build at least two plugins that will further extend the functionality: a &#8220;core&#8221; plugin, and a &#8220;pubsub&#8221; plugin. Here&#8217;s the template:</p>
<pre language="javascript">
(function(){
    var myPlugin = {
        /**
         * @var object settings - The configuration of this library
         */
        settings:{},

        /**
         * Will be called as soon as this library is loaded
         * into the main Tag library
         *
         * @return void
         */
        init:function(){},

        // other methods go below
    };
    Tag.extend("myPlugin",myPlugin);
})();
</pre>
<p>That&#8217;s all there is to it. Just an <a href="http://www.wait-till-i.com/2006/02/16/show-love-to-the-object-literal/">Object Literal notation</a> of an object, again inside a self-executing anonymous function. Include your JS file in your HTML, after you included the Tag library, and your plugin will be available as &#8220;Tag.myPlugin&#8221;.</p>
<p></p>
<h2>Next</h2>
<p>In the next part of this mini series, I&#8217;ll create the &#8220;core&#8221; plugin. This plugin will create functionality that is quite basic, but not essential to the library. Usually the methods will be utility methods which can be used by other plugins or regular Javascript code.</p>
<p>In the last part of the series, I&#8217;ll introduce my &#8220;pubsub&#8221; plugin. It&#8217;s a plugin that serves as a dispatcher for events. With it, it is possible to send out event notifications. Other parts of your code will be able to (un-)subscribe to events, and will get triggered when the event is fired.</p>
<p>If you have any remarks, please let me know. I&#8217;m open to suggestions and improvements. My code is available at <a href="http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library">http://www.codaset.com/theanalogguy/the-analog-guy-javascript-library</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/11/30/building-your-own-javascript-library-part-1/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Keeping your code DRY is also in the details</title>
		<link>http://www.encapsulated.org/blog/2009/11/27/keeping-your-code-dry-is-also-in-the-details/</link>
		<comments>http://www.encapsulated.org/blog/2009/11/27/keeping-your-code-dry-is-also-in-the-details/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 20:01:05 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[constants]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=86</guid>
		<description><![CDATA[What you should know already&#8230;
If you have been a developer for a while, chances are you&#8217;ll have come across the term DRY. It stands for Don&#8217;t Repeat Yourself, and it is actually a form of art. Keeping your code DRY ensures you have to think well before you start coding.  Instead of randomly throwing [...]]]></description>
			<content:encoded><![CDATA[<h1>What you should know already&#8230;</h1>
<p>If you have been a developer for a while, chances are you&#8217;ll have come across the term DRY. It stands for Don&#8217;t Repeat Yourself, and it is actually a form of art. Keeping your code DRY ensures you have to think well before you start coding.  Instead of randomly throwing code in your IDE, you should create the reflex of thinking how you can achieve what you want, without having to type too much code.</p>
<p>Not out of laziness, but because you&#8217;re smart. If you ever have to change your code, you&#8217;ll be glad you decided to write that particular bit only in that particular file. Sometimes it&#8217;s much easier to just copy/paste code from one class into another. But if you have to modify that piece of code, you&#8217;ll have to remember all the places that same code appears. Unless you kept it DRY of course.</p>
<p>Usually when an article talks about DRY, it will be in the context of Object Oriented Programming and model abstracts/inheritance/whatnot and making sure you don&#8217;t repeat the same code in different places. But the same principle can also be applied to other aspects of our code.</p>
<p><span id="more-86"></span></p>
<h1>Other aspects that can be kept DRY</h1>
<h2>Use class constants</h2>
<p>Not only does the DRY principle apply to OOP and methods in classes specificly. Other aspects of your classes should be kept DRY too. Recently, I&#8217;ve become a big fan of Class Constants in PHP. I use them to replace almost anything that is prone to change. I&#8217;ll just give you some examples:</p>
<p><strong>Status checking</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;initialized&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">&quot;initialized&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// do some stuff</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If it is decided that the status &#8220;initialized&#8221; should be renamed to &#8220;init&#8221;, then I have to change it everywhere I used that string literally.<br />
Here&#8217;s how you could do it better:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">const</span> STATUS_INITIALIZED <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;initialized&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">STATUS_INITIALIZED</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> MyClass<span style="color: #339933;">::</span><span style="color: #004000;">STATUS_INITIALIZED</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// do some stuff</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If we now have to change &#8220;initialized&#8221; to &#8220;init&#8221;, then we only have to change the constant. All other code is left unchanged, and will still work. Now that is DRY.</p>
<p><strong>Database metadata</strong></p>
<p>In a database, the ENUM datatype is often used to restrict the content of a field to a specific set of data it can contain. Excellent examples are the status of the previous example, flags, &#8230; Basically, anything you want to have full control of what could be a possible entry in that field. Keep these values also in Class Constants. If you change the database, then the only other manual thing you have to do, is update the associated constant.</p>
<p><strong>Configuration</strong></p>
<p>Since I use the <a href="http://framework.zend.com">Zend Framework</a> on a daily basis, here&#8217;s an example of how Zend Framework uses constants. First let&#8217;s see how it should not be done:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$translate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Translate<span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'gettext'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'my_it.mo'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'auto'</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'scan'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Instead, they use constants:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$translate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Translate<span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'gettext'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'my_it.mo'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'auto'</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'scan'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> Zend_Translate<span style="color: #339933;">::</span><span style="color: #004000;">LOCALE_FILENAME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Keep your templates DRY</h2>
<p>Not only is it important to keep your business logic DRY, the templates you write should benefit from the same principle. For your website basic, you won&#8217;t rewrite the entire HTML structure for each page on your site. Even if you don&#8217;t use PHP in an OOP way, you will probably still use includes to include parts on your site that will return on every page. Like the menu, or the header and footer. That&#8217;s DRY. But there&#8217;s more.</p>
<p><strong>Use partials (in partials)</strong></p>
<p>Partials is a term from Zend Framework. They are small templates that can be reused anywhere in your larger templates. Say you have a community site, with a messaging feature. Users can send each other messages, users can receive site notices, friend invitations, &#8230; In the inbox of a user, according to the type of message, a different partial can be shown:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$messagesList</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>partial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/inbox_'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$message</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>__toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.phtml'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For each messagetype that can be shown in the inbox, we create a new template. Inside that template, we do the markup for that specific message. Now in the inbox, we want to show the avatar and username of the person who sent you the message, or who invited you to become his friend. It is very tempting to write that part of the template once in inbox_message.phtml, and then copy/paste it in inbox_invitation.phtml and inbox_notice.phtml. It usually happens in a flurry of &#8220;yaaay it works !!&#8221;, and you&#8217;re so happy that you don&#8217;t care if your code becomes sloppy.</p>
<p>Of course, the avatar and username should be put in a new template. That new template is then used in each inbox_* template. Hence the partials inside partials. You also want to add the user ranking? No problem, only 1 file to update.</p>
<h1>Conclusion</h1>
<p>The DRY principle isn&#8217;t only reserved for the big chunks of your business logic. It is also an important part of the details. The small things that can easily lead to a search-and-replace rampage throughout your files. The templates are also often forgotten to be kept DRY.</p>
<p>As with many things: think before you act <img src='http://www.encapsulated.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/11/27/keeping-your-code-dry-is-also-in-the-details/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to let DomPDF and Zend Framework play along</title>
		<link>http://www.encapsulated.org/blog/2009/08/27/how-to-let-dompdf-and-zend-framework-play-along/</link>
		<comments>http://www.encapsulated.org/blog/2009/08/27/how-to-let-dompdf-and-zend-framework-play-along/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 18:06:28 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[autoload]]></category>
		<category><![CDATA[dompdf]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=77</guid>
		<description><![CDATA[For a recent project, I had to generate a PDF for a catalog. Since I&#8217;m using Zend Framework for my development, first thing that sprang in mind was Zend_Pdf. After some investigation, I found it too &#8220;difficult&#8221; to use. Flexible though that component is, I didn&#8217;t feel much for using coordinates to draw each and [...]]]></description>
			<content:encoded><![CDATA[<p>For a recent project, I had to generate a PDF for a catalog. Since I&#8217;m using Zend Framework for my development, first thing that sprang in mind was <a href="http://framework.zend.com/manual/en/zend.pdf.html">Zend_Pdf</a>. After some investigation, I found it too &#8220;difficult&#8221; to use. Flexible though that component is, I didn&#8217;t feel much for using coordinates to draw each and every line and text of that PDF. So I went to find another solution: <a href="http://www.digitaljunkies.ca/dompdf/">DomPDF</a>. DomPDF can generates PDF output, from HTML input. Exactly what I wanted and needed. It was however quite tricky to get it working. If you also have problems with this, read on!<br />
<span id="more-77"></span><br />
DomPDF isn&#8217;t part of the Zend Framework library, and as such doesn&#8217;t really follow the ZF conventions. That&#8217;s what caused me trouble: I couldn&#8217;t load the needed classes for it to work.</p>
<p>So how do you set everything up properly?</p>
<p><strong>First step: set include path</strong></p>
<p>When you&#8217;re setting your include paths, just add one for DomPDF.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span>APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/../../library/dompdf-0.5.1&quot;</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Second step:  make ZF &amp; DomPDF play along</strong></p>
<p>DomPDF has an autoloader which should take care of autoloading the classes for you. All you need to do, is make Zend Framework aware of that autoloader. The solution I found the most was this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'dompdf_config.inc.php'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">spl_autoload_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DOMPDF_autoload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>However this may not work if you use Zend Framework 1.8 or newer. The real solution I found on a forum, where <a href="http://weierophinney.net/matthew/">Matthew Weier O&#8217;Phinney</a> enlightened another soul with the same problem:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'dompdf_config.inc.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$autoloader</span> <span style="color: #339933;">=</span> Zend_Loader_Autoloader<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// assuming we're in a controller</span>
<span style="color: #000088;">$autoloader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pushAutoloader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DOMPDF_autoload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Final step: generate the PDF</strong></p>
<p>That&#8217;s the simplest one, as I found no problems with that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dompdf</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMPDF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_paper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a4&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;portrait&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load_html</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_base_path</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'DOCUMENT_ROOT'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;document.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There you go, this should set you up to get DomPDF up and running.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/08/27/how-to-let-dompdf-and-zend-framework-play-along/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Leveraging Zend_Auth for building your authentication</title>
		<link>http://www.encapsulated.org/blog/2009/05/07/leveraging-zend_auth-for-building-your-authentication/</link>
		<comments>http://www.encapsulated.org/blog/2009/05/07/leveraging-zend_auth-for-building-your-authentication/#comments</comments>
		<pubDate>Thu, 07 May 2009 21:06:09 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[codebase]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend_auth]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=63</guid>
		<description><![CDATA[In a series of posts, I will address the issue of authentication and authorization of users into your application. When you build a website with any form of back office, you will need to grant users access to the back office (authentication), and determine what actions they are allowed to take (authorization). The Zend Framework [...]]]></description>
			<content:encoded><![CDATA[<p>In a series of posts, I will address the issue of authentication and authorization of users into your application. When you build a website with any form of back office, you will need to grant users access to the back office (authentication), and determine what actions they are allowed to take (authorization). The Zend Framework has two tools just for that job: <a href="http://framework.zend.com/manual/en/zend.auth.html">Zend_Auth</a> and <a href="http://framework.zend.com/manual/en/zend.acl.html">Zend_ACL</a>. In this first part, I will build a custom User class, that will allow the programmer to perform a simple authentication of a user. This User class will be integrated into a so called &#8220;code base&#8221; or &#8220;framework&#8221; that you can use for your own applications. As of this moment, that framework doesn&#8217;t exist yet. I will gradually build it as my blog posts are added. Please look for the tag &#8220;<a href="http://www.encapsulated.org/blog/tag/codebase/">codebase</a>&#8221; if you want all of these posts.<br />
<span id="more-63"></span></p>
<p>First things first: if you have read my<a href="http://www.encapsulated.org/blog/2009/04/15/security-with-zend_amf-and-flex-part-2-practise/"> previous posts about securing your Flex &#8211; PHP services</a>, you will have seen that I have wrapped the bootstrapping process of that application in a class named &#8220;CB_Application&#8221;. That class is still under very heavy development. Since the release of Zend Framework 1.8, they have introduced <a href="http://framework.zend.com/manual/en/zend.application.html">Zend_Application</a>, just for that purpose. So it is very likely that in the future, I will replace my CB_Application class with something that makes use of Zend_Application. For now, I will focus on the other functionality, and leave CB_Application as it is, without bothering you with the details. But full disclosure will come, somewhere in the future.</p>
<p><strong>Structure</strong></p>
<p>I haven&#8217;t decided on some kind of structure for this framework yet. I think it will evolve as I add more classes and functionality to it. Below is a screenshot of how it currently looks like.</p>
<div id="attachment_67" class="wp-caption alignnone" style="width: 287px"><a href="http://www.encapsulated.org/blog/wp-content/uploads/2009/05/screenshot-documents-file-browser1.png"><img class="size-full wp-image-67" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/05/screenshot-documents-file-browser1.png" alt="Codebase Structure" width="277" height="208" /></a><p class="wp-caption-text">Codebase Structure</p></div>
<p>As you can see, quite empty. What we will be constructing during this blog post, is the CB_User class, the CB_Table_Users class and the CB_Auth_Adapter_Chap class. Once that is complete, you should include the Code Base folder into your include paths in your bootstrap. If that is done, you can just simple use your framework anywhere in your models or controllers.</p>
<p><strong>Database tables for the User class</strong></p>
<p>I&#8217;m going to concentrate on the authentication of the users, so I will keep the amount of data small. It will be the base for the User class, and will be extended with functionality and other data as the framework evolves. Right now, the only properties for a user we need, are: a username, a password, an e-mail address, and a flag to indicate if the user is active or not.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`cb_users`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`username`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`password`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`email`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`active`</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM  <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;</pre></td></tr></table></div>

<p>Small remark:All passwords are encrypted via the AES encryption method. I won&#8217;t use a hashing algorithm, because during the authentication process, I would like to know the exact password. In case I want to build PHP services for Flex, I will need that password, and I can&#8217;t come from the client directly. But that&#8217;s nothing to worry about now; just remember that the password will be encrypted via AES.</p>
<p>For testing purposes, we will add a dummy user via SQL now. Normally this will be done during some sort of registration process, but that&#8217;s for the future. Let&#8217;s add the user &#8220;test&#8221; with password &#8220;password&#8221; into the database. As key for the AES encryption, I have used &#8220;c0d3ba53&#8243;. You know, l33t speak as they say <img src='http://www.encapsulated.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  You should remember that key. I will hard code it into the code, as this is just a demonstration. In my real code, I have this key in a configuration file, and it is fetched by CB_Application. (I know, it&#8217;s irritating, but it will come up here an there during this blog post. Sorry!)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> cb_users <span style="color: #66cc66;">&#40;</span>username<span style="color: #66cc66;">,</span>password<span style="color: #66cc66;">,</span>email<span style="color: #66cc66;">,</span>active<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'test'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'S		í‹ôÚ×ýfÕ‘ˆ?	'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'test@example.com'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Building the authentication</strong></p>
<p>Authenticating a user will be very straightforward. There is only 1 condition: I want to be able to use CHAP and regular authentication. CHAP requires 3 parameters (username, challenge, signature) and regular authentication requires only 2 (username, password). I will incorporate that into 1 authentication function that will dynamically decide which authentication we will be doing, via the amount of parameters.</p>
<p>As I said before, the authentication process will make use of the Zend_Auth component of the Zend Framework. There are many different ways you could authenticate a user. Most commonly is against a database. Zend_Auth can accommodate to all these different ways, by making use of adapters. An auth adapter basically does the authentication, and Zend_Auth uses it. There are already some predefined adapters, but for the CHAP authentication, we will have to create our own.</p>
<p>To access all the data from the cb_users table in the database, We extend the regular Zend_Db_Table class with our own information:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> CB_Table_Users <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Abstract <span style="color: #009900;">&#123;</span>
	protected <span style="color: #000088;">$_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'cb_users'</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$_primary</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Nothing fancy, we just specify our table name, and the primary key, and then we&#8217;re good to go.</p>
<p>Before we can do the authentication, we first need to create our custom CHAP authentication adapter. The Zend Framework is designed to be extensible, and there is in interface for creating your own authentication adapters. The only method you need to implement is the authenticate() method. But you can add other methods if you deem that necessary. I will implement some setters for the credentials, and then a method that mimics the behaviour of the getResultRowObject() method from Zend_Auth_Adapter_DbTable. I added this extra functionality, because that will make it easier to process everything in the CB_User class. I have overdone myself documenting all the code, so I won&#8217;t explain it in depth anymore. If you are considering using this code, please replace all references to CB_application with your own code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * CB_Auth_Adapter_Chap class
 *
 * @author Tom Van Herreweghe
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CB_Auth_Adapter_Chap implements Zend_Auth_Adapter_Interface <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_signature</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_challenge</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_data</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set the username
     *
     * @param string $username
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUsername<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_username <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set the challenge, used to calculate the signature
     *
     * @param string $challenge
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setChallenge<span style="color: #009900;">&#40;</span><span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_challenge <span style="color: #339933;">=</span> <span style="color: #000088;">$challenge</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set the client-side calculated signature
     *
     * @param string $signature
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSignature<span style="color: #009900;">&#40;</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_signature <span style="color: #339933;">=</span> <span style="color: #000088;">$signature</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Authenticate the user
     *
     * @return Zend_Auth_Result
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> authenticate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// First, we need to retrieve the data, associated with the given username.</span>
        <span style="color: #666666; font-style: italic;">// The password will be decrypted for later use:</span>
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CB_Table_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTablePrefix</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cb&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;users&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #339933;">,</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDbAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteInto</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AES_DECRYPT(password,?) AS password&quot;</span><span style="color: #339933;">,</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username = ?&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_username<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;active = 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$authenticated</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// If we found a match, verify the other credentials</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// store the database data locally</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Compare calculated signature with given signature:</span>
            <span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_challenge<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// if the given signature and the calculated signature match, the user is authenticated:</span>
            <span style="color: #000088;">$authenticated</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_signature<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// We prefill the authentication result with a FAILURE</span>
        <span style="color: #000088;">$authResult</span> <span style="color: #339933;">=</span> Zend_Auth_Result<span style="color: #339933;">::</span><span style="color: #004000;">FAILURE_UNCATEGORIZED</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$authMessages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$authenticated</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// user is authenticated, overwrite the auth result:</span>
            <span style="color: #000088;">$authResult</span> <span style="color: #339933;">=</span> Zend_Auth_Result<span style="color: #339933;">::</span><span style="color: #004000;">SUCCESS</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Couldn't authenticate the user, set a message:</span>
            <span style="color: #000088;">$authMessages</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Login failed'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// return the result:</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Result<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$authResult</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_username<span style="color: #339933;">,</span> <span style="color: #000088;">$authMessages</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Mimics (actually almost copies) the behaviour of the getResultRowObjec() from Zend_Auth_Adapter_DbTable
     *
     * @param array $returnColumns
     * @param array $omitColumns
     * @return stdClass
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResultRowObject<span style="color: #009900;">&#40;</span><span style="color: #000088;">$returnColumns</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$omitColumns</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// If no data is set, return false:</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$returnObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$returnColumns</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Process only the specified columns:</span>
&nbsp;
            <span style="color: #000088;">$availableColumns</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$returnColumns</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$returnColumn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$returnColumn</span><span style="color: #339933;">,</span> <span style="color: #000088;">$availableColumns</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$returnObject</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$returnColumn</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#91;</span><span style="color: #000088;">$returnColumn</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$returnObject</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$omitColumns</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Process all columns, except the ones specified:</span>
&nbsp;
            <span style="color: #000088;">$omitColumns</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$omitColumns</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #b1b100;">as</span> <span style="color: #000088;">$resultColumn</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$resultValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultColumn</span><span style="color: #339933;">,</span> <span style="color: #000088;">$omitColumns</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$returnObject</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$resultColumn</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$resultValue</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$returnObject</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Process all columns, without restrictions:</span>
&nbsp;
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #b1b100;">as</span> <span style="color: #000088;">$resultColumn</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$resultValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$returnObject</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$resultColumn</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$resultValue</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$returnObject</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The puzzle is almost complete now. Let&#8217;s put the pieces together in the CB_User class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * CB_User class
 *
 * @author Tom Van Herreweghe
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CB_User <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Getter magical function to get properties
     *
     * @param string $key
     * @return mixed
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown property '<span style="color: #006699; font-weight: bold;">{$key}</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Setter magical function to set properties
     *
     * @param string $key
     * @param mixed $value
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$allowed</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$allowed</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Authenticate a user via credentials
     * The amount of parameters is variable, and are therefore not explicitly declared
     *
     * @return Zend_Auth_Result
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> authenticate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Get all the arguments:</span>
        <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Initialize an authAdapter, but set it to null</span>
        <span style="color: #000088;">$authAdapter</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// 2 arguments: We assume they are username &amp;amp; password. This means regular authentication</span>
            <span style="color: #000088;">$authAdapter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Adapter_DbTable<span style="color: #009900;">&#40;</span>
                CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDbAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// This returns an instance of a database adapter</span>
                CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTablePrefix</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// this will return a prefix, defined in a config.</span>
                <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'password'</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// set the necessary credentials</span>
            <span style="color: #000088;">$authAdapter</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredential</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentialTreatment</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AES_ENCRYPT(?, '&quot;</span><span style="color: #339933;">.</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;') AND active = 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">// CredentialTreatment says we'll use AES_Encrypt to encrypt the given password, and the user must be active.</span>
                <span style="color: #666666; font-style: italic;">// CB_application::getInstance()-&gt;getKey() returns a predefined key from a config file.</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// 3 arguments: We assume username, challenge and signature. This means custom digest authentication</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Here, we use our custom created CB_Auth_Adapter_DbTable:</span>
            <span style="color: #000088;">$authAdapter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CB_Auth_Adapter_Chap<span style="color: #009900;">&#40;</span>
                    CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDbAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                    CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTablePrefix</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">'password'</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// set the necessary credentials:</span>
            <span style="color: #000088;">$authAdapter</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUsername</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setChallenge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSignature</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$authAdapter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No authAdapter specified. Please check all the arguments&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Zend_Auth will now try to authenticate, using our auth adapter:</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$authAdapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">/*
             * When authentication is succesfull, we will write an instance of CB_User to the storage
             * Later on, when we want to know which user is authenticated, we will retrieve this CB_User from the storage
             * This all hapens quite transparently via regular Zend Framework code
             */</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// get the resulting line from the database, except the &quot;password&quot; column:</span>
            <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResultRowObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// retrieve an an instance of CB_User via the user id:</span>
            <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// write our CB_User object to the storage of Zend_Auth</span>
            <span style="color: #666666; font-style: italic;">// Zend_Auth::getInstance()-&gt;getIdentity() will return that CB_User object</span>
            Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStorage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// Return the Zend_Auth result:</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Return an instance of the CB_User class, via the user_id
     *
     * @param int $user_id
     * @return CB_User
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CB_Table_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTablePrefix</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cb&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;users&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id = ?&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$user_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;active = 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No user found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Transform the raw data from the database into a CB_User object
     *
     * @param Zend_Db_Table_Row $data
     * @return CB_User
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> factory<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Zend_Db_Table_Row&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CB_User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span>       <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Wrong datatype for the CB_User::factory() method&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now that we have written our classes, we want to use them. Mind you, this is just a demonstration. We will first log the user in via the CHAP method, then we do a logout, and then we log the user in via the regular method.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Layout&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ViewRenderer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNoRender</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Verify if we aren't logged in yet</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Oops, we were still logged in from a previous demonstration..</span>
            <span style="color: #666666; font-style: italic;">// Let's log out:</span>
            Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// Alright, at this point we're not logged in.. Let's proceed:</span>
&nbsp;
        <span style="color: #000088;">$challenge</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'challenge'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &lt;- this should be a random string</span>
        <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Calculate the signature:</span>
        <span style="color: #000088;">$signature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Authenticate via CHAP:</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> CB_User<span style="color: #339933;">::</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test'</span><span style="color: #339933;">,</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Show the result</span>
        <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Logout again for another demonstration:</span>
        Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Lets try the other authentication method:</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> CB_User<span style="color: #339933;">::</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test'</span><span style="color: #339933;">,</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;demonstration is over !&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s it for now. We have completed a first part of a framework: create a User class, and allow a user to authenticate. Right now, we can&#8217;t do much with it yet, but it will come in handy. We still need a way of registering users, but most importantly, we have to determine what that user can do now that he&#8217;s logged in. I will write about that in another blog post about Zend_ACL.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/05/07/leveraging-zend_auth-for-building-your-authentication/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Security with Zend_AMF and Flex &#8211; Part 2: Practise</title>
		<link>http://www.encapsulated.org/blog/2009/04/15/security-with-zend_amf-and-flex-part-2-practise/</link>
		<comments>http://www.encapsulated.org/blog/2009/04/15/security-with-zend_amf-and-flex-part-2-practise/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 21:58:45 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[air]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend_amf]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=31</guid>
		<description><![CDATA[In my previous post &#8220;Security with Zend_AMF and Flex &#8211; Part 1: Theory&#8220;, I explained the theory behind securing your Flex-PHP calls. After the theory comes the practise. I will only provide snippets for the PHP side of this story, as I&#8217;m totally ignorant about Flex and ActionScript. I used Zend_AMF, written by Wade Arnold, [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post &#8220;<a title="Security with Zend_AMF and Flex - Part 1: Theory" href="http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/">Security with Zend_AMF and Flex &#8211; Part 1: Theory</a>&#8220;, I explained the theory behind securing your Flex-PHP calls. After the theory comes the practise. I will only provide snippets for the PHP side of this story, as I&#8217;m totally ignorant about Flex and ActionScript. I used Zend_AMF, written by <a title="Wade Arnold" href="http://wadearnold.com/blog/">Wade Arnold</a>, to handle all the communications between Flex and PHP. If you need to know the basics, please <a href="http://framework.zend.com/manual/en/zend.amf.html">read the documentation</a> first.<br />
<span id="more-31"></span></p>
<p><strong>Project structure</strong></p>
<p>For the project, I used a slightly modified <a title="Application layout" href="http://framework.zend.com/wiki/display/ZFDEV/Choosing+Your+Application%27s+Directory+Layout#ChoosingYourApplication%27sDirectoryLayout-ConventionalModular">Conventional Modular</a> layout. The basics remains the same, but I have added support for so called versions of the application. The layout I maintain is not really important for the project. However, at the end of the article, you&#8217;ll be able to download everything, and I thought it necessary to explain the structure first. All code is part of the default module, and all classes are located in the &#8220;models&#8221; directory.</p>
<div id="attachment_51" class="wp-caption alignnone" style="width: 486px"><img class="size-full wp-image-51" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/screenshot.png" alt="Application layout" width="476" height="729" /><p class="wp-caption-text">Application layout</p></div>
<p><strong>The remoting endpoint</strong></p>
<p>Flex and AIR need to connect to an endpoint. A URL you provide, the point of access to your API. I have created only 1 endpoint. Ideally, you should make 2 endpoints: one for logging in, and one for the rest of the calls. That way, in the future, you could do the login process via an SSL secured line. The remoting endpoint is located in the <em>IndexController.php</em>, since that file is the door to the outside world for the project.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #666666; font-style: italic;">// Explicitly loading dependencies</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Proxies_AMF_System&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Proxies_AMF_Login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AMF_VO_Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AMF_VO_Message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action<span style="color: #009900;">&#123;</span>
		<span style="color: #009933; font-style: italic;">/**
		 * Everything to handle the rest of the API requests
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Layout&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ViewRenderer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNoRender</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$server</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Amf_Server<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;../models/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setClassMap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MessageVO&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;AMF_VO_Message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setClassMap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ErrorVO&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;AMF_VO_Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProduction</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$response</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Basically, the indexAction is where my endpoints is. There I set up the Zend_AMF server. After setting it up, the server is ready to listen to requests. All requests are handled by the classes from the folder specified with <em>addDirectory()</em>. In our case, the Proxy classes will handle all the requests. After specifying the directory of the classes, we set up the class mapping. Here, we tell Zend_AMF, how the mapping should be between our PHP classes on the server side, and the Flex classes on the client side. In my case, all Flex classes end with &#8220;VO&#8221;. Once the mapping is complete, our server is ready to give a response to the client. Note that I explicitly load every class in IndexController.php. For some reason, Zend_AMF isn&#8217;t able to use autoloading, which is a too bad.</p>
<p><strong>Responses to the client</strong></p>
<p>To have a cleaner interface, I have opted to return structured responses to the Flex client. When the client makes a call, there are only 2 kind of responses he can expect. When the call is successful, the response will be an object of the type <em>AMF_VO_Message</em> that&#8217;s being mapped to a <em>MessageVO</em> in Flex. Or it will be an object of the type <em>AMF_VO_Error</em>, mapped to <em>ErrorVO</em>, when an exception is thrown. This is very easy for the client side coder. All other data is encapsulated in the <em>AMF_VO_Message</em> object, and that makes it easy to process everything. It&#8217;s also easier for the PHP developer, since it is now possible to return a lot more information to the client. If you decide to use caching, then the <em>AMF_VO_Message</em> response can contain information about that; timestamps; transaction id&#8217;s; &#8230; Anything you would expect an API to return.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #009933; font-style: italic;">/**
     * This class is used to wrap data for output to the AMF adapter
     *
     */</span>
    <span style="color: #000000; font-weight: bold;">class</span> AMF_VO_Message <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Properties:</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$status</span>		<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$timestamp</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$transaction</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$data</span>		<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// mixed</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cachehit</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// boolean</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The class is basically a collection of public properties, where I can add data that is useful for the client, like timestamps, transaction-id&#8217;s, &#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #009933; font-style: italic;">/**
     * This class is used to wrap data for output to the AMF adapter
     *
     */</span>
    <span style="color: #000000; font-weight: bold;">class</span> AMF_VO_Error <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Properties:</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$timestamp</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$errorCode</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$file</span>		<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$linenumber</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// integer</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$messages</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Again, some properties that are useful for error tracking.</p>
<p><strong>First phase: login security</strong></p>
<p>In the first part of this series, I explained how securing the login process works. To initialize a login procedure, the client first needs to notify the server that an authentication attempt will be made. This is done by means of the<em> challenge()</em> function from the <em>Proxies_AMF_Login</em> class. Remember, all available calls are exposed via my so called Proxy classes. This is not mandatory, but a convention I want to impose on the users of the API I&#8217;m going to write. In Flex, the front-end developer can specify the class (proxy) and method (API call) to invoke.</p>
<p>When a client request a challenge from the server, I don&#8217;t necessarily want to know. Therefore, I can keep this function clean and simple. All code is explained with in-line comments.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Login proxy class to expose classes to AMF
 *
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Proxies_AMF_Login <span style="color: #000000; font-weight: bold;">extends</span> Proxies_AMF <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/**
         * Start the authentication session, by requesting a challenge.
         * The client then uses this challenge to calculate a signature (or digest)
         * When signing on, the username &amp;amp; signature is used, instead of the password
         *
         * @return AMF_VO_Message
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> challenge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$arguments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">processRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span><span style="color: #000088;">$arguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        protected <span style="color: #000000; font-weight: bold;">function</span> _challenge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// 1. generate the challenge</span>
            <span style="color: #000088;">$challengeString</span> <span style="color: #339933;">=</span> AMF_Login<span style="color: #339933;">::</span><span style="color: #004000;">generateChallenge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// 2. remember the challenge !</span>
            <span style="color: #000088;">$amfNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AMF_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">challenge</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$challengeString</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// 3. return the challenge to the client</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$challengeString</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/**
         * Authenticate the user
         *
         * @param string $username
         * @param string $signature
         * @return bool|AMF_VO_Error
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> signOn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$arguments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">processRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span><span style="color: #000088;">$arguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> _signOn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// 1. Digg up the challenge from our memory:</span>
            <span style="color: #000088;">$amfNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AMF_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$challenge</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">challenge</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// 2. Verify if we can authenticate the user</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>AMF_Login<span style="color: #339933;">::</span><span style="color: #004000;">signOn</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            	<span style="color: #666666; font-style: italic;">// User authenticated, let's forget the challenge</span>
                <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            	<span style="color: #666666; font-style: italic;">// Failed to authenticate the user</span>
            	<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not authenticate the user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A <em>challenge()</em> call is logically followed by a <em>signOn()</em> request (<em>verifySignature()</em> will be explained later on) to complete the login process. As you can see, for both <em>challenge()</em> and <em>signOn()</em>, there is a protected counterpart <em>_challenge()</em> and <em>_signOn()</em>. This has to do with the <em>processRequest()</em> call in both of the methods. More about that call later on, but for now, you only need to know that <em>processRequest()</em> does some administrative tasks, and wraps the responses for Zend_AMF server in a structured response.</p>
<p>An attentive reader will have noticed that I make use of an AMF_Login class, and I forgot to explain what and how. No wories, it&#8217;s not very mystical. I have just created that class to bundle some functionality. However, it does use some functionality from classes I have not written myself. They fall under the copyright of my employer, and I don&#8217;t want to infringe on that. So I won&#8217;t show you how AMF_User is written, but from the comments, it should be obvious what the purpose is. In the downloadable source, you&#8217;ll find a simplified version of AMF_User, which doesn&#8217;t really do anything, except illustrating the purpose.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AMF_Login <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Returns a random string, that is used as Challenge for the sign-on
	 * procedure
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> generateChallenge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> signOn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AMF_User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span> <span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Method to verify if each signature for an API call is valid
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> verifySignature<span style="color: #009900;">&#40;</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// 1. get the password from the session (it was put there during authentication)</span>
		<span style="color: #000088;">$amfNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AMF_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 2. calculate signature with given $challenge</span>
		<span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 3. compare calculated signature, with given signature</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Incorrect signature given&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This concludes the first part of the security measures: logging in. By now, a user can be logged in, and stay logged in, by means of the browser session.</p>
<p><strong>Second phase: API call security</strong></p>
<p>In the second phase, I want to verify all API calls. What we need, is a way to process every API call, and do a number of actions for each of those call. I want to be able to:</p>
<ul>
<li>Log what calls were made, by what user, what arguments, and what was the response</li>
<li>Have the possibility to cache certain calls</li>
<li>Use Zend_ACL to verify if a certain user has access to certain functionality</li>
<li>Create my structured response</li>
<li>Trap errors (yes, they should be trapped in the <em>ErrorController.php</em>, but I haven&#8217;t fully investigated that option yet)</li>
<li>Write code once, use it multiple times</li>
</ul>
<p>As you can see, there are plenty of requirements. There is one option to fulfil them all: Make sure all calls invoke 1 function that handles all the &#8220;administration&#8221;. The perfect solution exists, and is named <em>__call()</em> in PHP5. If a method does not exist in a class, then the <em>__call()</em> magic function is called with the exact same arguments. There, you can create all functionality for the administration, and then request another function that will do the real work. I know, quite the abstract explanation, but I won&#8217;t go any further on that road because using <em>__call()</em> fails. Zend_AMF uses Zend_Reflection, a <a title="PHP Reflection" href="http://us3.php.net/reflection">PHP reflection class</a>. Simply put, a reflection calls detects all publicly accessible methods and properties of a given class. Since <em>__call()</em> is a magic function, it isn&#8217;t publicly accessible. Hence, it won&#8217;t be discovered by Zend_Reflection, and consequently, Zend_AMF will not be able to make use of your functionality if you rely on <em>__call()</em>. That took me a while to discover, but if you think about it, it&#8217;s quite logical.</p>
<p>To work around this problem, I have decided to create my own <em>__call()</em> method, and I have named it <em>processRequest()</em>. This will do the exact same as I have explained in the paragraph above: handle the administration (logging, caching, &#8230;). The diagram below will immediately shine a light on the working.</p>
<div id="attachment_49" class="wp-caption alignnone" style="width: 398px"><img class="size-full wp-image-49" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/diagram12.png" alt="Diagram: handle requests" width="388" height="102" /><p class="wp-caption-text">Diagram: handle requests</p></div>
<p>A request comes in via <em>helloWorld()</em>. It then travels to <em>processRequest()</em> for the administrative tasks. Next it travels to<em> _helloWorld()</em> that will do what needs to be done and give a response. The response then will travel back from <em>_helloWorld()</em> to <em>processRequest()</em>. There, the response is packed in the structured response, and then that structured response is sent back to <em>helloWorld()</em>. There, it is delivered to the Zend_AMF server, and ultimately, to the Flex client.</p>
<p>To write the code only once and use it many times, the only option is to place the <em>processRequest()</em> function in the parent of our Proxy classes: <em>Proxies_AMF.php</em>. Below, you will find a simplified version of the code I have written for a recent project. Everything is explained in in-line comments.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * AMF proxy class to expose classes to AMF
 *
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Proxies_AMF <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Some classes do not need verification, like Login. Only the other API calls should be verified</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$exemptVerification</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Proxies_AMF_Login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> processRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span><span style="color: #000088;">$arrArguments</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		try <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$className</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Retrieve the parameters to verify the identiy of the requesting party:</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exemptVerification</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$challenge</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arrArguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$signature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arrArguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Verify the given signature, before executing the call, unless the request is to a method from an exempt class:</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exemptVerification</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> AMF_Login<span style="color: #339933;">::</span><span style="color: #004000;">verifySignature</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// Verify if the requested method actually exists:</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;_<span style="color: #006699; font-weight: bold;">{$method}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$cachehit</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// support for caching could be added, but not now.</span>
					<span style="color: #000088;">$returnValue</span> <span style="color: #339933;">=</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;_<span style="color: #006699; font-weight: bold;">{$method}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$arrArguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">// wrap the response of the API call in an AMF_VO_Message object, and return it:</span>
					<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wrapOutput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$returnValue</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #339933;">,</span><span style="color: #000088;">$cachehit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
					throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The method '<span style="color: #006699; font-weight: bold;">{$method}</span>' does not exist.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// An exception is already thrown in AMF::Login::verifySignature()</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$errorVO</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AMF_VO_Error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Get the currently logged in user</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timestamp</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y-%m-<span style="color: #009933; font-weight: bold;">%d</span> %H:%M:%S&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorCode</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// usually, this will be &quot;Exception&quot;, unless we introduce custom Exceptions</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file</span>			<span style="color: #339933;">=</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFile</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">linenumber</span>		<span style="color: #339933;">=</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">messages</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stacktrace</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">debug_backtrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// return response</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$errorVO</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> wrapOutput<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #000088;">$transaction</span><span style="color: #339933;">,</span><span style="color: #000088;">$cachehit</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$message</span>				<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AMF_VO_Message<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">status</span>		<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timestamp</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y-%m-<span style="color: #009933; font-weight: bold;">%d</span> %H:%M:%S&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">transaction</span>	<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$transaction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$transaction</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;no_transaction&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span>		<span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cachehit</span>		<span style="color: #339933;">=</span> <span style="color: #000088;">$cachehit</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The function <em>processRequest()</em> is now very basic. It only incorporates authentication of the requester, and catching errors. I will not implement the rest of the requirements during this article, since that would lead me outside the scope of this article. If you feel like implementing extra functionality for all your calls, then <em>processRequest()</em> is the method to elaborate on. For a business project, I have extended it with everything mentioned in my list earlier.</p>
<p>The hard part is over now. Most of the functionality has been written. Each API call will be accompanied by 2 extra parameters: a client-chosen challenge, and a calculated signature. The given challenge and signature are just the first two parameters of each of our service calls. These parameters will be verified in <em>processRequest()</em>.</p>
<p>The actual verification is done by the <em>verifySignature() </em>method in AMF_Login class. To make it easy, below is a copy of that method again. The parameters are the challenge, chosen by the client, and the signature, calculated by the client. The method will then use that same challenge, and the password stored in a session object, and then calculate the accompanying signature. If both signatures match, then we have a winner! If they don&#8217;t match, then somebody is trying to tamper with the requests. It&#8217;s up to you how you deal with that. I just output an AMF_VO_Error object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Method to verify if each signature for an API call is valid
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> verifySignature<span style="color: #009900;">&#40;</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// 1. get the password from the session (it was put there during authentication)</span>
	<span style="color: #000088;">$amfNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AMF_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 2. calculate signature with given $challenge</span>
	<span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 3. compare calculated signature, with given signature</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Incorrect signature given&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>With this last functionality explained, I think I have everything covered. Only thing left, is giving you an example on how our processRequest() method is used:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * System proxy class to expose classes to AMF
 *
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Proxies_AMF_System <span style="color: #000000; font-weight: bold;">extends</span> Proxies_AMF <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Say hello to the world
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> helloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">processRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> _helloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Hello World !!&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I mentioned the <em>helloWorld()</em> function earlier, and here is the implementation. The Flex client calls the <em>helloWorld()</em> function (without the underscore !!), and gives as paremeters the challenge and signature. You&#8217;ll see that those parameters aren&#8217;t explicitly mentioned in the function definition, but that is no problem. In that function, <em>processRequest()</em> is called, which will do the verificiation and then call<em> _helloWorld()</em>. The response from <em>_helloWorld()</em> will then travel back, over <em>processRequest()</em>, via <em>helloWorld()</em>, to Zend_AMF, stopping at the Flex client.</p>
<p><strong>Ammendments</strong></p>
<p>This article is the fruit of a quite lengthy development process at the company I work at. During this period, I have made many mistakes regarding Zend_AMF, and learned from them. To illustrate that development is a continuous learning process, I will end with a &#8220;mistake&#8221; I just recently discovered:</p>
<p>When I first implemented these security measures, I used the URL to transport the challenge and signature for each API call. This way, could keep my interfaces clean, without having to take into account the challenge and signature. In Flex, I had the front-end developer create a dynamical endpoint for each call. Each endpoint connection only made one call, since in the URL, the challenge and signature were added. However, when an endpoint connection is established, it is standard procedure of AMF to ping the server once. As a result, each call generated a new ping request, effectively doubling the amount of calls. I don&#8217;t have to explain that this is quite a big overhead. To overcome this, I had to extend each API call to accept the challenge and signature as a parameter of that call. And the result of my mistake is that you can enjoy a &#8220;better&#8221; implementation than what I had 2 days ago.</p>
<p>What I want to say: This method is not fool proof, and I have no doubt that there are probably many ways of improving the security. If you happen to have suggestions, by all means, let me know. I&#8217;m happy to learn from you.</p>
<p><strong>Download all files</strong>: <a href="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/amfsecurity.zip">zip</a> | <a href="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/amfsecurity.tar.gz">tar</a><br />
<strong>Credits</strong> where credits are due: Thanks <a href="http://www.vicr.be/blog/">Vic Rau</a> for the small <a href="http://www.encapsulated.org/dev/flex/application/v1/public/images/UnitTesting.html">UnitTest</a> (yes, it is also in the downloadable archive)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/04/15/security-with-zend_amf-and-flex-part-2-practise/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Security with Zend_AMF and Flex &#8211; Part 1: Theory</title>
		<link>http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/</link>
		<comments>http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 18:24:46 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[chap]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=24</guid>
		<description><![CDATA[In a series of two posts, I will explain how to secure the communication between a Flex client and PHP server architecture. The first part will explain how I envision that security, and in the second part I will show snippets of PHP code for the practical implementation.
I&#8217;m currently working together with my friend and [...]]]></description>
			<content:encoded><![CDATA[<p>In a series of two posts, I will explain how to secure the communication between a Flex client and PHP server architecture. The first part will explain how I envision that security, and in the second part I will show snippets of PHP code for the practical implementation.</p>
<p>I&#8217;m currently working together with my friend and colleague <a title="VICR" href="http://www.vicr.be">Vic</a> on a client-server application that involves <a title="Flex" href="http://www.adobe.com/products/flex/">Flex</a> on the front-side, and PHP (<a href="http://zendframework.com/">Zend Framework</a>) on the back. Since I&#8217;m the PHP guy, I&#8217;m in charge of creating the API for his Flex application. For the moment, the project will only be accessed locally from the client&#8217;s network. But there is a possibility that in a later stage, it might open up to the general public. One of my main concerns was how we could make every API call as secure as possible. This without making it too complicated, or involve too many service calls that might slow everything down.<br />
<span id="more-24"></span></p>
<p><strong>First login&#8230;</strong></p>
<p>The first aspect of securing the application, is authentication of users. The application is accessible to multiple users, and those are grouped in various groups. Each group has different privileges. To identify every user, they are given usernames and passwords. My biggest concern with the login process, is that we didn&#8217;t have access to a secure connection via SSL. This means that every communication between the client and server software can be monitored by network sniffers. Flex uses the <a href="http://en.wikipedia.org/wiki/Action_Message_Format">AMF protocol</a> to communicate with Zend_AMF. Even though this is a binary stream, the specifications are open to everyone. So it is possible to intercept communications, and read everything out.</p>
<p>I wanted to make sure that during the login process, the password was not exposed to the outside world via the communications line. Because once the password and username are intercepted, then it would be easy to start making fake API calls to the server. The solution I had in mind, is called CHAP &#8211; <a title="CHAP protocol" href="http://en.wikipedia.org/wiki/Challenge-handshake_authentication_protocol">Challenge-Handshake Authentication Protocol</a>. Here&#8217;s a diagram about how this protocol works.</p>
<p><img class="aligncenter size-full wp-image-26" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/diagram1.png" alt="CHAP diagram" width="350" height="217" />The main purpose of using CHAP, is to authenticate users, without having to transmit the password between the client and the server. To accomplish this, we have to replace the password with something else, but still allowing the server to recognize the correct user, and log him in. I call this password replacer the &#8220;signature&#8221;.</p>
<p>In the first phase of the diagram, the client requests a &#8220;challenge&#8221; from the server. A challenge is a key that will be used by both the client and the server, to create the signature. During the first phase, the client asks the server what key will be used to encrypt the password. It is important that both the client and the server use the exact same challenge.</p>
<p>During the second phase, an authentication request is being transmitted. Instead of using the username and password, we use the username and signature. The final signature is calculated like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$signature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">MD5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">MD5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The client calculates this signature, and sends it to the server. The server then looks up the given username in the list of possible users. The corresponding password is then used by the server, to also calculate a signature, with the earlier given challenge. The outcome of that comparison is given during the third and last phase of the CHAP authentication.</p>
<p>This is not a very difficult protocol to implement, but according to me, it still provides a reasonable amount of security during the login process.</p>
<p><strong>&#8230; and then the rest of the calls.</strong></p>
<p>Now that a user is logged in, the most important stuff still has to happen: interactivity with the server by calling API methods. Once the user is authenticated, he remains authenticated by using the browser session. So now we have to keep in mind that other people might try to steal the session, and invoke undesired API calls. During each call, the server needs to be sure that it was still the same client making the call, and not some <a title="Man-in-the-middle attack" href="http://en.wikipedia.org/wiki/Man-in-the-middle_attack">man-in-the-middle</a>.</p>
<p>For this, I decided to elaborate on the CHAP protocol, used for authentication. How about we could keep the system, where the client signs its calls with a signature. For obvious reasons, it is not possible to recycle the signature from the login procedure. But I wanted to force the client to generate a new signature for each API request. There are still two variables to generate the signature: the password and the challenge.</p>
<p>It was not a practical option to ask the user&#8217;s password for each and every API call. Instead, once the user was authenticated, I had the Flex application remember the password in its memory. This way, it could be recycled to generate new signatures.</p>
<p>The only problem was the challenge. A first option was to have the client request a challenge from the server. But this would mean that for each regular API call, there would be an additional call for a challenge. That would double the amount of API calls. I was afraid that in the long run, this would generate too much calls for the server. That&#8217;s why I chose a second option: have the client generate the challenge and transmit it alongside the signature. The server would then use this challenge, and the password from the session, to calculate the signature. If they matched, then the call was permitted to be executed.</p>
<p>As far as I could see, this wouldn&#8217;t impose a bigger security risk. Of course, a man-in-the-middle could generate fake challenges. But he still wouldn&#8217;t be able to generate fake signatures. For that, he still needs the correct password. Once he has that, then well&#8230; all security checks fail. The responsibility of keeping the password secret lies entirely at the client now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Garbage collector problem after foreach loop ?</title>
		<link>http://www.encapsulated.org/blog/2009/04/01/garbage-collector-problem-after-foreach-loop/</link>
		<comments>http://www.encapsulated.org/blog/2009/04/01/garbage-collector-problem-after-foreach-loop/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 09:45:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[by-reference]]></category>
		<category><![CDATA[foreach]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=3</guid>
		<description><![CDATA[In PHP5, it is possible to loop through an array, and alter each item on the fly via the &#8220;by-reference&#8221; operator: the &#38;-symbol. However, you should be on the lookout for unexpected behaviour.

First the initial data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	$items = array&#40;
		array&#40;
			&#34;field&#34;		=&#62; &#34;field1&#34;,
			&#34;value&#34;		=&#62; &#34;value1&#34;,
			&#34;options&#34;	=&#62; array&#40;
				&#34;operator&#34;	=&#62; &#34;=&#34;,
				&#34;logic&#34;		=&#62; &#34;OR&#34;
			&#41;
		&#41;,
		array&#40;
			&#34;field&#34;		=&#62; &#34;field2&#34;,
			&#34;value&#34;		=&#62; &#34;value2&#34;,
			&#34;options&#34;	=&#62; array&#40;
				&#34;operator&#34;	=&#62; &#34;=&#34;,
				&#34;logic&#34;		=&#62; &#34;OR&#34;
			&#41;
		&#41;,
		array&#40;
			&#34;field&#34;		=&#62; &#34;field3&#34;,
			&#34;value&#34;		=&#62; &#34;value3&#34;,
			&#34;options&#34;	=&#62; array&#40;
				&#34;operator&#34;	=&#62; &#34;=&#34;,
				&#34;logic&#34;		=&#62; &#34;OR&#34;
			&#41;
		&#41;,
		array&#40;
			&#34;field&#34;		=&#62; &#34;field4&#34;,
			&#34;value&#34;		=&#62; &#34;value4&#34;,
			&#34;options&#34;	=&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>In PHP5, it is possible to loop through an array, and alter each item on the fly via the &#8220;by-reference&#8221; operator: the &amp;-symbol. However, you should be on the lookout for unexpected behaviour.<br />
<span id="more-3"></span><br />
First the initial data:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;field&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;field1&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;value&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value1&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;options&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;operator&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;logic&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;OR&quot;</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;field&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;field2&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;value&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value2&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;options&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;operator&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;logic&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;OR&quot;</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;field&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;field3&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;value&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value3&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;options&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;operator&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;logic&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;OR&quot;</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;field&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;field4&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;value&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value4&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;options&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;operator&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;logic&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;OR&quot;</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It is a simple array. It could be an array of where-clauses you wish to define for your own search module (like this example), or it could just simply be test data for a unit test.</p>
<p>In my case, this data comes into a function, and in that function I want to strip some of the data. The &#8220;logic&#8221; index under &#8220;options&#8221; is deprecated, and I want to remove it. To make my code compact, I have decided to alter each filter on the fly, by using the by-reference operator.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;options&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;logic&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This results in the following altered version of my initial data:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field1
            <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value1
            <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
                <span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field2
            <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value2
            <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
                <span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field3
            <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value3
            <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
                <span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field4
            <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value4
            <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
                <span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>As you can see, I have successfully removed the &#8220;logic&#8221; index by using an ampersand in my foreach loop. This is potentially very useful functionality. It&#8217;s very easy to manipulate your data like this. No more fiddling around with indexes, or even create temporary arrays to house your data. Just edit it on the fly. There is however a (big) flaw, and I&#8217;ll illustrate it below.</p>
<p>In the previous loop, I have altered my array, and now I want to loop through it again, and just output each item in the array. Nothing more. But things go awry quite unexpectantly.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Item <span style="color: #006699; font-weight: bold;">{$counter}</span>: &quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$counter</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The output of this loop is:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Item <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field1
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value1
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span>
&nbsp;
Item <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field2
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value2
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span>
&nbsp;
Item <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field3
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value3
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span>
&nbsp;
Item <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field3
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value3
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The last output isn&#8217;t what I expected it to be. As you can see, it is the same output as the previous item &#8220;3&#8243;.</p>
<p>I have encountered this problem in production code, and only by rigorously outputting all my data, I have discovered that this was my problem. I am not a PHP expert by far. So I don&#8217;t always know how things technically work. My guess is that after a foreach loop, with the by-reference operator, the garbage collector doesn&#8217;t do its job well. But I&#8217;m not sure this is the correct explanation. I have found a workaround though. After the first foreach, where you use the &amp; symbol, you just unset that variable. Like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;options&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;logic&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now the output should be what you expect it to be. Just to be sure, here&#8217;s what I got:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// ...</span>
Item <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field4
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value4
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>If anyone has an idea to whether or not this is a bug, please let me know. I have so far not filed a bug (yet), as I feel I need to do a bit more research first. It could be something by-design, and then I&#8217;m making quite a fool of myself. I guess that&#8217;s just part of the learning process</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/04/01/garbage-collector-problem-after-foreach-loop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
