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 ++++++++++++++++++++++++++----------- framework/Util/TParameterModule.php | 76 +++++++++++++++++++++++++----------- 2 files changed, 108 insertions(+), 45 deletions(-) (limited to 'framework/Util') 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); } diff --git a/framework/Util/TParameterModule.php b/framework/Util/TParameterModule.php index 529f20ca..7bc03e3b 100644 --- a/framework/Util/TParameterModule.php +++ b/framework/Util/TParameterModule.php @@ -4,7 +4,7 @@ * * @author Qiang Xue * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005-2008 PradoSoft + * @copyright Copyright © 2005-2008 PradoSoft * @license http://www.pradosoft.com/license/ * @version $Id$ * @package System.Util @@ -46,63 +46,95 @@ */ class TParameterModule extends TModule { + /** + * @deprecated since 3.2 + */ const PARAM_FILE_EXT='.xml'; private $_initialized=false; private $_paramFile=null; /** * Initializes the module by loading parameters. - * @param TXmlElement content enclosed within the module tag + * @param mixed content enclosed within the module tag */ public function init($config) { $this->loadParameters($config); if($this->_paramFile!==null) { + $configFile = null; if(($cache=$this->getApplication()->getCache())!==null) { $cacheKey='TParameterModule:'.$this->_paramFile; if(($dom=$cache->get($cacheKey))===false) { - $dom=new TXmlDocument; - $dom->loadFromFile($this->_paramFile); - $cache->set($cacheKey,$dom,0,new TFileCacheDependency($this->_paramFile)); + if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) + $configFile = include $this->_paramFile; + else + { + $configFile=new TXmlDocument; + $configFile->loadFromFile($this->_paramFile); + } + $cache->set($cacheKey,$configFile,0,new TFileCacheDependency($this->_paramFile)); } } else { - $dom=new TXmlDocument; - $dom->loadFromFile($this->_paramFile); + if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) + $configFile = include $this->_paramFile; + else + { + $configFile=new TXmlDocument; + $configFile->loadFromFile($this->_paramFile); + } } - $this->loadParameters($dom); + $this->loadParameters($configFile); } $this->_initialized=true; } /** * Loads parameters into application. - * @param TXmlElement XML representation of the parameters + * @param mixed XML or PHP representation of the parameters * @throws TConfigurationException if the parameter file format is invalid */ - protected function loadParameters($xmlNode) + protected function loadParameters($config) { $parameters=array(); - foreach($xmlNode->getElementsByTagName('parameter') as $node) + if(is_array($config)) + { + foreach($config as $id => $parameter) + { + if(is_array($parameter) && isset($parameter['class'])) + { + $properties = isset($parameter['properties'])?$parameter['properties']:array(); + $parameters[$id]=array($parameter['class'],$properties); + } + else + { + $parameters[$id] = $parameter; + } + } + } + else if($config instanceof TXmlElement) { - $properties=$node->getAttributes(); - if(($id=$properties->remove('id'))===null) - throw new TConfigurationException('parametermodule_parameterid_required'); - if(($type=$properties->remove('class'))===null) + foreach($config->getElementsByTagName('parameter') as $node) { - if(($value=$properties->remove('value'))===null) - $parameters[$id]=$node; + $properties=$node->getAttributes(); + if(($id=$properties->remove('id'))===null) + throw new TConfigurationException('parametermodule_parameterid_required'); + if(($type=$properties->remove('class'))===null) + { + if(($value=$properties->remove('value'))===null) + $parameters[$id]=$node; + else + $parameters[$id]=$value; + } else - $parameters[$id]=$value; + $parameters[$id]=array($type,$properties->toArray()); } - else - $parameters[$id]=array($type,$properties->toArray()); } - + $appParams=$this->getApplication()->getParameters(); foreach($parameters as $id=>$parameter) { @@ -136,7 +168,7 @@ class TParameterModule extends TModule { if($this->_initialized) throw new TInvalidOperationException('parametermodule_parameterfile_unchangeable'); - else if(($this->_paramFile=Prado::getPathOfNamespace($value,self::PARAM_FILE_EXT))===null || !is_file($this->_paramFile)) + else if(($this->_paramFile=Prado::getPathOfNamespace($value,$this->getApplication()->getConfigurationFileExt()))===null || !is_file($this->_paramFile)) throw new TConfigurationException('parametermodule_parameterfile_invalid',$value); } } -- cgit v1.2.3