📜 ⬆️ ⬇️

XML config for different hosts

I read the article about the config, the main idea is to make a config that does not have to be changed on the local computer and on the hosting. Depending on where the site is running, specific settings will be used. Unfortunately, the link was not saved, could not be found again. I decided to implement it by inheriting Zend_Config_Xml.


Using this idea, the config will look something like this:
<? xml version = "1.0" encoding = "UTF-8" ? >
< config >
< base >
< models >
< account > Model_Account </ account >
</ models >
< database >
< adapter > Pdo_Mysql </ adapter >
</ database >
< site >
< server > </ server >
< root > </ root >
< language > en </ language >
< timezone > Europe / Moscow </ timezone >
</ site >
< debug > 4 </ debug >
</ base >
< configuration xmlns = " localhost " name = "Home" extends = "base" >
< database >
< params >
< host > localhost </ host >
< dbname > mydb </ dbname >
< username > root </ username >
< password > </ password >
</ params >
</ database >
</ configuration >
< configuration xmlns = " mysite.com " name = "Hosting" extends = "base" >
< database >
< params >
< host > localhost </ host >
< dbname > mysiteDb </ dbname >
< username > dbUser </ username >
< password > siteDbPassword </ password >
</ params >
</ database >
</ configuration >
</ config > This source code was highlighted with Source Code Highlighter .


The base configuration is common for any conditions to use it, in other configurations it is specified through the extends attribute, this is the standard way that is implemented in Zend_Config_Xml. The xmlns attribute indicates the address for which its own configuration will be applied.
')
Here is the class itself:
<? php
/ **
*
* @package Mego
* copyright Copyright © 2006-2008 Alexey Prokhorov
* version 1.0.0.1
* /

/ **
* see Zend_Config
* /
require_once 'Zend / Config.php' ;

/ **
* @category Mego
* @package Zend_Config
* copyright Copyright © 2006-2008 Alexey Prokhorov
* version 1.0.0.1
* @throws Zend_Config_Exception
* /
class Mego_Config_Xml extends Zend_Config_Xml
{
/ **
* Loads data from an XML file.
*
* Extends the capabilities of Zend_Config_Xml by adding support for XML namespaces.
* Convenient for different domain names.
*
* param string $ file
* param string $ namespace XML Namespace
* param bool $ allowModifications
* @throws Zend_Config_Exception
* /
public function __construct ($ file, $ namespace = null , $ allowModifications = false )
{
if (empty ($ file)) {
/ **
* see Zend_Config_Exception
* /
require_once 'Zend / Config / Exception.php' ;
throw new Zend_Config_Exception ( 'Filename is not set' );
}

$ config = $ baseConfig = array ();

$ baseXml = simplexml_load_file ($ file);
if ($ namespace ) {
$ xml = $ baseXml-> children ($ namespace );
$ attrib = $ xml-> attributes ();
if (isset ($ attrib [ 'extends' ])) {
$ extends = ( string ) $ attrib [ 'extends' ];
$ baseConfig = $ this -> _ processExtends ($ baseXml, $ extends);
}
if (isset ($ attrib [ 'name' ])) {
$ baseConfig [ 'cfgName' ] = ( string ) $ attrib [ 'name' ];
}
$ xml = $ xml-> children ();
} else {
$ xml = $ baseXml;
}

foreach ($ xml as $ k => $ v) {
$ config [$ k] = $ this -> _ processExtends ($ xml, $ k);
}

if (isset ($ baseConfig)) {
$ config = $ this -> _ arrayMergeRecursive ($ baseConfig, $ config);
}

$ this -> _ allowModifications = $ allowModifications;
$ this -> _ loadedSection = null ;
$ this -> _ index = 0;
$ this -> _ data = array ();
foreach ($ config as $ key => $ value ) {
if (is_array ($ value )) {
$ this -> _ data [$ key] = new Zend_Config ($ value , $ this -> _ allowModifications);
} else {
$ this -> _ data [$ key] = $ value ;
}
}
$ this -> _ count = count ($ this -> _ data);
}
}
* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/28387/


All Articles