<?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>TBNL &#187; config</title>
	<atom:link href="http://www.tibobeijen.nl/blog/tag/config/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tibobeijen.nl</link>
	<description>...another view on the web and how it's built</description>
	<lastBuildDate>Wed, 06 Jul 2011 19:28:47 +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>Zend_Config strategies</title>
		<link>http://www.tibobeijen.nl/blog/2008/11/30/zend_config-strategies/</link>
		<comments>http://www.tibobeijen.nl/blog/2008/11/30/zend_config-strategies/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 18:24:30 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[articles]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.yunademo.nl/preview/tibobeijen.nl/?p=44</guid>
		<description><![CDATA[As applications often need to run on different setups (think: develop, test, production), configuration settings can usually be divided in a static &#8216;application&#8217; part and a more dynamic &#8216;environment&#8217; part. Zend Framework offers a very flexible set of classes that help reading and organizing configuration data and making it available throughout the entire application. But, [...]]]></description>
			<content:encoded><![CDATA[<p>As applications often need to run on different setups (think: develop, test, production), configuration settings can usually be divided in a static &#8216;application&#8217; part and a more dynamic &#8216;environment&#8217; part. Zend Framework offers a very flexible set of classes that help reading and organizing configuration data and making it available throughout the entire application. But, as very often, there is no &#8216;only way&#8217;.
</p>
<p><span id="more-44"></span></p>
<h3>Loading configuration data</h3>
<p>Basically there are three ways of loading configuration data:
</p>
<ul>
<li>1.Using Zend_Config and passing a PHP array as parameter</li>
<li>2.Using Zend_Config_Ini to load an ini configuration file</li>
<li>3.Using Zend_Config_Xml to load an xml configuration file</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$configData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'somecategory'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'somesetting'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'somevalue'</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// 1. Load array</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config<span style="color: #009900;">&#40;</span><span style="color: #000088;">$configData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// 2. Load ini file</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config_Ini<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/config.ini'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// 3. Load xml file</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config_Xml<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/config.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Alternatively, you can specify a file containing an array as require() returns the return value of a PHP script being included:
</p>
<p>config.php</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$CONFIG</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'somecategory'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'somesetting'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'somevalue'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$CONFIG</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// 1b Load array</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config<span style="color: #009900;">&#40;</span><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'path/to/config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Using configuration data</h3>
<p>Zend_Config creates a hierachy of Zend_Config instances. So a specific part of the total configuration data can be extracted and passed to a specific component. All subparts behave the same. If the component requires configuration to be passed as an array the toArray() method can be used. Instead of accessing values as properties one can also use the get() method. The advantage of get() is that a default value can be supplied to be used when the requested variable isn&#8217;t available.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// configure an instance using a specific part of the config</span>
<span style="color: #000088;">$modelComponent</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ModelComponent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">components</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">componentName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Or, if the component requires an array</span>
<span style="color: #000088;">$modelComponent</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ModelComponent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">components</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">componentName</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Get an optional config setting, supplying a default value</span>
<span style="color: #000088;">$someValue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">common</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'someValue'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'myDefaultValue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The data contained in Zend_Config is read-only which is normally a good thing. In case the configuration data has to be changed this can be overridden by supplying an additional parameter in the constructor. Then after making the neccessary changes the config object can be &#8216;sealed&#8217; by calling setReadOnly().</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// read config with $allowModifications set to true</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config<span style="color: #009900;">&#40;</span><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'path/to/config.php'</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>
&nbsp;
  <span style="color: #666666; font-style: italic;">// perhaps change some bits here and there</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullMoonMadness</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">site</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">theme</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'silver'</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allowComments</span> <span style="color: #339933;">=</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: #666666; font-style: italic;">// and prevent accidental modification</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setReadOnly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To make the loaded configuration available throughout the entire application, Zend_Registry is probably the tool of choice.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config<span style="color: #009900;">&#40;</span><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'path/to/config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// and store in registry</span>
Zend_Registry<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;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Organizing configuration data</h3>
<p>When using ini or xml configuration files, sections can be declared. On loading the file the section to be  used can be specified. A section can extend other sections thereby allowing configuration data to have a certain hierarchy.</p>
<p>Ini-file example:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>production<span style="">&#93;</span></span>
<span style="color: #000099;">www_root</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> www.mysite.com</span>
site.title <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;My Website - &quot;</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>test : production<span style="">&#93;</span></span>
<span style="color: #000099;">www_root</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> test.mysite.com</span></pre></div></div>

<p>The root node of XML files can be named arbitrarily. If you want to use sections the first level in the XML has to correspond to the section requested. There are two reserved attributes: value and extends. Other attributes can hold values that otherwise would be childnodes. See example that holds the same data as the Ini file above:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;production<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;www_root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>www.mysite.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/www_root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;site</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;My Website - &quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/production<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;test</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">&quot;production&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;www_root</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;test.mysite.com&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/test<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now the section-specific configuration can be loaded by specifying a second parameter in the constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config_Xml<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/config.xml'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Ini, XML or Array?</h3>
<p>As mentioned at the top of this article: There is no &#8216;only&#8217; way and there is no &#8216;right&#8217; or &#8216;wrong&#8217; way. Of course there are aspects that can make you favor one approach over the other, the most notable being:</p>
<ul>
<li>Only Ini and XML provide sections, allowing you to use hierarchic configuration data</li>
<li>Ini and especially XML are better suited for tooling: Software other than PHP can read or modify configuration data.</li>
<li>When using ini files it is not possible to use quotes in a value.
<li>PHP arrays have the benefit that you can put program logic <em>in</em> them. For example, you can construct variables based on defaults that are set initially. See example below</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$CONFIG</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'defaults'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fs_root'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/var/www/mysite_com/'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #000088;">$CONFIG</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'locations'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'libraries'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'zf'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$CONFIG</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'defaults'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fs_root'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'lib/zf/'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$CONFIG</span><span style="color: #339933;">;</span></pre></div></div>

<p><em>2009-05-15: added example of loading specific section by passing section parameter to constructor</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2008/11/30/zend_config-strategies/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

