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/Web/Services/TFeedService.php | 86 +++++++++++++++------ framework/Web/Services/TJsonService.php | 76 ++++++++++++++---- framework/Web/Services/TPageService.php | 10 ++- framework/Web/Services/TSoapService.php | 25 ++++-- framework/Web/TUrlMapping.php | 132 +++++++++++++++++++------------- 5 files changed, 229 insertions(+), 100 deletions(-) (limited to 'framework/Web') diff --git a/framework/Web/Services/TFeedService.php b/framework/Web/Services/TFeedService.php index 7ecd10a7..d4afbade 100644 --- a/framework/Web/Services/TFeedService.php +++ b/framework/Web/Services/TFeedService.php @@ -5,7 +5,7 @@ * @author Qiang Xue * @author Knut Urdalen * @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.Web.Services @@ -28,6 +28,20 @@ * * * where each <feed> element specifies a feed identified by its "id" value (case-sensitive). + * + * PHP configuration style: + * + * array( + * 'feed' => array( + * 'ch1' => array( + * 'class' => 'Path.To.FeedClass1', + * 'properties' => array( + * ... + * ), + * ), + * ) + * + * * The class attribute indicates which PHP class will provide the actual feed * content. Note, the class must implement {@link IFeedContentProvider} interface. * Other initial properties for the feed class may also be specified in the @@ -38,6 +52,7 @@ * * @author Qiang Xue * @author Knut Urdalen + * @author Carl G. Mathisen * @package System.Web.Services * @since 3.1 */ @@ -48,16 +63,27 @@ class TFeedService extends TService /** * 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 */ public function init($config) { - foreach($config->getElementsByTagName('feed') as $feed) + if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) { - if(($id=$feed->getAttributes()->remove('id'))!==null) - $this->_feeds[$id]=$feed; - else - throw new TConfigurationException('feedservice_id_required'); + if(is_array($config)) + { + foreach($config as $id => $feed) + $this->_feeds[$id] = $feed; + } + } + else + { + foreach($config->getElementsByTagName('feed') as $feed) + { + if(($id=$feed->getAttributes()->remove('id'))!==null) + $this->_feeds[$id]=$feed; + else + throw new TConfigurationException('feedservice_id_required'); + } } } @@ -79,27 +105,43 @@ class TFeedService extends TService if(isset($this->_feeds[$id])) { $feedConfig=$this->_feeds[$id]; - $properties=$feedConfig->getAttributes(); - if(($class=$properties->remove('class'))!==null) + $properties = array(); + $feed = null; + if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) { - $feed=Prado::createComponent($class); - if($feed instanceof IFeedContentProvider) + if(isset($feedConfig['class'])) { - // init feed properties - foreach($properties as $name=>$value) - $feed->setSubproperty($name,$value); - $feed->init($feedConfig); - - $content=$feed->getFeedContent(); - //$this->getResponse()->setContentType('application/rss+xml'); - $this->getResponse()->setContentType($feed->getContentType()); - $this->getResponse()->write($content); + $feed=Prado::createComponent($feedConfig['class']); + if($service instanceof IFeedContentProvider) + $properties=isset($feedConfig['properties'])?$feedConfig['properties']:array(); + else + throw new TConfigurationException('jsonservice_response_type_invalid',$id); } else - throw new TConfigurationException('feedservice_feedtype_invalid',$id); + throw new TConfigurationException('jsonservice_class_required',$id); } else - throw new TConfigurationException('feedservice_class_required',$id); + { + $properties=$feedConfig->getAttributes(); + if(($class=$properties->remove('class'))!==null) + { + $feed=Prado::createComponent($class); + if(!($feed instanceof IFeedContentProvider)) + throw new TConfigurationException('feedservice_feedtype_invalid',$id); + } + else + throw new TConfigurationException('feedservice_class_required',$id); + } + + // init feed properties + foreach($properties as $name=>$value) + $feed->setSubproperty($name,$value); + $feed->init($feedConfig); + + $content=$feed->getFeedContent(); + //$this->getResponse()->setContentType('application/rss+xml'); + $this->getResponse()->setContentType($feed->getContentType()); + $this->getResponse()->write($content); } else throw new THttpException(404,'feedservice_feed_unknown',$id); diff --git a/framework/Web/Services/TJsonService.php b/framework/Web/Services/TJsonService.php index e3ed9a7f..84f2dee2 100644 --- a/framework/Web/Services/TJsonService.php +++ b/framework/Web/Services/TJsonService.php @@ -29,10 +29,24 @@ * where each JSON response is specified via a <json> element. * Initial property values can be configured in a <json> element. * + * + * PHP configuration style: + * + * 'services' => array( + * 'get_article' => array( + * 'class' => 'Path.To.JsonResponseClass1', + * 'properties' => array( + * ... + * ) + * ) + * ) + * + * * To retrieve the JSON content provided by "get_article", use the URL * index.php?json=get_article * * @author Wei Zhuo + * @author Carl G. Mathisen * @version $Id$ * @package System.Web.Services * @since 3.1 @@ -47,7 +61,7 @@ class TJsonService extends TService /** * 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 */ public function init($xml) { @@ -56,16 +70,27 @@ class TJsonService extends TService /** * Load the service definitions. - * @param TXmlElement configuration for this module, can be null + * @param mixed configuration for this module, can be null */ - protected function loadJsonServices($xml) + protected function loadJsonServices($config) { - foreach($xml->getElementsByTagName('json') as $config) + if($this->getApplication->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) { - if(($id=$config->getAttribute('id'))!==null) - $this->_services[$id]=$config; - else - throw new TConfigurationException('jsonservice_id_required'); + if(is_array($config)) + { + foreach($config as $id => $json) + $this->_services[$id] = $json; + } + } + else + { + foreach($config->getElementsByTagName('json') as $json) + { + if(($id=$json->getAttribute('id'))!==null) + $this->_services[$id]=$config; + else + throw new TConfigurationException('jsonservice_id_required'); + } } } @@ -79,20 +104,39 @@ class TJsonService extends TService if(isset($this->_services[$id])) { $serviceConfig=$this->_services[$id]; - $properties=$serviceConfig->getAttributes(); - if(($class=$properties->remove('class'))!==null) + if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) { - $service=Prado::createComponent($class); - if($service instanceof TJsonResponse) - $this->createJsonResponse($service,$properties,$serviceConfig); + if(isset($serviceConfig['class'])) + { + $service=Prado::createComponent($serviceConfig['class']); + if($service instanceof JsonResponse) + { + $properties = isset($serviceConfig['properties'])?$serviceConfig['properties']:array(); + $this->createJsonResponse($service,$properties,$serviceConfig); + } + else + throw new TConfigurationException('jsonservice_response_type_invalid',$id); + } else - throw new TConfigurationException('jsonservice_response_type_invalid',$id); + throw new TConfigurationException('jsonservice_class_required',$id); } else - throw new TConfigurationException('jsonservice_class_required',$id); + { + $properties=$serviceConfig->getAttributes(); + if(($class=$properties->remove('class'))!==null) + { + $service=Prado::createComponent($class); + if($service instanceof TJsonResponse) + $this->createJsonResponse($service,$properties,$serviceConfig); + else + throw new TConfigurationException('jsonservice_response_type_invalid',$id); + } + else + throw new TConfigurationException('jsonservice_class_required',$id); + } } else - throw new THttpException(404,'jsonservice_provider_unknown',$id); + throw new THttpException(404,'jsonservice_feed_unknown',$id); } /** diff --git a/framework/Web/Services/TPageService.php b/framework/Web/Services/TPageService.php index 10c7da6e..4d08ed4c 100644 --- a/framework/Web/Services/TPageService.php +++ b/framework/Web/Services/TPageService.php @@ -69,6 +69,7 @@ Prado::using('System.Web.UI.TThemeManager'); * accessing to any resources. * * @author Qiang Xue + * @author Carl G. Mathisen * @version $Id$ * @package System.Web.Services * @since 3.0 @@ -274,7 +275,12 @@ class TPageService extends TService { $pageConfig=new TPageConfiguration($pagePath); if($config!==null) - $pageConfig->loadPageConfigurationFromXml($config,$application->getBasePath(),''); + { + if($application->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) + $pageConfig->loadPageConfigurationFromPhp($config,$application->getBasePath(),''); + else + $pageConfig->loadPageConfigurationFromXml($config,$application->getBasePath(),''); + } $pageConfig->loadFromFiles($this->getBasePath()); $cache->set(self::CONFIG_CACHE_PREFIX.$this->getID().$pagePath,array($pageConfig,$currentTimestamp)); } @@ -721,6 +727,7 @@ class TPageConfiguration extends TComponent $rules[]=new TAuthorizationRule($action,$users,$roles,$verb,$ips); } } + $this->_rules=array_merge($rules,$this->_rules); } // pages if(isset($config['pages']) && is_array($config['pages'])) @@ -747,7 +754,6 @@ class TPageConfiguration extends TComponent if($matching) $this->_properties=array_merge($this->_properties,$properties); } - $this->_rules=array_merge($rules,$this->_rules); } // external configurations diff --git a/framework/Web/Services/TSoapService.php b/framework/Web/Services/TSoapService.php index f5962647..d528fe1d 100644 --- a/framework/Web/Services/TSoapService.php +++ b/framework/Web/Services/TSoapService.php @@ -30,12 +30,16 @@ * * * - * - * The above example specifies a single SOAP provider named "stockquote" - * whose class is "MyStockQuote". A SOAP client can then obtain the WSDL for - * this provider via the following URL: + * PHP configuration style: * - * http://hostname/path/to/index.php?soap=stockquote.wsdl + * 'services' => array( + * 'soap' => array( + * 'class' => 'System.Web.Services.TSoapService' + * 'properties' => array( + * 'provider' => 'MyStockQuote' + * ) + * ) + * ) * * * The WSDL for the provider class "MyStockQuote" is generated based on special @@ -79,13 +83,13 @@ * * @author Knut Urdalen * @author Qiang Xue + * @author Carl G. Mathisen * @package System.Web.Services * @since 3.1 */ class TSoapService extends TService { const DEFAULT_SOAP_SERVER='TSoapServer'; - const CONFIG_FILE_EXT='.xml'; private $_servers=array(); private $_configFile=null; private $_wsdlRequest=false; @@ -195,7 +199,7 @@ class TSoapService extends TService */ public function setConfigFile($value) { - if(($this->_configFile=Prado::getPathOfNamespace($value,self::CONFIG_FILE_EXT))===null) + if(($this->_configFile=Prado::getPathOfNamespace($value,Prado::getApplication()->getConfigurationFileExt()))===null) throw new TConfigurationException('soapservice_configfile_invalid',$value); } @@ -237,7 +241,12 @@ class TSoapService extends TService protected function createServer() { $properties=$this->_servers[$this->_serverID]; - if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP || ($serverClass=$properties->remove('class'))===null) + $serverClass=null; + if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP && isset($config['class'])) + $serverClass=$config['class']; + else if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_XML) + $serverClass=$properties->remove('class'); + if($serverClass===null) $serverClass=self::DEFAULT_SOAP_SERVER; Prado::using($serverClass); $className=($pos=strrpos($serverClass,'.'))!==false?substr($serverClass,$pos+1):$serverClass; diff --git a/framework/Web/TUrlMapping.php b/framework/Web/TUrlMapping.php index 83dd99b6..30e62496 100644 --- a/framework/Web/TUrlMapping.php +++ b/framework/Web/TUrlMapping.php @@ -4,7 +4,7 @@ * * @author Wei Zhuo * @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.Web @@ -69,10 +69,6 @@ Prado::using('System.Collections.TAttributeCollection'); */ class TUrlMapping extends TUrlManager { - /** - * File extension of external configuration file - */ - const CONFIG_FILE_EXT='.xml'; /** * @var TUrlMappingPattern[] list of patterns. */ @@ -101,17 +97,17 @@ class TUrlMapping extends TUrlManager /** * 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 module is configured in the global scope. */ - public function init($xml) + public function init($config) { - parent::init($xml); + parent::init($config); if($this->getRequest()->getRequestResolved()) throw new TConfigurationException('urlmapping_global_required'); if($this->_configFile!==null) $this->loadConfigFile(); - $this->loadUrlMappings($xml); + $this->loadUrlMappings($config); if($this->_urlPrefix==='') $this->_urlPrefix=$this->getRequest()->getApplicationUrl(); $this->_urlPrefix=rtrim($this->_urlPrefix,'/'); @@ -125,9 +121,17 @@ class TUrlMapping extends TUrlManager { if(is_file($this->_configFile)) { - $dom=new TXmlDocument; - $dom->loadFromFile($this->_configFile); - $this->loadUrlMappings($dom); + if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) + { + $config = include $this->_configFile; + $this->loadUrlMappings($dom); + } + else + { + $dom=new TXmlDocument; + $dom->loadFromFile($this->_configFile); + $this->loadUrlMappings($dom); + } } else throw new TConfigurationException('urlmapping_configfile_inexistent',$this->_configFile); @@ -191,7 +195,7 @@ class TUrlMapping extends TUrlManager */ 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('urlmapping_configfile_invalid',$value); } @@ -218,28 +222,52 @@ class TUrlMapping extends TUrlManager /** * Load and configure each url mapping pattern. - * @param TXmlElement configuration node + * @param mixed configuration node * @throws TConfigurationException if specific pattern class is invalid */ - protected function loadUrlMappings($xml) + protected function loadUrlMappings($config) { - foreach($xml->getElementsByTagName('url') as $url) + if(is_array($config)) { - $properties=$url->getAttributes(); - if(($class=$properties->remove('class'))===null) - $class=$this->getDefaultMappingClass(); - $pattern=Prado::createComponent($class,$this); - if(!($pattern instanceof TUrlMappingPattern)) - throw new TConfigurationException('urlmapping_urlmappingpattern_required'); - foreach($properties as $name=>$value) - $pattern->setSubproperty($name,$value); - $this->_patterns[]=$pattern; - $pattern->init($url); - - $key=$pattern->getServiceID().':'.$pattern->getServiceParameter(); - $this->_constructRules[$key][]=$pattern; + if(isset($config['urls']) && is_array($config['urls'])) + { + foreach($config['urls'] as $url) + { + $class=null; + if(!isset($url['class'])) + $class=$this->getDefaultMappingClass(); + $pattern=Prado::createComponent($class,$this); + $properties = isset($url['properties'])?$url['properties']:array(); + $this->buildUrlMapping($class,$pattern,$properties,$url); + } + } + } + else + { + foreach($config->getElementsByTagName('url') as $url) + { + $properties=$url->getAttributes(); + if(($class=$properties->remove('class'))===null) + $class=$this->getDefaultMappingClass(); + $pattern=Prado::createComponent($class,$this); + $this->buildUrlMapping($class,$pattern,$properties,$url); + } } } + + private function buildUrlMapping($class, $pattern, $properties, $url) + { + $pattern=Prado::createComponent($class,$this); + if(!($pattern instanceof TUrlMappingPattern)) + throw new TConfigurationException('urlmapping_urlmappingpattern_required'); + foreach($properties as $name=>$value) + $pattern->setSubproperty($name,$value); + $this->_patterns[]=$pattern; + $pattern->init($url); + + $key=$pattern->getServiceID().':'.$pattern->getServiceParameter(); + $this->_constructRules[$key][]=$pattern; + } /** * Parses the request URL and returns an array of input parameters. @@ -264,7 +292,7 @@ class TUrlMapping extends TUrlManager if(is_string($key)) $params[$key]=$value; } - if (!$pattern->getIsWildCardPattern()) + if (!$pattern->getIsWildCardPattern()) $params[$pattern->getServiceID()]=$pattern->getServiceParameter(); return $params; } @@ -300,8 +328,8 @@ class TUrlMapping extends TUrlManager if(!(is_array($getItems) || ($getItems instanceof Traversable))) $getItems=array(); $key=$serviceID.':'.$serviceParam; - $wildCardKey = ($pos=strrpos($serviceParam,'.'))!==false ? - $serviceID.':'.substr($serviceParam,0,$pos).'.*' : $serviceID.':*'; + $wildCardKey = ($pos=strrpos($serviceParam,'.'))!==false ? + $serviceID.':'.substr($serviceParam,0,$pos).'.*' : $serviceID.':*'; if(isset($this->_constructRules[$key])) { foreach($this->_constructRules[$key] as $rule) @@ -309,16 +337,16 @@ class TUrlMapping extends TUrlManager if($rule->supportCustomUrl($getItems)) return $rule->constructUrl($getItems,$encodeAmpersand,$encodeGetItems); } - } - elseif(isset($this->_constructRules[$wildCardKey])) - { + } + elseif(isset($this->_constructRules[$wildCardKey])) + { foreach($this->_constructRules[$wildCardKey] as $rule) { if($rule->supportCustomUrl($getItems)) - { - $getItems['*']= $pos ? substr($serviceParam,$pos+1) : $serviceParam; + { + $getItems['*']= $pos ? substr($serviceParam,$pos+1) : $serviceParam; return $rule->constructUrl($getItems,$encodeAmpersand,$encodeGetItems); - } + } } } } @@ -413,8 +441,8 @@ class TUrlMappingPattern extends TComponent private $_manager; private $_caseSensitive=true; - - private $_isWildCardPattern=false; + + private $_isWildCardPattern=false; /** * Constructor. @@ -444,8 +472,8 @@ class TUrlMappingPattern extends TComponent { if($this->_serviceParameter===null) throw new TConfigurationException('urlmappingpattern_serviceparameter_required', $this->getPattern()); - if(strpos($this->_serviceParameter,'*')!==false) - $this->_isWildCardPattern=true; + if(strpos($this->_serviceParameter,'*')!==false) + $this->_isWildCardPattern=true; } /** @@ -462,11 +490,11 @@ class TUrlMappingPattern extends TComponent $params[]='{'.$key.'}'; $values[]='(?P<'.$key.'>'.$value.')'; } - if ($this->getIsWildCardPattern()) { - $params[]='{*}'; - // service parameter must not contain '=' and '/' - $values[]='(?P<'.$this->getServiceID().'>[^=/]+)'; - } + if ($this->getIsWildCardPattern()) { + $params[]='{*}'; + // service parameter must not contain '=' and '/' + $values[]='(?P<'.$this->getServiceID().'>[^=/]+)'; + } $params[]='/'; $values[]='\\/'; $regexp=str_replace($params,$values,trim($this->getPattern(),'/').'/'); @@ -608,13 +636,13 @@ class TUrlMappingPattern extends TComponent } /** - * @return boolean whether this pattern is a wildcard pattern + * @return boolean whether this pattern is a wildcard pattern * @since 3.1.4 */ - public function getIsWildCardPattern() { - return $this->_isWildCardPattern; - } - + public function getIsWildCardPattern() { + return $this->_isWildCardPattern; + } + /** * @param array list of GET items to be put in the constructed URL * @return boolean whether this pattern IS the one for constructing the URL with the specified GET items. -- cgit v1.2.3