<?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>Jens Arps &#187; doctrine</title>
	<atom:link href="http://jensarps.de/tag/doctrine/feed/" rel="self" type="application/rss+xml" />
	<link>http://jensarps.de</link>
	<description></description>
	<lastBuildDate>Fri, 25 Nov 2011 16:16:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Static data handling in PHP</title>
		<link>http://jensarps.de/2009/09/03/static-data-handling-in-php/</link>
		<comments>http://jensarps.de/2009/09/03/static-data-handling-in-php/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 19:13:42 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Goodies to go]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=26</guid>
		<description><![CDATA[Every now and then I stumble upon PHP Code dealing with config files. Besides being happy that people use config files (uh, yes, it happens too often that people don&#8217;t…), I am often unhappy with how they&#8217;re dealing with config files. You can find the ugliest things out there, like arrays in .php files (my [...]]]></description>
			<content:encoded><![CDATA[<figure><img class="alignleft size-thumbnail wp-image-32" title="Bild 17" src="http://jensarps.de/wp-content/uploads/2009/09/Bild-17.png" alt="Bild 17" width="150" />Every now and then I stumble upon PHP Code dealing with config files. Besides being happy that people use config files (uh, yes, it happens too often that people don&#8217;t…), I am often unhappy with how they&#8217;re dealing with config files. You can find the ugliest things out there, like arrays in .php files (my favorite). However, even if config files are used, there are strange things happening to them, like being read once and serialized into a constant.</p>
<p>So I want to share today a very primitive static data handler that I use for quite some time now, and wich has always served me well. By static, I mean data that doesn&#8217;t change during runtime, like smtp server connection data, database connection data and the likes.</p>
<p><span id="more-26"></span></figure>
<p>The idea is to have a seperate directory (named &#8220;static&#8221; in my case) with .data files in it. Every .data file should care about a certain aspect of static data needed in an application. This directory is then blocked via an .htaccess file, containing the single line:</p>
<pre># file: .htaccess
Deny From All</pre>
<p>During the bootsrap process of your app, the collector is called to walk through that directory and gather the data from all the files:</p>
<pre>// file: bootstrap.php
// load static data
YourProject_StaticData::getInstance()-&gt;collectStaticData();</pre>
<p>These files are pretty much standard config files, so they are readable via php&#8217;s function parse_ini_file. That function has two major limitations: It cannot handle hierarchy beyond two levels and it breaks when it encounters a quote character in a value. These limitations are discussed elsewhere, but with this static data collector we can at least push the first one a level further away. A data file could look like this:</p>
<pre># file: mailconfig.data
[smtp]
host     = somehost.tld
auth     = true
username = user@somehost.tld
password = somepass
[lists]
whitelist = "goodone@example.com,admin@somehost.tld"
blacklist = "badone@example.com"
[options]
checkwhitelist = false
checkblacklist = true
# ...</pre>
<p>To get the connection data to the applications&#8217;s smtp account, you&#8217;d simply call:</p>
<pre>$smtpConfig = YourProject_StaticData::getInstance()-&gt;getSubSection('mailconfig','smtp');</pre>
<p>Fairly easy to use, pretty and readable. And it also is very handy for something that *very* often annoys me when looking into some client&#8217;s code: establishing a connection to a database. To different databases in most cases, for development, testing and deployment. I&#8217;ve seen pretty awkward attempts to handle this – but it can be done with one line of code, regardless of where the code is running. Say, you use <a href="http://www.doctrine-project.org/" target="_blank">Doctrine</a> as ORM for your database, and have a data file like the following, using host names as keys (besides all my love for the <a href="http://en.wikipedia.org/wiki/Nosql" target="_blank">NoSQL</a> movement, I run into a mysql database on nearly every project):</p>
<pre># file: dbms.data
project.local    = "mysql://user:pass@localhost/project_db"
test.project.tld = "mysql://testuser:testpass@project.tld/test_project_db"
project.tld      = "mysql://user:pass@project.tld/project_db"</pre>
<p>you can easily connect to your database by defining a constant that contains the current host name (wich is a good idea, in general) and call</p>
<pre>// file: bootstrap.php
//set up a connection
Doctrine_Manager::connection(
    YourProject_StaticData::getInstance()-&gt;getSubSection('dbms',HOST),
    'mainConnection'
)-&gt;setCharset("utf8");</pre>
<p>Clean and short. And independant from your location – just see to it that your data file contains the right connection data for each host, and never touch that line of code in your bootstrapping process again.</p>
<p>The PHP class uses the <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank">singleton design pattern</a> (as you already know from the code above), and is less than 100 lines of code in size. Including doc. It&#8217;s as simple as it is handy. And it can be easily extended – if the need arises, what never happened to me.</p>
<p>I hope you can find it as useful as I do!</p>
<p>You can grab the file over here: <a href="/code/StaticData.php.txt">StaticData.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2009/09/03/static-data-handling-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

