summaryrefslogtreecommitdiff
path: root/framework/TApplication.php
diff options
context:
space:
mode:
authorcarlgmathisen <>2008-12-02 00:02:51 +0000
committercarlgmathisen <>2008-12-02 00:02:51 +0000
commit439b0b12dc8f6cbeb769fe4f2c0061ff9d3c9d31 (patch)
tree30046fc52a8c2ae53bad05224c95dbabd0d5238f /framework/TApplication.php
parentc356dfc6660d68f1e7fbde92aaf15c6a94894d4f (diff)
php configuration type
Diffstat (limited to 'framework/TApplication.php')
-rw-r--r--framework/TApplication.php231
1 files changed, 220 insertions, 11 deletions
diff --git a/framework/TApplication.php b/framework/TApplication.php
index 9677d192..d9626179 100644
--- a/framework/TApplication.php
+++ b/framework/TApplication.php
@@ -127,11 +127,28 @@ class TApplication extends TComponent
/**
* Application configuration file name
*/
- const CONFIG_FILE='application.xml';
+ const CONFIG_FILE_XML='application.xml';
/**
* File extension for external config files
*/
- const CONFIG_FILE_EXT='.xml';
+ const CONFIG_FILE_EXT_XML='.xml';
+ /**
+ * Configuration file type, application.xml and config.xml
+ */
+ const CONFIG_TYPE_XML = 'xml';
+ /**
+ * Application configuration file name
+ */
+ const CONFIG_FILE_PHP='application.php';
+ /**
+ * File extension for external config files
+ */
+ const CONFIG_FILE_EXT_PHP='.php';
+ /**
+ * Configuration file type, application.php and config.php
+ */
+ const CONFIG_TYPE_PHP = 'php';
+
/**
* Runtime directory name
*/
@@ -201,6 +218,14 @@ class TApplication extends TComponent
*/
private $_configFile;
/**
+ * @var string configuration file extension
+ */
+ private $_configFileExt;
+ /**
+ * @var string configuration type
+ */
+ private $_configType;
+ /**
* @var string application base path
*/
private $_basePath;
@@ -287,11 +312,11 @@ class TApplication extends TComponent
* @param boolean whether to cache application configuration. Defaults to true.
* @throws TConfigurationException if configuration file cannot be read or the runtime path is invalid.
*/
- public function __construct($basePath='protected',$cacheConfig=true)
+ public function __construct($basePath='protected',$cacheConfig=true, $configType=self::CONFIG_TYPE_XML)
{
// register application as a singleton
Prado::setApplication($this);
-
+ $this->setConfigurationType($configType);
$this->resolvePaths($basePath);
if($cacheConfig)
@@ -319,8 +344,8 @@ class TApplication extends TComponent
// determine configuration path and file
if(empty($basePath) || ($basePath=realpath($basePath))===false)
throw new TConfigurationException('application_basepath_invalid',$basePath);
- if(is_file($basePath.DIRECTORY_SEPARATOR.self::CONFIG_FILE))
- $configFile=$basePath.DIRECTORY_SEPARATOR.self::CONFIG_FILE;
+ if(is_file($basePath.DIRECTORY_SEPARATOR.$this->getConfigurationFileName()))
+ $configFile=$basePath.DIRECTORY_SEPARATOR.$this->getConfigurationFileName();
else if(is_file($basePath))
{
$configFile=$basePath;
@@ -541,6 +566,49 @@ class TApplication extends TComponent
$this->_configFile=$value;
}
+ public function getConfigurationType()
+ {
+ return $this->_configType;
+ }
+
+ public function setConfigurationType($value)
+ {
+ $this->_configType = $value;
+ }
+
+ public function getConfigurationFileExt()
+ {
+ if($this->_configFileExt===null)
+ {
+ switch($this->_configType)
+ {
+ case TApplication::CONFIG_TYPE_PHP:
+ $this->_configFileExt = TApplication::CONFIG_FILE_EXT_PHP;
+ break;
+ default:
+ $this->_configFileExt = TApplication::CONFIG_FILE_EXT_XML;
+ }
+ }
+ return $this->_configFileExt;
+ }
+
+ public function getConfigurationFileName()
+ {
+ static $fileName;
+ if($fileName == null)
+ {
+ switch($this->_configType)
+ {
+ case TApplication::CONFIG_TYPE_PHP:
+ $fileName = TApplication::CONFIG_FILE_PHP;
+ break;
+ default:
+ $fileName = TApplication::CONFIG_FILE_XML;
+ }
+ }
+ return $fileName;
+ }
+
/**
* @return string the directory storing cache data and application-level persistent data. (absolute path)
*/
@@ -890,7 +958,7 @@ class TApplication extends TComponent
$condition=$this->evaluateExpression($condition);
if($condition)
{
- if(($path=Prado::getPathOfNamespace($filePath,self::CONFIG_FILE_EXT))===null || !is_file($path))
+ if(($path=Prado::getPathOfNamespace($filePath,$this->getConfigurationFileExt()))===null || !is_file($path))
throw new TConfigurationException('application_includefile_invalid',$filePath);
$c=new TApplicationConfiguration;
$c->loadFromFile($path);
@@ -958,7 +1026,10 @@ class TApplication extends TComponent
if($configElement!==null)
{
$config=new TApplicationConfiguration;
- $config->loadFromXml($configElement,$this->getBasePath());
+ if($this->getConfigurationType()==self::CONFIG_TYPE_PHP)
+ $config->loadFromPhp($configElement,$this->getBasePath());
+ else
+ $config->loadFromXml($configElement,$this->getBasePath());
$this->applyConfiguration($config,true);
}
@@ -1188,9 +1259,17 @@ class TApplicationConfiguration extends TComponent
*/
public function loadFromFile($fname)
{
- $dom=new TXmlDocument;
- $dom->loadFromFile($fname);
- $this->loadFromXml($dom,dirname($fname));
+ if(Prado::getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP)
+ {
+ $fcontent = include $fname;
+ $this->loadFromPhp($fcontent,dirname($fname));
+ }
+ else
+ {
+ $dom=new TXmlDocument;
+ $dom->loadFromFile($fname);
+ $this->loadFromXml($dom,dirname($fname));
+ }
}
/**
@@ -1201,6 +1280,34 @@ class TApplicationConfiguration extends TComponent
return $this->_empty;
}
+ public function loadFromPhp($config, $configPath)
+ {
+ // application properties
+ if(isset($config['application']))
+ {
+ foreach($config['application'] as $name=>$value)
+ {
+ $this->_properties[$name]=$value;
+ }
+ $this->_empty = false;
+ }
+
+ if(isset($config['paths']) && is_array($config['paths']))
+ $this->loadPathsPhp($config['paths'],$configPath);
+
+ if(isset($config['modules']) && is_array($config['modules']))
+ $this->loadModulesPhp($config['modules'],$configPath);
+
+ if(isset($config['services']) && is_array($config['services']))
+ $this->loadServicesPhp($config['services'],$configPath);
+
+ if(isset($config['parameters']) && is_array($config['parameters']))
+ $this->loadParametersPhp($config['parameters'], $configPath);
+
+ if(isset($config['includes']) && is_array($config['includes']))
+ $this->loadExternalXml($config['includes'],$configPath);
+ }
+
/**
* Parses the application configuration given in terms of a TXmlElement.
* @param TXmlElement the XML element
@@ -1241,6 +1348,34 @@ class TApplicationConfiguration extends TComponent
}
}
+ protected function loadPathsPhp($pathsNode, $configPath)
+ {
+ if(isset($pathsNode['aliases']) && is_array($pathsNode['aliases']))
+ {
+ foreach($pathsNode['aliases'] as $id=>$path)
+ {
+ $path=str_replace('\\','/',$path);
+ if(preg_match('/^\\/|.:\\/|.:\\\\/',$path)) // if absolute path
+ $p=realpath($path);
+ else
+ $p=realpath($configPath.DIRECTORY_SEPARATOR.$path);
+ if($p===false || !is_dir($p))
+ throw new TConfigurationException('appconfig_aliaspath_invalid',$id,$path);
+ if(isset($this->_aliases[$id]))
+ throw new TConfigurationException('appconfig_alias_redefined',$id);
+ $this->_aliases[$id]=$p;
+ }
+ }
+
+ if(isset($pathsNode['using']) && is_array($pathsNode['using']))
+ {
+ foreach($pathsNode['using'] as $namespace)
+ {
+ $this->_usings[] = $namespace;
+ }
+ }
+ }
+
/**
* Loads the paths XML node.
* @param TXmlElement the paths XML node
@@ -1287,6 +1422,26 @@ class TApplicationConfiguration extends TComponent
}
}
+ protected function loadModulesPhp($modulesNode, $configPath)
+ {
+ foreach($modulesNode as $id=>$module)
+ {
+ if(!isset($module['class']))
+ throw new TConfigurationException('appconfig_moduletype_required',$id);
+ $type = $module['class'];
+ unset($module['class']);
+ $properties = array();
+ if(isset($module['properties']))
+ {
+ $properties = $module['properties'];
+ unset($module['properties']);
+ }
+ $properties['id'] = $id;
+ $this->_modules[$id]=array($type,$properties,$module);
+ $this->_empty=false;
+ }
+ }
+
/**
* Loads the modules XML node.
* @param TXmlElement the modules XML node
@@ -1315,6 +1470,22 @@ class TApplicationConfiguration extends TComponent
}
}
+ protected function loadServicesPhp($servicesNode,$configPath)
+ {
+ foreach($servicesNode as $id => $service)
+ {
+ if(!isset($service['class']))
+ throw new TConfigurationException('appconfig_servicetype_required');
+ $type = $service['class'];
+ unset($service['class']);
+ $properties = isset($service['properties']) ? $service['properties'] : array();
+ unset($service['properties']);
+ $properties['id'] = $id;
+ $this->_services[$id] = array($type,$properties,$service);
+ $this->_empty = false;
+ }
+ }
+
/**
* Loads the services XML node.
* @param TXmlElement the services XML node
@@ -1340,6 +1511,28 @@ class TApplicationConfiguration extends TComponent
}
}
+ protected function loadParametersPhp($parametersNode,$configPath)
+ {
+ foreach($parametersNode as $id => $parameter)
+ {
+ if(is_array($parameter))
+ {
+ if(isset($parameter['class']))
+ {
+ $type = $parameter['class'];
+ unset($parameter['class']);
+ $properties = isset($service['properties']) ? $service['properties'] : array();
+ $properties['id'] = $id;
+ $this->_parameters[$id] = array($type,$properties);
+ }
+ }
+ else
+ {
+ $this->_parameters[$id] = $parameter;
+ }
+ }
+ }
+
/**
* Loads the parameters XML node.
* @param TXmlElement the parameters XML node
@@ -1370,6 +1563,22 @@ class TApplicationConfiguration extends TComponent
}
}
+ protected function loadExternalPhp($includeNode,$configPath)
+ {
+ foreach($includeNode as $include)
+ {
+ $when = isset($include['when'])?true:false;
+ if(!isset($include['file']))
+ throw new TConfigurationException('appconfig_includefile_required');
+ $filePath = $include['file'];
+ if(isset($this->_includes[$filePath]))
+ $this->_includes[$filePath]='('.$this->_includes[$filePath].') || ('.$when.')';
+ else
+ $$this->_includes[$filePath]=$when;
+ $this->_empty=false;
+ }
+ }
+
/**
* Loads the external XML configurations.
* @param TXmlElement the application DOM element