Section 3.4. Configuration

Putting It All Together > Configuration

3.4. Configuration

A well-designed configuration saves a lot of work and provides flexibility in your application. It's good practice to have a distinct configuration file for each module or functionality that you're developing.

You should keep all external dependencies (database access settings, cache lifetime, authentication settings, web services URLs, etc.) in your configuration files, so you can fine-tune them later without touching a line of code.

In this example, we use XML files that are converted to PHP arrays (the $conf array you saw previously). Each array corresponds to an XML document and will be stored on a separate file. This approach gives you the readability of XML, as well as the performance of a pure PHP configuration file.

To convert XML documents into PHP arrays, we use the PEAR Config package.[||]Please note that this script is designed for Unix-based systems.

[||] The PEAR Config package manipulates configuration data in different formats. More information is available on the PEAR web site documentation (

Here is the configuration processing script:

#!/path/to/php/binary/php -q <?php // Load PEAR's Config package. require_once 'Config.php'; if ($handle = opendir('.')) {     echo "Preparing directory...\n";     //     // We will keep all PHP configuration in a .php directory under config.     // Create the '.php' directory if it doesn't     // already exist.     //     if (!is_dir('.php')) {         mkdir('.php');     }     echo "Configuring XML files:\n";     //     // Get all '.xml' files in the current directory.     //     if (!empty($argv[1])) {         $file = $argv[1] . '.xml';     } else {         $file = readdir($handle);     }     while (false !== $file) {         if (!is_dir($file) &&             preg_match('/^(.*?)\.xml$/', $file, &$matches)) {             //             // Parse the configuration file.             //             $config =& new Config();             $root =& $config->parseConfig($file, 'xml');             //             // Output status information.             //             printf('%20s', $file);             if (!PEAR::isError($root)) {                 //                 // Open a new file and write the parsed                 // configuration array.                 //                 $newFile = fopen('.php/' . $matches[1] . '.php', 'w');                 fputs($newFile, "<?php\n" . $root->toString('phparray') . "?>");                 fclose($newFile);                 echo "\tOK\n";             } else {                 echo "\tERROR (previous configuration preserved)\n";             }             //             // Destroy the configuration object.             //             unset($config);         }         if (!empty($argv[1])) {             $file = false;         } else {             $file = readdir($handle);         }     }     closedir($handle); } else {     echo "Couldn't open configuration directory, please fix that...\n"; } echo "Finished...\n"; ?>     

This script simply finds all XML documents on a given directory and attempts to convert them into PHP arrays, saving each one in a separate .php directory.

If, for some reason, there is an error processing one of the files, the previous configuration is preserved. This feature makes this script somehow safe to run even on a production environment.

Let's now look at one possible XML configuration file. (Remember these are the only files you'll ever manipulate. You are not supposed to edit the generated PHP files.)

<?xml version="1.0" ?> <conf>   <smarty>   <paths>     <template>/Books/docroot/</template>     <compile>/Books/cache/smarty_compiled/</compile>     <cache>/Books/cache/smarty_cached</cache>   </paths>   <compile>     <check>1</check> <!-- Check whether the template has changed since last compile -->     <force>1</force> <!-- Force Smarty to compile templates on every request -->     </compile>     <caching>0</caching>   </smarty> </conf>     

This document is easy to read and edit, but it is not so good to be interpreted on every call to your scripts. That's why we convert it to a PHP array:

<?php $conf['smarty']['paths']['template'] = '/Books/docroot/'; $conf['smarty']['paths']['compile'] = '/Books/cache/smarty_compiled/'; $conf['smarty']['paths']['cache'] = '/Books/smarty_cached/'; $conf['smarty']['compile']['check'] = '1'; $conf['smarty']['compile']['force'] = '1'; $conf['smarty']['paths']['template'] = '/Books/docroot/'; $conf['smarty']['caching'] = '0'; ?>

This final configuration file is more performance oriented, but it should never be edited. If you want to make changes, you should edit the XML document and re-run the configuration script.



PHP and Smarty on Large-Scale Web Development
PHP and Smarty on Large-Scale Web Development
ISBN: 047008023X
Year: 2007
Pages: 20
BUY ON AMAZON © 2008-2017.
If you may any questions please contact us: