<?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>BinaryKitten&#039;s Development Dropbox &#187; Zend Framework</title>
	<atom:link href="http://binarykitten.com/category/dev/zend-framework/feed" rel="self" type="application/rss+xml" />
	<link>http://binarykitten.com</link>
	<description>Curently a work in progress. Please be patient</description>
	<lastBuildDate>Thu, 27 Oct 2011 21:49:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Royal Mail Postage by Weight &#8211; Zend Framework Module</title>
		<link>http://binarykitten.com/dev/zend-framework/354-royal-mail-postage-by-weight-zend-framework-module.html</link>
		<comments>http://binarykitten.com/dev/zend-framework/354-royal-mail-postage-by-weight-zend-framework-module.html#comments</comments>
		<pubDate>Sun, 17 Jul 2011 23:24:39 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Calculator]]></category>
		<category><![CDATA[Royal Mail]]></category>
		<category><![CDATA[Shipping]]></category>

		<guid isPermaLink="false">http://binarykitten.me.uk/?p=354</guid>
		<description><![CDATA[For a recent project I have had to calculate weight based shipping costs. Before I had this as a db table, but that became a mess to manage.

So I've created this small shipping calculator. It's set to use KG in weight and prices inclusive of vat taken from the Royal Mail's 2011 Prices.]]></description>
			<content:encoded><![CDATA[<p>For a recent project I have had to calculate weight based shipping costs. Before I had this as a db table, but that became a mess to manage.</p>
<p>So I&#8217;ve created this small shipping calculator. It&#8217;s set to use KG in weight and prices inclusive of vat taken from the Royal Mail&#8217;s 2011 Prices.</p>
<p>Follows are the config ini file, the class and a basic usage.</p>
<pre class="brush: php">
[EUR]
prices[0.01] =1.79
prices[0.12] =1.94
prices[0.14] =2.15
prices[0.16] =2.32
prices[0.18] =2.54
prices[0.2] =2.63
prices[0.22] =2.75
prices[0.24] =2.87
prices[0.26] =2.99
prices[0.28] =3.08
prices[0.3] =3.14
prices[0.32] =3.8
prices[0.42] =4.46
prices[0.52] =5.12
prices[0.62] =5.78
prices[0.72] =6.44
prices[0.82] =7.1
prices[0.92] =7.76
prices[1] =7.76

above.start = 1.1
above.increment = 0.1
above.valuePerIncrement = 0.55

[ITL]
prices[0.01] =2.49
prices[0.12] =2.79
prices[0.14] =3.12
prices[0.16] =3.48
prices[0.18] =3.84
prices[0.2] =4.2
prices[0.22] =4.56
prices[0.24] =4.76
prices[0.26] =4.88
prices[0.28] =5
prices[0.3] =5.12
prices[0.32] =6.45
prices[0.42] =7.78
prices[0.52] =9.11
prices[0.62] =10.44
prices[0.72] =11.78
prices[0.82] =13.11
prices[0.92] =14.44
prices[1] =14.44

above.start = 1.1
above.increment = 0.1
above.valuePerIncrement = 0.66

[UK]
prices[0.1] =1.9
prices[0.25] =2.36
prices[0.5] =2.98
prices[0.75] =3.66
prices[1] =4.46
prices[1.25] =5.88
prices[1.5] =6.8
prices[1.75] =7.71
prices[2] =8.62
prices[4] =10.74

above.start = 6
above.increment = 2
above.valuePerIncrement = 3.66
</pre>
<pre class="brush: php">
&lt;?php
class BinaryKitten_Shipping {
    private $_cfg;
    private $_curCfg;
    public function __construct($postalZone=null) {
        $this-&gt;_curCfg = $this-&gt;_cfg = new Zend_Config_Ini(APPLICATION_PATH .&#039;/configs/postal.ini&#039;);
        if (isset($postalZone)) {
            $this-&gt;setPostalZone($postalZone);
        }
    }
    public function setPostalZone($postalZone)
    {
        if (self::validatePostalZone($postalZone)) {
            $this-&gt;_curCfg = $this-&gt;_cfg-&gt;get($postalZone, $this-&gt;_cfg);
        }

    }
    public static function validatePostalZone($postalZone)
    {
        return in_array(strtoupper($postalZone), array(&#039;UK&#039;,&#039;EUR&#039;,&#039;ITL&#039;));
    }
    public function getPriceForWeight($weight)
    {
        $priceOut = -1;
        foreach($this-&gt;_curCfg-&gt;prices as $weightBoundary=&gt;$price) {
            if ($weightBoundary &lt; $weight) continue;
            if ($weightBoundary &gt;= $weight) {
                $priceOut = $price;
                break;
            }
        }
        if ($priceOut == -1) {
            for($weightBoundary = (float)$this-&gt;_curCfg-&gt;above-&gt;start;$weightBoundary &lt; $weight;$weightBoundary += $this-&gt;_curCfg-&gt;above-&gt;increment) {
                $price+=$this-&gt;_curCfg-&gt;above-&gt;valuePerIncrement;
            }

            $priceOut = $price;
            //try to calculate the difference
        }
        return (float)$priceOut;
    }
}
</pre>
<pre class="brush: php">
$prices = new BinaryKitten_Shipping();
$prices-&gt;setPostalZone(&#039;EUR&#039;);
$price = $prices-&gt;getPriceForWeight(5);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/zend-framework/354-royal-mail-postage-by-weight-zend-framework-module.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>TinyMCE and Zend_Auth &#8211; Simple Authentication checking.</title>
		<link>http://binarykitten.com/dev/zend-framework/332-tinymce-and-zend_auth-simple-authentication-checking.html</link>
		<comments>http://binarykitten.com/dev/zend-framework/332-tinymce-and-zend_auth-simple-authentication-checking.html#comments</comments>
		<pubDate>Fri, 11 Mar 2011 22:35:28 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://binarykitten.me.uk/?p=332</guid>
		<description><![CDATA[I&#8217;ve been using TinyMCE for a good long while and wanted to integrate their fantastic FileManager plugin and ImageManager plugin&#8217;s (Both Commercial) in with my ZF client site. This is relatively easy, the easiest way is to create a controller action and a iframe in the view that points to the location of the imagemanager [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using TinyMCE for a good long while and wanted to integrate their fantastic FileManager plugin and ImageManager plugin&#8217;s (Both Commercial) in with my ZF client site. This is relatively easy, the easiest way is to create a controller action and a iframe in the view that points to the location of the imagemanager or filemanager plugins.  eg<br />
<code><br />
&lt;iframe src="/js/tiny_mce/plugins/filemanager/index.php?type=fm"&gt;<br />
</code></p>
<p>This works well, but I wanted to make sure that the plugn was not accessible unless the user logged in. I already used Zend_Auth and this doesn&#8217;t exactly play well with the basic SessionAuthenticator. So I set out to create a basic simple plugin for TinyMCE that would allow it to work with the Zend_Auth System.</p>
<p><strong>- Creating the plugin -</strong><br />
first off we have to create our plugin and plugin folder within the tinymce plugin&#8217;s plugin folder. If you think that&#8217;s a lot of use of plugin, you can understand where my brain decided to up and leave.</p>
<p>1st locate your tinymce folder, in there you should find plugins and then go into the filemanager folder. Of course this assumes you have purchased and installed the filemanager plugin. Your path should be something like the following:<br />
<code>&lt;path_to_tiny_mce&gt;/plugins/filemanager/</code> &#8211; this is what we shall refer to our &#8220;Base Path&#8221; from now on.</p>
<p>now we are at a comfortable location to proceed, we can go ahead and create our folder and file<br />
<code>&lt;Base Path&gt;/plugins/ZendAuthenticator</code><br />
in here place a file name ZendAuthenticator.php containing this code:</p>
<pre class="brush: php">&lt;?php
/**
 * Use Zend_Auth to validate the logged in user.
 *
 * <a href="http://twitter.com/author">@author</a> BinaryKitten
 */
class BinaryKitten_ZendAuthenticator extends Moxiecode_ManagerPlugin {

    private $auth = null;
    public function  __construct($man)
    {
        $config = $man-&gt;getConfig();
        if (isset($config[&#039;ZendAuthenticator.LibraryPath&#039;]) &amp;&amp; !empty($config[&#039;ZendAuthenticator.LibraryPath&#039;])) {
            set_include_path(
                implode(
                    PATH_SEPARATOR, array(
                        realpath(
                        $config[&#039;ZendAuthenticator.LibraryPath&#039;]
                        ),
                        get_include_path(),
                    )
                )
            );
        }
        require &quot;Zend/Auth.php&quot;;
        $this-&gt;auth = Zend_Auth::getInstance();
    }

    //put your code here
    public function onAuthenticate(&amp;$man)
    {
        return $this-&gt;auth-&gt;hasIdentity();
    }
}

$man-&gt;registerPlugin(&quot;ZendAuthenticator&quot;, new BinaryKitten_ZendAuthenticator($man));
</pre>
<p>Voila the plugin is ready.</p>
<p><strong>- Configuring the plugin -</strong><br />
Now open up the config.php found in the &#8220;Base Path&#8221; mentioned earlier.<br />
locate: <code>$mcFileManagerConfig['authenticator']</code> and set it&#8217;s value to the following<br />
<code>$mcFileManagerConfig['authenticator'] = 'ZendAuthenticator';</code></p>
<p><strong>- Library Location -</strong></p>
<p>Finally, If your Zend_Framework is not located on the include path (like mine is) then you will need to tell the system where to find the library, to do this add the following to the config.php<br />
<code>$mcFileManagerConfig['ZendAuthenticator.LibraryPath'] = '
<path_to_library>';</code><br />
Please remember though that this is relative to the &#8220;Base Path&#8221; and not the Document_Root unless you specify it as such. </p>
<p><strong> &#8211; - &#8211; - &#8211; </strong><br />
The last thing I will leave you with is that this only checks to see if there is a stored session for Zend_Auth. I&#8217;ll leave it up to you if you want to get roles and other fancy stuff integrated.</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/zend-framework/332-tinymce-and-zend_auth-simple-authentication-checking.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Active Module Config V2</title>
		<link>http://binarykitten.com/dev/zend-framework/296-active-module-config-v2.html</link>
		<comments>http://binarykitten.com/dev/zend-framework/296-active-module-config-v2.html#comments</comments>
		<pubDate>Fri, 18 Jun 2010 00:55:26 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://binarykitten.me.uk/?p=296</guid>
		<description><![CDATA[A brand new version of my previously Released Front controller plugin for Zend Framework. Since the last version of the plugin, I&#8217;ve used and cleaned and tried to maintain it. It just ended up unwieldy. So I set about creating this new version. Does exactly the same as the last one, just should be a [...]]]></description>
			<content:encoded><![CDATA[<p>A brand new version of my previously Released Front controller plugin for Zend Framework.<br />
<span id="more-296"></span><br />
Since the last version of the plugin, I&#8217;ve used and cleaned and tried to maintain it. It just ended up unwieldy. So I set about creating this new version. Does exactly the same as the last one, just should be a lot easier to see what it&#8217;s doing and when.</p>
<p>So I present to you active Module Config V2</p>
<p>Many thanks to Paul Bouzakis (<a href="http://twitter.com/paul_eye">@paul_eye</a>) for the pointers in ways to make this more readable and concise</p>
<pre class="brush: php">
&lt;?php
class BinaryKitten_ModuleConfig extends Zend_Controller_Plugin_Abstract
{
    /**@var string The Init Name aka initModule */
    private $_moduleInitName = &#039;&#039;;

    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $activeModuleName = $request-&gt;getModuleName();
        $this-&gt;_moduleInitName = $activeModuleName.&quot;Init&quot;;

        $appBootstrap = $this-&gt;_getMainBootstrap();
        $activeModuleBootstrap = $this-&gt;_getActiveBootstrap($appBootstrap, $activeModuleName);
        $this-&gt;_processApplicationBootstrap($appBootstrap);
        if ($activeModuleBootstrap instanceof Zend_Application_Module_Bootstrap) {
            $this-&gt;_processActiveModuleBootstrap($activeModuleBootstrap);
        }
    }

    /*****************************************************************
     * Gets the Main Boostrap Object
     *
     * @return Zend_Application_Bootstrap_Bootstrap Main Bootstrap
     *****************************************************************/
    private function _getMainBootstrap()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $bootstrap =  $frontController-&gt;getParam(&#039;bootstrap&#039;);
        return $bootstrap;
    }

    /*******************************************************************************
     * Gets the Current Active Module&#039;s Boostrap Object
     *
     * @param Zend_Application_Bootstrap_Bootstap $appBootstrap The Main Bootstrap
     * @param String $activeModuleName The name to find.
     * @return Zend_Application_Module_Bootstrap Active Module Bootstrap
     ******************************************************************************/
    private function _getActiveBootstrap($appBootstrap, $activeModuleName)
    {
        $moduleList = $appBootstrap-&gt;modules;
        if (isset($moduleList[$activeModuleName])) {
            $activeModule = $moduleList[$activeModuleName];
        } else {
            $activeModule = $appBootstrap;
        }
        return $activeModule;
    }

     /*********************************************************
     * Process the methods from within the main bootstrap
     * @param Zend_Application_Bootstrap_BootstrapAbstract $appBootstrap The Application Bootstrap;
     **********************************************************/
    private function _processApplicationBootstrap($appBootstrap)
    {
        $moduleInitNameLength = strlen($this-&gt;_moduleInitName);
        $bootstrapMethodNames = get_class_methods($appBootstrap);
        foreach ($bootstrapMethodNames as $key=&gt;$method) {
            $runMethod = false;
            $methodNameLength = strlen($method);
            if ($this-&gt;_isModuleNameInitMethod($method)) {
                $resource = call_user_func(array($appBootstrap, $method));
                $resourceName = substr($method, $moduleInitNameLength);
                if (!is_null($resource)) {
                    $this-&gt;storeResource($resource, $resourceName, $appBootstrap);
                }
            }
        }
    }

    /*********************************************************
     * Process the methods from within the main bootstrap
     * @param Zend_Application_Module_Bootstrap $activeModuleBootstrap The &quot;Active&quot;  Modules&#039;s Bootstrap;
     **********************************************************/
    private function _processActiveModuleBootstrap($activeModuleBootstrap)
    {
        $moduleInitNameLength = strlen($this-&gt;_moduleInitName);
        $methodNames = get_class_methods($activeModuleBootstrap);
        foreach ($methodNames as $key=&gt;$method) {
            $runMethod = false;
            if ($this-&gt;_isActiveInitMethod($method)) {
                $resourceName = substr($method, 10);
                $runMethod = true;
            } elseif ($this-&gt;_isModuleNameInitMethod($method)) {
                $resourceName = substr($method, $moduleInitNameLength);
                $runMethod = true;
            }
            if ($runMethod) {
                $resource = call_user_func(array($activeModuleBootstrap, $method));
                if (!is_null($resource)) {
                    $this-&gt;storeResource($resource, $resourceName, $activeModuleBootstrap);
                }
            }
        }
    }

    /*******************************************************
     * Check to see if the method is in style of ModulenameInitXXX
     * @param string $method The method name to check
     ********************************************************/
    private function _isModuleNameInitMethod($method)
    {
        $methodNameLength = strlen($method);
        $moduleInitNameLength = strlen($this-&gt;_moduleInitName);
        $methodNameLonger = ($moduleInitNameLength &lt; $methodNameLength);
        $methodNameBeginMatch = $this-&gt;_moduleInitName == substr($method, 0, $moduleInitNameLength);
        return $methodNameLonger &amp;&amp; $methodNameBeginMatch;
    }

    /*******************************************************
     * Check to see if the method is in style of activeInitXXX
     * @param string $method The method name to check
     ********************************************************/
    private function _isActiveInitMethod($method)
    {
        $methodNameLength = strlen($method);
        $methodNameLonger = ($methodNameLength &gt; 10);
        $methodNameBeginMatch = &#039;activeInit&#039; === substr($method, 0, 10);
        return $methodNameLonger &amp;&amp; $methodNameBeginMatch;
    }

    /***********************************
     * Store the resource returned by the function so that it can be &quot;bootstrapped&quot;
     * @param misc $resource The Resource to be stored
     * @param string $name the name of the resource
     * @param Zend_Application_Bootstrap_BootstrapAbstract $bootstrap The Bootstrap against which to store the resource
     ********************/
    private function storeResource($resource, $name, $bootstrap)
    {
        // Store the resource.. not sure how to do this yet.. if you do let me know! <img src='http://binarykitten.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/zend-framework/296-active-module-config-v2.html/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Per form stylesheet with Zend_Form</title>
		<link>http://binarykitten.com/dev/zend-framework/274-per-form-stylesheet-with-zend_form.html</link>
		<comments>http://binarykitten.com/dev/zend-framework/274-per-form-stylesheet-with-zend_form.html#comments</comments>
		<pubDate>Mon, 01 Mar 2010 21:58:04 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://binarykitten.me.uk/?p=274</guid>
		<description><![CDATA[I needed to have certain form styles attached when a form is in use. I didn&#8217;t want to do this from the controller each and every time as it was really was only to do with the form and I might want to use the form with it&#8217;s own stylesheet in other places. The Solution [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to have certain form styles attached when a form is in use. I didn&#8217;t want to do this from the controller each and every time as it was really was only to do with the form and I might want to use the form with it&#8217;s own stylesheet in other places.<br />
<span id="more-274"></span></p>
<h3>The Solution</h3>
<p>The solution is to extend Zend_form and use the extended version as the basis for all the forms in the application. Here is my Form class.<br />
<code>
<pre class="brush: php">
&lt;?php
class BinaryKitten_Form extends Zend_Form
{
    private $_stylesheets = array(
        &#039;main&#039;=&gt;array(&#039;href&#039;=&gt;&#039;/css/form.css&#039;, &#039;media&#039;=&gt;&#039;screen&#039;)
    );

    public function addStylesheet($href,$media=&#039;screen&#039;)
    {
        $this-&gt;_stylesheets[basename($href)] = array(
            &quot;href&quot;=&gt;$href,
            &quot;media&quot;=&gt;$media
        );
    }

    public function removeStylesheet($href)
    {
        unset($this-&gt;_stylesheets[basename($href)]);
    }

    public function clearStylesheets()
    {
        $this-&gt;_stylesheets = array();
    }

    public function render(Zend_View_Interface $view = null)
    {
        if (null !== $view) {
            $this-&gt;setView($view);
        }
        else {
            $view = $this-&gt;getView();
        }

        foreach ($this-&gt;_stylesheets as $stylesheet) {
            $view-&gt;headLink()-&gt;appendStylesheet($stylesheet[&#039;href&#039;], $stylesheet[&#039;media&#039;]);
        }

        $content = &#039;&#039;;
        foreach ($this-&gt;getDecorators() as $decorator) {
            $decorator-&gt;setElement($this);
            $content = $decorator-&gt;render($content);
        }
        return $content;
    }
}
</pre>
<p></code></p>
<p>Of course you would extend it as usual to create a new form&#8230;.</p>
<pre class="brush: php">
&lt;?php
class BinaryKitten_Form_LoginForm extends BinaryKitten_Form
{
    public function __construct($option = null)
    {
        parent::__construct($option);
        $decorators = array(
            array(&#039;ViewHelper&#039;),
            array(&#039;Label&#039;, array(
                &#039;requiredSuffix&#039;=&gt;&quot; *&quot;
            )),
            array(&#039;HtmlTag&#039;, array(&#039;tag&#039;=&gt;&#039;div&#039;))
        );

        $username = new Zend_Form_Element_Text(&#039;username&#039;);
        $username
            -&gt;setLabel(&#039;Username&#039;)
            -&gt;setRequired(true)
            -&gt;addValidator(&#039;NotEmpty&#039;, true)
            -&gt;addFilter(&quot;StringTrim&quot;)
            -&gt;addErrorMessage(&#039;Please Enter your Username&#039;)
            -&gt;setDecorators($decorators);

        $password = new Zend_Form_Element_Password(&#039;password&#039;);
        $password
            -&gt;setLabel(&#039;Password&#039;)
            -&gt;setRequired(true)
            -&gt;addValidator(&#039;NotEmpty&#039;, true)
            -&gt;addFilter(&quot;StringTrim&quot;)
            -&gt;addErrorMessage(&#039;Please Enter your Password&#039;)
            -&gt;setDecorators($decorators);

        $loginButton = new Zend_Form_Element_Submit(&#039;login&#039;);
        $loginButton
            -&gt;setValue(&#039;login&#039;);

        $this
            -&gt;addElements(
                array(
                    $username,
                    $password,
                    $loginButton
                )
            )
            -&gt;setMethod(&#039;post&#039;)
            -&gt;setName(&#039;loginForm&#039;)
            -&gt;addDecorator(
                array(&#039;mytag&#039; =&gt; &#039;HtmlTag&#039;), array(&#039;tag&#039; =&gt; &#039;div&#039;)
            )
            -&gt;removeDecorator(&#039;HtmlTag&#039;);

        $this-&gt;addStylesheet(&#039;/css/loginform.css&#039;,&#039;screen&#039;);
    }
}
</pre>
<p>And then as usual in you controller:</p>
<pre class="brush: php">
$form = new BinaryKitten_Form_LoginForm();
$this-&gt;view-&gt;form = $form;
</pre>
<p>and of course, echo it out in the view<br />
[sourcecoe lang="php"]<br />
echo $this->form;<br />
[/sourcecode]</p>
<p>And that&#8217;s it, Simple.<br />
Hopefully this will help someone out there</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/zend-framework/274-per-form-stylesheet-with-zend_form.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Active Module Based Config with Zend Framework</title>
		<link>http://binarykitten.com/dev/zend-framework/177-active-module-based-config-with-zend-framework.html</link>
		<comments>http://binarykitten.com/dev/zend-framework/177-active-module-based-config-with-zend-framework.html#comments</comments>
		<pubDate>Mon, 04 Jan 2010 22:41:41 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://binarykitten.me.uk/?p=177</guid>
		<description><![CDATA[I&#8217;ve recently taken to using Zend Framework for a project that I needed to bring up to date. I won&#8217;t go into the pros and cons of choosing a framework as there are many much more qualified people who have done a much better job of this subject than I would or could. So Instead [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently taken to using Zend Framework for a project that I needed to bring up to date. I won&#8217;t go into the pros and cons of choosing a framework as there are many much more qualified people who have done a much better job of this subject than I would or could. So Instead I bring to you How I managed to get Active Module Based Configuration within Zend Framework.</p>
<h2><strong>The Problem</strong></h2>
<p>The Concept I wanted to achieve was to have unique Configuration based upon the module that was active. The Issue with this is that the Bootstrap files and the _init functions for ALL modules are called with no bias as to which module is active. Thus if you created a 3 modules wanted to make menu alterations in one, those alterations will be applied to all. I also wanted to have a a system where if i added extra modules i could just add extra functions to the bootstrap file and it would work in a similar way.</p>
<p>With this in mind, I set about trying to figure out the solution.<br />
<span id="more-177"></span></p>
<h2><strong>New Version</strong></h2>
<p>Though this version still works and you should read through the code to see how to implement the plugin, there is a new version available at:<br />
<a href="http://binarykitten.me.uk/dev/zend-framework/296-active-module-config-v2.html">http://binarykitten.me.uk/dev/zend-framework/296-active-module-config-v2.html</a> <br />
Please refer to this as the latest version.. Thanks</p>
<p>&#8212;&#8211; Original Post &#8212;&#8211;</p>
<h2><strong>Solution 1 &#8211; Failure</strong></h2>
<p>With the Idea that no-body is perfect, including myself. My 1st attempt ended in failure. This attempt was to modify/extend the Bootstrap class to add extra functions to the resources list.. In the end I couldn&#8217;t determine if the Module bootstrap was the active one. ok So Attempt 1 was a failure, onto the next</p>
<h2>Solution 2 &#8211; Success!</h2>
<p>A quick conversation with Matthew Weier O&#8217;Phinney (<a href="http://twitter.com/weierophinney">@weierophinney</a>) pointed me in the direction of Controller plugins and the routeShutdown method, as after the route had been finished which module was active would be able to be discerned.<br />
At this point I must apologise to Pieter Kokx ( @kokxie )  who I had a small disagreement with in the #zftalk channel. Pieter had done his best to point me down this route to start with, though being a stubborn mule I am refused to see the quality and precision of his comments.<br />
Thank you both for your help here.</p>
<p>The way that this works is that is scans the active modules bootstrap for functions starting with <span style="text-decoration: underline">activeInit</span> or <span style="text-decoration: underline">modulenameInit</span> just like the <a href="http://is.gd/64s1y">_init</a> functions but these would only be called if the module is active.<br />
I was successfully able to create the plugin and trigger the functions, unfortunately it was triggering/calling them in a static sense.. which meant that standard _init style code wouldn&#8217;t work. Luckily with a little digging in the source of the framework i found a storage of the modules and their initiated bootstrap classes. Lucky Me! So finally we call the methods within the right context.</p>
<p>So Here it is the Final Code, Please do comment, I learn from people as I hope that others can learn from me.</p>
<p>I&#8217;ve used the &#8220;Namespace&#8221; of BinaryKitten here. If you want to use a different &#8220;Namespace&#8221; Replace BinaryKitten with what you want.<br />
The &#8220;Namespace&#8221; allows for the use of the BinaryKitten folder within the Libray Folder.<br />
Remember the Controller Plugin should go in the right place for your application, if you are using the autoloader you can add the &#8220;Namespace&#8221; to be autoloaded via your application.ini</p>
<pre class="brush: text">
autoloadernamespaces[] = &quot;BinaryKitten&quot;
</pre>
<p>First off we have the Controller Plugin.<br />
This should go into the &#8220;Namespace&#8221; folder within the Library Folder and should be called ModuleConfig.php</p>
<pre class="brush: php">
&lt;?php
class BinaryKitten_ModuleConfig extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $frontController = Zend_Controller_Front::getInstance();
        $bootstrap =  $frontController-&gt;getParam(&#039;bootstrap&#039;);
        $activeModuleName = $request-&gt;getModuleName();
        $moduleList = $bootstrap-&gt;modules;

        $moduleInitName = strtolower($activeModuleName).&quot;Init&quot;;
        $moduleInitNameLength = strlen($moduleInitName);

        if (isset($moduleList[$activeModuleName])) {
            $activeModule = $moduleList[$activeModuleName];

            $bootstrapMethodNames = get_class_methods($bootstrap);
            foreach ($bootstrapMethodNames as $key=&gt;$method) {
                $runMethod = false;
                $methodNameLength = strlen($method);
                if ($moduleInitNameLength &lt; $methodNameLength &amp;&amp;
                    $moduleInitName == substr($method, 0, $moduleInitNameLength)) {
                    call_user_func(array($bootstrap,$method));
                }
            }
        } else {
            $activeModule = $bootstrap;
        }

        $methodNames = get_class_methods($activeModule);
        foreach ($methodNames as $key=&gt;$method) {
            $runMethod = false;
            $methodNameLength = strlen($method);
            if (10 &lt; $methodNameLength &amp;&amp; &#039;activeInit&#039; === substr($method, 0, 10)) {
                $runMethod = true;
            } elseif ($moduleInitNameLength &lt; $methodNameLength &amp;&amp;
                    $moduleInitName == substr($method, 0, $moduleInitNameLength)) {
                $runMethod = true;
            }
            if ($runMethod) {
                call_user_func(array($activeModule,$method));
            }
        }
    }
}
</pre>
<p>Next we need to make sure the Controller Plugin is loaded.<br />
We can do this in one of two ways. Either in the Application Bootstrap via an _init function</p>
<pre class="brush: php">
public function _initControllerPlugins()
{
    $plugin = Zend_Controller_Front::getInstance()-&gt;registerPlugin(
        new BinaryKitten_ModuleConfig()
    );
}
</pre>
<p>*&#8211; Or &#8211;*</p>
<p>We can add a line to the application.ini</p>
<pre class="brush: text">
resources.frontController.plugins.BKModuleConfig = &quot;BinaryKitten_ModuleConfig&quot;
</pre>
<p>Finally some example init code from the module bootstrap.<br />
Please remember that the activeInit*() Functions need to be public for this to work properly</p>
<pre class="brush: php">
class Default_Bootstrap extends Zend_Application_Module_Bootstrap {
    public function activeInitMenus() {
        $layout = $this
                    -&gt;bootstrap(&#039;layout&#039;)
                    -&gt;getResource(&#039;layout&#039;);

        $view = $layout-&gt;getView();
        $config = new Zend_Config_Xml(APPLICATION_PATH.&#039;/configs/navigation_default.xml&#039;,&quot;menu&quot;);
        $navigation = new Zend_Navigation($config);
        $view-&gt;navigation($navigation);
    }
    public function activeInitDoSomethingElse() {
        /* some other code */
    }
    public function defaultInitSomething() {
    	/* more code */
    }
}
</pre>
<p>We can also add module inits to the application bootstrap like so:</p>
<pre class="brush: php">
    public function modulenameInitFunction() {
    	/* place code here */
    }
</pre>
<p>Where modulename is the lowercase version of the Modules name, eg if you have the Admin module, then you would use:</p>
<pre class="brush: php">
    public function adminInitFunction() {
    	/* place code here */
    }
</pre>
<p>Hopefully someone will find this code useful.</p>
<p>[ Edit January 5th 2010 ]<br />
Thanks to:<br />
	Matthew Weier O&#8217;Phinney for pointing out places for update.<br />
	Rob Allen (@Akrabat) for the info that the plugin could be loaded via the application.ini<br />
	Elizabeth Marie Smith and Matthew Turland for questioning the use of the Reflection.</p>
<ul>
<li>Updated the Bootstrap code to properly define the functions as Public</li>
<li>Removed the reflection as this wasn&#8217;t actually required any more.</li>
<li>Updated Post to make clean up the order of things and to properly designate that the BinaryKitten is the &#8220;Namespace&#8221;<br />
&#8220;Namespace&#8221; is used to reference that we&#8217;re not using PHP5.3 Namespaces, but the Namespaces within the Zend Framework.</li>
</ul>
<p>[ Edit January 11th 2010 ]<br />
Thanks to:<br />
	septem for pointing out a typo where i had $boostrap instead of $bootstrap<br />
	Gerard Roche for pointing out that by default, the default module doesn&#8217;t require a module bootstrap (in my code it has one)</p>
<ul>
<li>Updated to fix the typos</li>
<li>Added in a quick check to see if the module exists in the modules list of bootstraps</li>
<li>Removed the _ from the function name that it searches for, this should please the people who are adamant over the Zend Coding Standards</li>
<li>Added in the functionality to have $modulenameInit() functions as well in both active module bootstrap and the application bootstrap</li>
</ul>
<p>[ Edit February 13th 2010 ]<br />
Ran the code through codesniffer against the Zend Standard supplied.. updated so no errors found. 3 Warnings are left .. they are as follows:<br />
22 | WARNING | Line exceeds 80 characters; contains 83 characters<br />
35 | WARNING | Line exceeds 80 characters; contains 84 characters<br />
38 | WARNING | Line exceeds 80 characters; contains 83 characters<br />
Don&#8217;t think it&#8217;s worth the change for 3/4 characters </p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/zend-framework/177-active-module-based-config-with-zend-framework.html/feed</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Zend Framework – Getting Single Components</title>
		<link>http://binarykitten.com/dev/zend-framework/126-zend-framework-getting-single-components.html</link>
		<comments>http://binarykitten.com/dev/zend-framework/126-zend-framework-getting-single-components.html#comments</comments>
		<pubDate>Sun, 22 Feb 2009 19:43:17 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=126</guid>
		<description><![CDATA[Recently i was looking at implementing some caching to recent websites.. 1 to improve load times as well as a possibility to improve the way that my &#8220;Framework&#8221; works. I had just recently looked over Rob Allen&#8217;s (@Akrabat http://akrabat.com/) PHP Nw 08 Conference Talk on &#8220;First Steps with Zend Framework&#8221;. As I had not been [...]]]></description>
			<content:encoded><![CDATA[<p>Recently i was looking at implementing some caching to recent websites.. 1 to improve load times as well as a possibility to improve the way that my &#8220;Framework&#8221; works.</p>
<p>I had just recently looked over Rob Allen&#8217;s (<a href="http://www.twitter.com/akrabat" target="_blank"><a href="http://twitter.com/Akrabat">@Akrabat</a></a> <a href="http://akrabat.com/" target="_blank">http://akrabat.com/</a>) <a href="http://conference.phpnw.org.uk/" target="_blank">PHP Nw 08 Conference</a> Talk on &#8220;First Steps with Zend Framework&#8221;. As I had not been really feeling all that great at the conference itself, I was glad to be able to watch teh recorded version on the web. During the 1st segment of the talk, Rob described how zend cache was his first foray into the Zend Framework and this lead me onto how I was thinking about how the best way to implement the caching on my site(s).</p>
<p>So, with this in mind, off I trotted to <a href="http://framework.zend.com" target="_blank">http://framework.zend.com</a> to grab the Zend Cache component that I wanted to look at. That&#8217;s where I hit a snag. It seemed impossible to actually get the components on their own. After about half and hour of scouring the site I still couldn&#8217;t find what I wanted, so I <a href="http://twitter.com/BinaryKitten/status/1237645252" target="_self">Tweeted about my annoyance</a>..   To which I got a couple of replies&#8230; the 1st <a href="http://twitter.com/rchiswell/status/1237680023" target="_blank">reply</a> came from Richard Chiswell (<a href="http://twitter.com/rchiswell" target="_blank">@rchiswell</a>) who said:</p>
<p><em><span class="status-body"><span class="entry-content">@<a href="http://twitter.com/BinaryKitten">BinaryKitten</a> You might find the Dependencies list at <a rel="nofollow" href="http://bit.ly/199FYR" target="_blank">http://bit.ly/199FYR</a> useful. You could try contacting @<a href="http://twitter.com/CalEvans">CalEvans</a> who wrote a book on ZF</span></span></em></p>
<p><span class="status-body"><span class="entry-content">The bit.ly link took me to: http://framework.zend.com/manual/en/requirements.html#requirements.dependencies  the listing of the components and their dependencies, Though this was </span></span><span class="status-body"><span class="entry-content">helpful it wasn&#8217;t really as helpful as the <a href="http://twitter.com/CalEvans/status/1237686345" target="_blank">reply</a> i got from Cal Evans (<a href="http://twitter.com/CalEvans" target="_blank">@CalEvans</a>):</span></span></p>
<p><em><span class="status-body"><span class="entry-content">@<a href="http://twitter.com/BinaryKitten">BinaryKitten</a> <a rel="nofollow" href="http://epic.codeutopia.net/pack/" target="_blank">http://epic.codeutopia.net/pack/</a></span></span></em></p>
<p><span class="status-body"><span class="entry-content">Short Sweet and Wham.. That is exactly what I needed.</span></span></p>
<p><span class="status-body"><span class="entry-content">Shortly afterwards <a href="http://blog.rac.me.uk/2009/02/22/php-getting-individual-packages-on-zend-framework/" target="_blank">Richard blogged</a> about this, which in turn made me feel like I should blog about it as well, and oooh look.. So I have.</span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/zend-framework/126-zend-framework-getting-single-components.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

