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 +++++++--- 4 files changed, 149 insertions(+), 48 deletions(-) (limited to 'framework/Web/Services') 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; -- cgit v1.2.3