diff options
author | xue <> | 2005-12-23 03:31:07 +0000 |
---|---|---|
committer | xue <> | 2005-12-23 03:31:07 +0000 |
commit | 153581e1081ba4225eb93221aced493709cb606c (patch) | |
tree | 7b2a165708d6fb8e587749e3e0fe1f813c2011aa /framework/TApplication.php | |
parent | 5c0517b7748dcfae1264d28df7ea111a67bd3aa4 (diff) |
Implemented new page storage method.
Diffstat (limited to 'framework/TApplication.php')
-rw-r--r-- | framework/TApplication.php | 124 |
1 files changed, 100 insertions, 24 deletions
diff --git a/framework/TApplication.php b/framework/TApplication.php index 18d54c9c..8455c632 100644 --- a/framework/TApplication.php +++ b/framework/TApplication.php @@ -119,6 +119,18 @@ class TApplication extends TComponent */ const DEFAULT_SERVICE='page'; /** + * Page service ID + */ + const PAGE_SERVICE='page'; + /** + * Application configuration file name + */ + const CONFIG_FILE='application.xml'; + /** + * Runtime directory name + */ + const RUNTIME_PATH='runtime'; + /** * Config cache file */ const CONFIGCACHE_FILE='config.cache'; @@ -165,7 +177,11 @@ class TApplication extends TComponent /** * @var IService current service instance */ - private $_service; + private $_service=null; + /** + * @var TPageService page service + */ + private $_pageService=null; /** * @var array list of application modules */ @@ -179,9 +195,13 @@ class TApplication extends TComponent */ private $_configFile; /** + * @var string configuration path + */ + private $_configPath; + /** * @var string directory storing application state */ - private $_statePath; + private $_runtimePath; /** * @var boolean if any global state is changed during the current request */ @@ -236,22 +256,36 @@ class TApplication extends TComponent * @param boolean whether to cache application configuration. Defaults to true. * @throws TConfigurationException if configuration file cannot be read or the state path is invalid. */ - public function __construct($configFile,$statePath=null,$cacheConfig=true) + public function __construct($configPath=null,$cacheConfig=true) { parent::__construct(); + + // register application as a singleton Prado::setApplication($this); - if(($this->_configFile=realpath($configFile))===false || !is_file($this->_configFile)) - throw new TConfigurationException('application_configfile_inexistent',$configFile); - if($statePath===null) - $this->_statePath=dirname($this->_configFile); - else if(is_dir($statePath) && is_writable($statePath)) - $this->_statePath=$statePath; + + // determine configuration path and file + if($configPath===null) + $configPath=dirname($_SERVER['SCRIPT_FILENAME']).'/protected'; + if(($this->_configPath=realpath($configPath))===false) + throw new TConfigurationException('application_configpath_inexistent',$configPath); + if(is_dir($this->_configPath)) + { + $configFile=$this->_configPath.'/'.self::CONFIG_FILE; + $this->_configFile=is_file($configFile) ? $configFile : null; + } else - throw new TConfigurationException('application_statepath_invalid',$statePath); - $this->_cacheFile=$cacheConfig ? $this->_statePath.'/'.self::CONFIGCACHE_FILE : null; + { + $this->_configFile=$this->_configPath; + $this->_configPath=dirname($this->_configPath); + } + $this->_runtimePath=$this->_configPath.'/'.self::RUNTIME_PATH; + if(!is_dir($this->_runtimePath) || !is_writable($this->_runtimePath)) + throw new TConfigurationException('application_runtimepath_invalid',$this->_runtimePath); - // generates unique ID by hashing the configuration file path - $this->_uniqueID=md5($this->_configFile); + $this->_cacheFile=$cacheConfig ? $this->_runtimePath.'/'.self::CONFIGCACHE_FILE : null; + + // generates unique ID by hashing the configuration path + $this->_uniqueID=md5($this->_configPath); } @@ -270,7 +304,7 @@ class TApplication extends TComponent $this->_requestCompleted=false; while($this->_step<$n) { - if($this->_mode==='Off') + if($this->_mode===self::STATE_OFF) throw new THttpException(503,'application_service_unavailable'); $method='on'.self::$_steps[$this->_step]; $this->$method($this); @@ -365,7 +399,7 @@ class TApplication extends TComponent $this->_globals=$value; else { - if(($content=@file_get_contents($this->getStatePath().'/'.self::GLOBAL_FILE))!==false) + if(($content=@file_get_contents($this->getRuntimePath().'/'.self::GLOBAL_FILE))!==false) $this->_globals=unserialize($content); } } @@ -389,7 +423,7 @@ class TApplication extends TComponent } if($saveFile) { - $fileName=$this->getStatePath().'/'.self::GLOBAL_FILE; + $fileName=$this->getRuntimePath().'/'.self::GLOBAL_FILE; if(version_compare(phpversion(),'5.1.0','>=')) file_put_contents($fileName,$content,LOCK_EX); else @@ -438,6 +472,14 @@ class TApplication extends TComponent } /** + * @return string configuration path + */ + public function getConfigurationPath() + { + return $this->_configPath; + } + + /** * @return string configuration file path */ public function getConfigurationFile() @@ -449,9 +491,9 @@ class TApplication extends TComponent * Gets the directory storing application-level persistent data. * @return string application state path */ - public function getStatePath() + public function getRuntimePath() { - return $this->_statePath; + return $this->_runtimePath; } /** @@ -501,6 +543,29 @@ class TApplication extends TComponent } /** + * @return TPageService page service + */ + public function getPageService() + { + if(!$this->_pageService) + { + $this->_pageService=new TPageService; + $this->_pageService->init($this,null); + } + return $this->_pageService; + } + + /** + * Registers the page service instance. + * This method should only be used by framework developers. + * @param TPageService page service + */ + public function setPageService(TPageService $service) + { + $this->_pageService=$service; + } + + /** * @return THttpRequest the request module */ public function getRequest() @@ -637,6 +702,19 @@ class TApplication extends TComponent */ protected function initApplication() { + Prado::setPathOfAlias('Application',$this->_configPath); + + 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); + return; + } + if($this->_cacheFile===null || @filemtime($this->_cacheFile)<filemtime($this->_configFile)) { $config=new TApplicationConfiguration; @@ -657,7 +735,6 @@ class TApplication extends TComponent $config=Prado::unserialize(file_get_contents($this->_cacheFile)); } - // set path aliases and using namespaces foreach($config->getAliases() as $alias=>$path) Prado::setPathOfAlias($alias,$path); @@ -706,10 +783,11 @@ class TApplication extends TComponent foreach($serviceConfig[1] as $name=>$value) $service->setSubProperty($name,$value); $service->init($this,$serviceConfig[2]); - $this->attachEventHandler('RunService',array($service,'run')); } + else if($serviceID===self::DEFAULT_SERVICE) + $this->_service=$this->getPageService(); else - throw new TConfigurationException('application_service_unknown',$serviceID); + throw new THttpException(500,'application_service_unknown',$serviceID); } /** @@ -894,9 +972,7 @@ class TApplicationConfiguration extends TComponent /** * @var array list of service configurations */ - private $_services=array( - 'page'=>array('TPageService',array(),null) - ); + private $_services=array(); /** * @var array list of parameters */ |