From 9c1e1d5efa7ebef6596e4d2dd8a42892648c8e1b Mon Sep 17 00:00:00 2001 From: xue <> Date: Fri, 23 Dec 2005 17:52:51 +0000 Subject: --- framework/TApplication.php | 41 ++++++++++---------- framework/Web/Services/TPageService.php | 66 ++++++++++++++++++++------------- framework/Web/THttpRequest.php | 40 +++++++++++++++----- 3 files changed, 92 insertions(+), 55 deletions(-) (limited to 'framework') diff --git a/framework/TApplication.php b/framework/TApplication.php index 8455c632..97c9f8e3 100644 --- a/framework/TApplication.php +++ b/framework/TApplication.php @@ -114,14 +114,10 @@ class TApplication extends TComponent const STATE_NORMAL='Normal'; const STATE_PERFORMANCE='Performance'; - /** - * Default service ID - */ - const DEFAULT_SERVICE='page'; /** * Page service ID */ - const PAGE_SERVICE='page'; + const PAGE_SERVICE_ID='page'; /** * Application configuration file name */ @@ -252,9 +248,8 @@ class TApplication extends TComponent * Initializes the application singleton. This method ensures that users can * only create one application instance. * @param string configuration file path (absolute or relative to current running script) - * @param string a directory used to store application-level persistent data. Defaults to the path having the application configuration file. * @param boolean whether to cache application configuration. Defaults to true. - * @throws TConfigurationException if configuration file cannot be read or the state path is invalid. + * @throws TConfigurationException if configuration file cannot be read or the runtime path is invalid. */ public function __construct($configPath=null,$cacheConfig=true) { @@ -706,12 +701,8 @@ class TApplication extends TComponent if($this->_configFile===null) { - if(($serviceID=$this->getRequest()->getServiceID())===null) - $serviceID=self::DEFAULT_SERVICE; - if($serviceID===self::PAGE_SERVICE) - $this->_service=$this->getPageService(); - else - throw new THttpException(500,'application_service_unknown',$serviceID); + $this->getRequest()->setAvailableServices(array(self::PAGE_SERVICE_ID)); + $this->_service=$this->getPageService(); return; } @@ -771,23 +762,28 @@ class TApplication extends TComponent $module->init($this,$moduleConfig[2]); } - if(($serviceID=$this->getRequest()->getServiceID())===null) - $serviceID=self::DEFAULT_SERVICE; + // load service + $services=$config->getServices(); + $serviceIDs=array_keys($services); + array_unshift($serviceIDs,self::PAGE_SERVICE_ID); + $request=$this->getRequest(); + $request->setAvailableServices($serviceIDs); - if(($serviceConfig=$config->getService($serviceID))!==null) + if(($serviceID=$request->getServiceID())===null) + $serviceID=self::PAGE_SERVICE_ID; + if(isset($services[$serviceID])) { + $serviceConfig=$services[$serviceID]; $service=Prado::createComponent($serviceConfig[0]); if(!($service instanceof IService)) - throw new TConfigurationException('application_service_invalid',$serviceID); + throw new THttpException(500,'application_service_unknown',$serviceID); $this->_service=$service; foreach($serviceConfig[1] as $name=>$value) $service->setSubProperty($name,$value); $service->init($this,$serviceConfig[2]); } - else if($serviceID===self::DEFAULT_SERVICE) - $this->_service=$this->getPageService(); else - throw new THttpException(500,'application_service_unknown',$serviceID); + $this->_service=$this->getPageService(); } /** @@ -1113,6 +1109,11 @@ class TApplicationConfiguration extends TComponent return isset($this->_services[$id])?$this->_services[$id]:null; } + public function getServices() + { + return $this->_services; + } + /** * @return array list of parameters */ diff --git a/framework/Web/Services/TPageService.php b/framework/Web/Services/TPageService.php index 485346e2..53ce9c58 100644 --- a/framework/Web/Services/TPageService.php +++ b/framework/Web/Services/TPageService.php @@ -27,14 +27,19 @@ Prado::using('System.Web.UI.TPageStatePersister'); * Pages that are available to client users are stored under a directory specified by * {@link setBasePath BasePath}. The directory may contain subdirectories. * A directory may be used to group together the pages serving for the similar goal. - * Each directory must contain a configuration file config.xml that is similar to the application - * configuration file. The only difference is that the page directory configuration - * contains a mapping between page IDs and page types. The page IDs are visible - * by client users while page types are used on the server side. + * A directory may contain a configuration file config.xml whose content + * is similar to that of application configuration file. + * * A page is requested via page path, which is a dot-connected directory names - * appended by the page ID. Assume '/Users/Admin' is the directory - * containing the page 'Update' whose type is UpdateUserPage. Then the page can - * be requested via 'Users.Admin.Update'. + * appended by the page name. Assume '/Users/Admin' is the directory + * containing the page 'Update'. Then the page can be requested via 'Users.Admin.Update'. + * + * Page name refers to the file name (without extension) of the page template. + * In order to differentiate from the common control template files, the extension + * name of the page template files must be '.page'. If there is a PHP file with + * the same page name under the same directory as the template file, that file + * will be considered as the page class file and the file name is the page class name. + * If such a file is not found, the page class is assumed as {@link TPage}. * * Modules can be configured and loaded in page directory configurations. * Configuration of a module in a subdirectory will overwrite its parent @@ -170,42 +175,53 @@ class TPageService extends TComponent implements IService else { $configCached=true; + $currentTimestamp=array(); $arr=$cache->get(self::CONFIG_CACHE_PREFIX.$this->_pagePath); if(is_array($arr)) { - list($pageConfig,$timestamp)=$arr; + list($pageConfig,$timestamps)=$arr; if($application->getMode()!==TApplication::STATE_PERFORMANCE) { - // check to see if cache is the latest - $paths=explode('.',$this->_pagePath); - array_pop($paths); - $configPath=$this->_basePath; - foreach($paths as $path) + foreach($timestamps as $fileName=>$timestamp) { - if(@filemtime($configPath.'/'.self::CONFIG_FILE)>$timestamp) + if($fileName===0) // application config file { - $configCached=false; - break; + $appConfigFile=$application->getConfigurationFile(); + $currentTimestamp[0]=$appConfigFile===null?0:@filemtime($appConfigFile); + if($currentTimestamp[0]>$timestamp || ($timestamp>0 && !$currentTimestamp[0])) + $configCached=false; + } + else + { + $currentTimestamp[$fileName]=@filemtime($fileName); + if($currentTimestamp[$fileName]>$timestamp || ($timestamp>0 && !$currentTimestamp[$fileName])) + $configCached=false; } - $configPath.='/'.$path; - } - if($configCached) - { - $appConfig=$application->getConfigurationFile(); - if(!$appConfig || @filemtime($appConfig)>$timestamp || @filemtime($configPath.'/'.self::CONFIG_FILE)>$timestamp) - $configCached=false; } } } else + { $configCached=false; + $paths=explode('.',$this->_pagePath); + array_pop($paths); + $configPath=$this->_basePath; + foreach($paths as $path) + { + $configFile=$configPath.'/'.self::CONFIG_FILE; + $currentTimestamp[$configFile]=@filemtime($configFile); + $configPath.='/'.$path; + } + $appConfigFile=$application->getConfigurationFile(); + $currentTimestamp[0]=$appConfigFile===null?0:@filemtime($appConfigFile); + } if(!$configCached) { $pageConfig=new TPageConfiguration; if($config!==null) $pageConfig->loadXmlElement($config,$application->getConfigurationPath(),null); $pageConfig->loadConfigurationFiles($this->_pagePath,$this->_basePath); - $cache->set(self::CONFIG_CACHE_PREFIX.$this->_pagePath,array($pageConfig,time())); + $cache->set(self::CONFIG_CACHE_PREFIX.$this->_pagePath,array($pageConfig,$currentTimestamp)); } } @@ -738,6 +754,4 @@ class TPageConfiguration extends TComponent } } - - ?> \ No newline at end of file diff --git a/framework/Web/THttpRequest.php b/framework/Web/THttpRequest.php index 938e56a3..d42035df 100644 --- a/framework/Web/THttpRequest.php +++ b/framework/Web/THttpRequest.php @@ -64,6 +64,9 @@ class THttpRequest extends TModule */ private $_items; + private $_services; + private $_requestResolved=false; + /** * Initializes the module. * This method is required by IModule and is invoked by application. @@ -105,7 +108,6 @@ class THttpRequest extends TModule $this->_items=new TMap(array_merge($_POST,$_GET)); - $this->resolveRequest(); $this->_initialized=true; $application->setRequest($this); } @@ -338,9 +340,9 @@ class THttpRequest extends TModule public function constructUrl($serviceID,$serviceParam,$getItems=null) { $url=$this->getApplicationPath(); - $url.='?'.self::SERVICE_VAR.'='.$serviceID; + $url.='?'.$serviceID.'='; if(!empty($serviceParam)) - $url.='.'.$serviceParam; + $url.=$serviceParam; if(is_array($getItems) || $getItems instanceof Traversable) { foreach($getItems as $name=>$value) @@ -361,23 +363,41 @@ class THttpRequest extends TModule */ protected function resolveRequest() { - if(($sp=$this->_items->itemAt(self::SERVICE_VAR))!==null) + $this->_requestResolved=true; + foreach($this->_services as $id) { - if(($pos=strpos($sp,'.'))===false) - $this->setServiceID($sp); - else + if(isset($_GET[$id])) { - $this->setServiceID(substr($sp,0,$pos)); - $this->setServiceParameter(substr($sp,$pos+1)); + $this->setServiceID($id); + $this->setServiceParameter($_GET[$id]); + break; } } } + /** + * @return array IDs of the available services + */ + public function getAvailableServices() + { + return $this->_services; + } + + /** + * @param array IDs of the available services + */ + public function setAvailableServices($services) + { + $this->_services=$services; + } + /** * @return string requested service ID */ public function getServiceID() { + if(!$this->_requestResolved) + $this->resolveRequest(); return $this->_serviceID; } @@ -395,6 +415,8 @@ class THttpRequest extends TModule */ public function getServiceParameter() { + if(!$this->_requestResolved) + $this->resolveRequest(); return $this->_serviceParam; } -- cgit v1.2.3