From 1dc53969d1e4db7248db9892e10f0cad25b5205d Mon Sep 17 00:00:00 2001 From: xue <> Date: Sun, 19 Aug 2007 21:16:57 +0000 Subject: refactored app config loading code. --- framework/Exceptions/messages.txt | 5 ++ framework/TApplication.php | 180 ++++++++++++++++++++++++++------------ 2 files changed, 130 insertions(+), 55 deletions(-) diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt index f4b5678f..6fc7360a 100644 --- a/framework/Exceptions/messages.txt +++ b/framework/Exceptions/messages.txt @@ -44,6 +44,11 @@ appconfig_serviceid_required = Application configuration element mus appconfig_servicetype_required = Application configuration must have a "class" attribute. appconfig_parameterid_required = Application configuration element must have an "id" attribute. appconfig_includefile_required = Application configuration element must have a "file" attribute. +appconfig_paths_invalid = Application configuration cannot contain element <{0}>. +appconfig_modules_invalid = Application configuration cannot contain element <{0}>. +appconfig_services_invalid = Application configuration cannot contain element <{0}>. +appconfig_parameters_invalid = Application configuration cannot contain element <{0}>. +appconfig_tag_invalid = Application configuration cannot contain element <{0}>. securitymanager_validationkey_invalid = TSecurityManager.ValidationKey must not be empty. securitymanager_encryptionkey_invalid = TSecurityManager.EncryptionKey must not be empty. diff --git a/framework/TApplication.php b/framework/TApplication.php index 5ae86a39..4f05fd42 100644 --- a/framework/TApplication.php +++ b/framework/TApplication.php @@ -1215,85 +1215,149 @@ class TApplicationConfiguration extends TComponent $this->_empty=false; } - // paths - if(($pathsNode=$dom->getElementByTagName('paths'))!==null) + foreach($dom->getElements() as $element) { - foreach($pathsNode->getElementsByTagName('alias') as $aliasNode) + switch($element->getTagName()) { - if(($id=$aliasNode->getAttribute('id'))!==null && ($path=$aliasNode->getAttribute('path'))!==null) + case 'paths': + $this->loadPathsXml($element,$configPath); + break; + case 'modules': + $this->loadModulesXml($element,$configPath); + break; + case 'services': + $this->loadServicesXml($element,$configPath); + break; + case 'parameters': + $this->loadParametersXml($element,$configPath); + break; + case 'include': + $this->loadExternalXml($element,$configPath); + break; + default: + //throw new TConfigurationException('appconfig_tag_invalid',$element->getTagName()); + break; + } + } + } + + /** + * Loads the paths XML node. + * @param TXmlElement the paths XML node + * @param string the context path (for specifying relative paths) + */ + protected function loadPathsXml($pathsNode,$configPath) + { + foreach($pathsNode->getElements() as $element) + { + switch($element->getTagName()) + { + case 'alias': { - $path=str_replace('\\','/',$path); - if(preg_match('/^\\/|.:\\/|.:\\\\/',$path)) // if absolute path - $p=realpath($path); + if(($id=$element->getAttribute('id'))!==null && ($path=$element->getAttribute('path'))!==null) + { + $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; + } 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; + throw new TConfigurationException('appconfig_alias_invalid'); + $this->_empty=false; + break; } - else - throw new TConfigurationException('appconfig_alias_invalid'); - $this->_empty=false; - } - foreach($pathsNode->getElementsByTagName('using') as $usingNode) - { - if(($namespace=$usingNode->getAttribute('namespace'))!==null) - $this->_usings[]=$namespace; - else - throw new TConfigurationException('appconfig_using_invalid'); - $this->_empty=false; + case 'using': + { + if(($namespace=$element->getAttribute('namespace'))!==null) + $this->_usings[]=$namespace; + else + throw new TConfigurationException('appconfig_using_invalid'); + $this->_empty=false; + break; + } + default: + throw new TConfigurationException('appconfig_paths_invalid',$tagName); } } + } - // application modules - if(($modulesNode=$dom->getElementByTagName('modules'))!==null) + /** + * Loads the modules XML node. + * @param TXmlElement the modules XML node + * @param string the context path (for specifying relative paths) + */ + protected function loadModulesXml($modulesNode,$configPath) + { + foreach($modulesNode->getElements() as $element) { - foreach($modulesNode->getElementsByTagName('module') as $node) + if($element->getTagName()==='module') { - $properties=$node->getAttributes(); + $properties=$element->getAttributes(); $id=$properties->itemAt('id'); $type=$properties->remove('class'); if($type===null) throw new TConfigurationException('appconfig_moduletype_required',$id); - $node->setParent(null); + $element->setParent(null); if($id===null) - $this->_modules[]=array($type,$properties->toArray(),$node); + $this->_modules[]=array($type,$properties->toArray(),$element); else - $this->_modules[$id]=array($type,$properties->toArray(),$node); + $this->_modules[$id]=array($type,$properties->toArray(),$element); $this->_empty=false; } + else + throw new TConfigurationException('appconfig_modules_invalid',$element->getTagName()); } + } - // services - if(($servicesNode=$dom->getElementByTagName('services'))!==null) + /** + * Loads the services XML node. + * @param TXmlElement the services XML node + * @param string the context path (for specifying relative paths) + */ + protected function loadServicesXml($servicesNode,$configPath) + { + foreach($servicesNode->getElements() as $element) { - foreach($servicesNode->getElementsByTagName('service') as $node) + if($element->getTagName()==='service') { - $properties=$node->getAttributes(); + $properties=$element->getAttributes(); if(($id=$properties->itemAt('id'))===null) throw new TConfigurationException('appconfig_serviceid_required'); if(($type=$properties->remove('class'))===null) throw new TConfigurationException('appconfig_servicetype_required',$id); - $node->setParent(null); - $this->_services[$id]=array($type,$properties->toArray(),$node); + $element->setParent(null); + $this->_services[$id]=array($type,$properties->toArray(),$element); $this->_empty=false; } + else + throw new TConfigurationException('appconfig_services_invalid',$element->getTagName()); } + } - // parameters - if(($parametersNode=$dom->getElementByTagName('parameters'))!==null) + /** + * Loads the parameters XML node. + * @param TXmlElement the parameters XML node + * @param string the context path (for specifying relative paths) + */ + protected function loadParametersXml($parametersNode,$configPath) + { + foreach($parametersNode->getElements() as $element) { - foreach($parametersNode->getElementsByTagName('parameter') as $node) + if($element->getTagName()==='parameters') { - $properties=$node->getAttributes(); + $properties=$element->getAttributes(); if(($id=$properties->remove('id'))===null) throw new TConfigurationException('appconfig_parameterid_required'); if(($type=$properties->remove('class'))===null) { if(($value=$properties->remove('value'))===null) - $this->_parameters[$id]=$node; + $this->_parameters[$id]=$element; else $this->_parameters[$id]=$value; } @@ -1301,23 +1365,29 @@ class TApplicationConfiguration extends TComponent $this->_parameters[$id]=array($type,$properties->toArray()); $this->_empty=false; } - } - - // external configurations - foreach($dom->getElementsByTagName('include') as $node) - { - if(($when=$node->getAttribute('when'))===null) - $when=true; - if(($filePath=$node->getAttribute('file'))===null) - throw new TConfigurationException('appconfig_includefile_required'); - if(isset($this->_includes[$filePath])) - $this->_includes[$filePath]='('.$this->_includes[$filePath].') || ('.$when.')'; else - $this->_includes[$filePath]=$when; - $this->_empty=false; + throw new TConfigurationException('appconfig_parameters_invalid',$element->getTagName()); } } + /** + * Loads the external XML configurations. + * @param TXmlElement the application DOM element + * @param string the context path (for specifying relative paths) + */ + protected function loadExternalXml($includeNode,$configPath) + { + if(($when=$includeNode->getAttribute('when'))===null) + $when=true; + if(($filePath=$includeNode->getAttribute('file'))===null) + throw new TConfigurationException('appconfig_includefile_required'); + if(isset($this->_includes[$filePath])) + $this->_includes[$filePath]='('.$this->_includes[$filePath].') || ('.$when.')'; + else + $this->_includes[$filePath]=$when; + $this->_empty=false; + } + /** * Returns list of page initial property values. * Each array element represents a single property with the key -- cgit v1.2.3