From 736e92efbc75908a2bf26fe333a03990e3bb8100 Mon Sep 17 00:00:00 2001 From: carlgmathisen <> Date: Sun, 7 Dec 2008 13:05:05 +0000 Subject: work on php style configuration --- framework/Util/TLogRouter.php | 77 ++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 23 deletions(-) (limited to 'framework/Util/TLogRouter.php') diff --git a/framework/Util/TLogRouter.php b/framework/Util/TLogRouter.php index aa772194..c693d24a 100644 --- a/framework/Util/TLogRouter.php +++ b/framework/Util/TLogRouter.php @@ -27,20 +27,21 @@ Prado::using('System.Data.TDbConnection'); * * * + * PHP configuration style: + * + * + * * You can specify multiple routes with different filtering conditions and different * targets, even if the routes are of the same type. * * @author Qiang Xue + * @author Carl G. Mathisen * @version $Id$ * @package System.Util * @since 3.0 */ class TLogRouter extends TModule { - /** - * File extension of external configuration file - */ - const CONFIG_FILE_EXT='.xml'; /** * @var array list of routes available */ @@ -53,7 +54,7 @@ class TLogRouter extends TModule /** * Initializes this module. * This method is required by the IModule interface. - * @param TXmlElement configuration for this module, can be null + * @param mixed configuration for this module, can be null * @throws TConfigurationException if {@link getConfigFile ConfigFile} is invalid. */ public function init($config) @@ -62,9 +63,17 @@ class TLogRouter extends TModule { if(is_file($this->_configFile)) { - $dom=new TXmlDocument; - $dom->loadFromFile($this->_configFile); - $this->loadConfig($dom); + if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) + { + $phpConfig = include $this->_configFile; + $this->loadConfig($phpConfig); + } + else + { + $dom=new TXmlDocument; + $dom->loadFromFile($this->_configFile); + $this->loadConfig($dom); + } } else throw new TConfigurationException('logrouter_configfile_invalid',$this->_configFile); @@ -74,24 +83,46 @@ class TLogRouter extends TModule } /** - * Loads configuration from an XML element - * @param TXmlElement configuration node + * Loads configuration from an XML element or PHP array + * @param mixed configuration node * @throws TConfigurationException if log route class or type is not specified */ - private function loadConfig($xml) + private function loadConfig($config) { - foreach($xml->getElementsByTagName('route') as $routeConfig) + if(is_array($config)) { - $properties=$routeConfig->getAttributes(); - if(($class=$properties->remove('class'))===null) - throw new TConfigurationException('logrouter_routeclass_required'); - $route=Prado::createComponent($class); - if(!($route instanceof TLogRoute)) - throw new TConfigurationException('logrouter_routetype_invalid'); - foreach($properties as $name=>$value) - $route->setSubproperty($name,$value); - $this->_routes[]=$route; - $route->init($routeConfig); + if(isset($config['routes']) && is_array($config['routes'])) + { + foreach($config['routes'] as $route) + { + $properties = isset($route['properties'])?$route['properties']:array(); + if(!isset($route['class'])) + throw new TConfigurationException('logrouter_routeclass_required'); + $route=Prado::createComponent($route['class']); + if(!($route instanceof TLogRoute)) + throw new TConfigurationException('logrouter_routetype_invalid'); + foreach($properties as $name=>$value) + $route->setSubproperty($name,$value); + $this->_routes[]=$route; + $route->init($routeConfig); + } + } + } + else + { + foreach($config->getElementsByTagName('route') as $routeConfig) + { + $properties=$routeConfig->getAttributes(); + if(($class=$properties->remove('class'))===null) + throw new TConfigurationException('logrouter_routeclass_required'); + $route=Prado::createComponent($class); + if(!($route instanceof TLogRoute)) + throw new TConfigurationException('logrouter_routetype_invalid'); + foreach($properties as $name=>$value) + $route->setSubproperty($name,$value); + $this->_routes[]=$route; + $route->init($routeConfig); + } } } @@ -124,7 +155,7 @@ class TLogRouter extends TModule */ public function setConfigFile($value) { - if(($this->_configFile=Prado::getPathOfNamespace($value,self::CONFIG_FILE_EXT))===null) + if(($this->_configFile=Prado::getPathOfNamespace($value,$this->getApplication()->getConfigurationFileExt()))===null) throw new TConfigurationException('logrouter_configfile_invalid',$value); } -- cgit v1.2.3