summaryrefslogtreecommitdiff
path: root/framework/Util
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Util')
-rw-r--r--framework/Util/TLogRouter.php81
-rw-r--r--framework/Util/TParameterModule.php76
2 files changed, 109 insertions, 48 deletions
diff --git a/framework/Util/TLogRouter.php b/framework/Util/TLogRouter.php
index b7b2f0a3..cc736fae 100644
--- a/framework/Util/TLogRouter.php
+++ b/framework/Util/TLogRouter.php
@@ -27,10 +27,15 @@ Prado::using('System.Data.TDbConnection');
* <route class="TFileLogRoute" Categories="System.Web.UI" Levels="Warning" />
* <route class="TEmailLogRoute" Categories="Application" Levels="Fatal" Emails="admin@pradosoft.com" />
* </code>
+ * PHP configuration style:
+ * <code>
+ *
+ * </code>
* You can specify multiple routes with different filtering conditions and different
* targets, even if the routes are of the same type.
*
* @author Qiang Xue <qiang.xue@gmail.com>
+ * @author Carl G. Mathisen <carlgmathisen@gmail.com>
* @version $Id$
* @package System.Util
* @since 3.0
@@ -38,10 +43,6 @@ Prado::using('System.Data.TDbConnection');
class TLogRouter extends TModule
{
/**
- * File extension of external configuration file
- */
- const CONFIG_FILE_EXT='.xml';
- /**
* @var array list of routes available
*/
private $_routes=array();
@@ -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,31 +83,53 @@ 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($route);
+ }
+ }
+ }
+ 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);
+ }
}
}
/**
* Adds a TLogRoute instance to the log router.
- *
- * @param TLogRoute $route
+ *
+ * @param TLogRoute $route
* @throws TInvalidDataTypeException if the route object is invalid
*/
public function addRoute($route)
@@ -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..0109ca32 100644
--- a/framework/Util/TParameterModule.php
+++ b/framework/Util/TParameterModule.php
@@ -4,7 +4,7 @@
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005-2008 PradoSoft
+ * @copyright Copyright &copy; 2005-2008 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Id$
* @package System.Util
@@ -40,67 +40,97 @@
* tag, the former takes precedence.
*
* @author Qiang Xue <qiang.xue@gmail.com>
+ * @author Carl G. Mathisen <carlgmathisen@gmail.com>
* @version $Id$
* @package System.Util
* @since 3.0
*/
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)
{
- if(($cache=$this->getApplication()->getCache())!==null)
+ $configFile = null;
+ if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_XML && ($cache=$this->getApplication()->getCache())!==null)
{
$cacheKey='TParameterModule:'.$this->_paramFile;
- if(($dom=$cache->get($cacheKey))===false)
+ if(($configFile=$cache->get($cacheKey))===false)
{
- $dom=new TXmlDocument;
- $dom->loadFromFile($this->_paramFile);
- $cache->set($cacheKey,$dom,0,new TFileCacheDependency($this->_paramFile));
+ $cacheFile=new TXmlDocument;
+ $cacheFile->loadFromFile($this->_paramFile);
+ $cache->set($cacheKey,$cacheFile,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 of 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))
{
- $properties=$node->getAttributes();
- if(($id=$properties->remove('id'))===null)
- throw new TConfigurationException('parametermodule_parameterid_required');
- if(($type=$properties->remove('class'))===null)
+ foreach($config as $id => $parameter)
{
- if(($value=$properties->remove('value'))===null)
- $parameters[$id]=$node;
+ if(is_array($parameter) && isset($parameter['class']))
+ {
+ $properties = isset($parameter['properties'])?$parameter['properties']:array();
+ $parameters[$id]=array($parameter['class'],$properties);
+ }
else
- $parameters[$id]=$value;
+ {
+ $parameters[$id] = $parameter;
+ }
+ }
+ }
+ else if($config instanceof TXmlElement)
+ {
+ foreach($config->getElementsByTagName('parameter') as $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]=array($type,$properties->toArray());
}
- else
- $parameters[$id]=array($type,$properties->toArray());
}
$appParams=$this->getApplication()->getParameters();
@@ -136,7 +166,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);
}
}