Source for file jCoordinator.class.php
Documentation is available at jCoordinator.class.php
- <?php
- /**
- * @package jelix
- * @subpackage core
- * @author Laurent Jouanneau
- * @contributor Thibault PIRONT < nuKs >, Julien Issler
- * @copyright 2005-2008 laurent Jouanneau
- * @copyright 2007 Thibault PIRONT
- * @copyright 2008 Julien Issler
- * @link http://www.jelix.org
- * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
- */
-
- /**
- * the main class of the jelix core
- *
- * this is the "chief orchestra" of the framework. Its goal is
- * to load the configuration, to get the request parameters
- * used to instancie the correspondant controllers and to run the right method.
- * @package jelix
- * @subpackage core
- */
- class jCoordinator {
-
- /**
- * plugin list
- * @var array
- */
- public $plugins=array();
-
- /**
- * current response object
- * @var jResponse
- */
- public $response = null;
-
- /**
- * current request object
- * @var jRequest
- */
- public $request = null;
-
- /**
- * the selector of the current action
- * @var jSelectorAct
- */
- public $action = null;
-
- /**
- * the current module name
- * @var string
- */
- public $moduleName;
-
- /**
- * the current action name
- * @var string
- */
- public $actionName;
-
- /**
- * List of all errors
- * @var array
- */
- public $errorMessages=array();
-
- /**
- * List of all log messages
- * @var array
- * @since 1.0
- */
- public $logMessages=array();
-
- /**
- * @param string $configFile name of the ini file to configure the framework
- */
- function __construct ($configFile) {
- global $gJCoord, $gJConfig;
-
- $gJCoord = $this;
-
- if(JELIX_APP_TEMP_PATH=='/'){ // le realpath dans application.ini.php a renvoyé false...
- die('Jelix Error: Application temp directory doesn\'t exist !');
- }
- if(!is_writable(JELIX_APP_TEMP_PATH)){
- die('Jelix Error: Application temp directory is not writable');
- }
- // load configuration data
- $gJConfig = jConfig::load($configFile);
-
- // set Error and exception handler
- // ne devrait être désactivé que lors de certains tests de jelix
- if($gJConfig->use_error_handler){
- set_error_handler('jErrorHandler');
- set_exception_handler('JExceptionHandler');
- }
- date_default_timezone_set($gJConfig->timeZone);
- $this->_loadPlugins();
- }
-
- /**
- * load the plugins and their configuration file
- */
- private function _loadPlugins(){
- global $gJConfig;
-
- foreach($gJConfig->plugins as $name=>$conf){
- if($conf && isset($gJConfig->_pluginsPathList_coord[$name])){
- if($conf=='1'){
- $conf = array();
- }else{
- $conff = $conf;
- if(!file_exists(JELIX_APP_CONFIG_PATH.$conff))
- die("Jelix Error: Error in the main configuration. Configuration file '$conff' for plugin $name doesn't exist!");
- if( false === ($conf = parse_ini_file(JELIX_APP_CONFIG_PATH.$conff,true)))
- die("Jelix Error: Error in the configuration file of plugin $name ($conff)!");
- }
- include( $gJConfig->_pluginsPathList_coord[$name].$name.'.coord.php');
- $class= $name.'CoordPlugin';
- $this->plugins[strtolower($name)] = new $class($conf);
- }
- }
- }
-
- /**
- * Store an error/warning/notice message. Responses object should take care
- * of the errorMessages properties to display them.
- * @param string $type error type : 'error', 'warning', 'notice'
- * @param integer $code error code
- * @param string $message error message
- * @param string $file the file name where the error appear
- * @param integer $line the line number where the error appear
- * @return boolean true= the process should stop now, false = the error manager do its job
- */
- public function addErrorMsg($type, $code, $message, $file, $line){
- $this->errorMessages[] = array($type, $code, $message, $file, $line);
- if(!$this->response){
- return $this->initDefaultResponseOfRequest();
- }
- return !$this->response->acceptSeveralErrors();
- }
-
- /**
- * Store a log message. Responses object should take care
- * of the logMessages properties to display them.
- * @param string $message error message
- * @since 1.0
- */
- public function addLogMsg($message, $type='default'){
- $this->logMessages[$type][] = $message;
- }
-
- /**
- * main method : launch the execution of the action.
- *
- * This method should be called in a entry point.
- * @param jRequest $request the request object
- */
- public function process ($request){
- global $gJConfig;
-
- $this->request = $request;
- $this->request->init();
- jSession::start();
-
- $this->moduleName = $this->request->getParam('module');
- $this->actionName = $this->request->getParam('action');
-
- if(empty($this->moduleName)){
- $this->moduleName = $gJConfig->startModule;
- }
- if(empty($this->actionName)){
- if($this->moduleName == $gJConfig->startModule)
- $this->actionName = $gJConfig->startAction;
- else {
- $this->actionName = 'default:index';
- }
- }
-
- // verification du module
- if(!in_array($this->moduleName,$gJConfig->_trustedModules)){
- throw new jException('jelix~errors.module.untrusted',$this->moduleName);
- }
-
- jContext::push ($this->moduleName);
- try{
- $this->action = new jSelectorActFast($this->request->type, $this->moduleName, $this->actionName);
- $ctrl = $this->getController($this->action);
- }catch(jException $e){
- if ($gJConfig->urlengine['notfoundAct'] =='') {
- throw $e;
- }
- try {
- $this->action = new jSelectorAct($gJConfig->urlengine['notfoundAct']);
- $ctrl = $this->getController($this->action);
- }catch(jException $e2){
- throw $e;
- }
- }
-
- $pluginparams = array();
- if(isset($ctrl->pluginParams['*'])){
- $pluginparams = $ctrl->pluginParams['*'];
- }
-
- if(isset($ctrl->pluginParams[$this->action->method])){
- $pluginparams = array_merge($pluginparams, $ctrl->pluginParams[$this->action->method]);
- }
-
- foreach ($this->plugins as $name => $obj){
- $result = $this->plugins[$name]->beforeAction ($pluginparams);
- if($result){
- $this->action = $result;
- jContext::pop();
- jContext::push($result->module);
- $this->moduleName = $result->module;
- $this->actionName = $result->resource;
- $ctrl = $this->getController($this->action);
- break;
- }
- }
-
- $this->response = $ctrl->{$this->action->method}();
-
- if($this->response == null){
- throw new jException('jelix~errors.response.missing',$this->action->toString());
- }
-
- foreach ($this->plugins as $name => $obj){
- $this->plugins[$name]->beforeOutput ();
- }
-
- // envoi de la réponse
- if(!$this->response->output()){
- $this->response->outputErrors();
- }
-
- foreach ($this->plugins as $name => $obj){
- $this->plugins[$name]->afterProcess ();
- }
-
- jContext::pop();
- jSession::end();
- }
-
- /**
- * get the controller corresponding to the selector
- * @param jSelectorAct $selector
- */
- private function getController($selector){
-
- $ctrlpath = $selector->getPath();
-
- if(!file_exists($ctrlpath)){
- throw new jException('jelix~errors.ad.controller.file.unknow',array($this->actionName,$ctrlpath));
- }
- require_once($ctrlpath);
- $class = $selector->getClass();
- if(!class_exists($class,false)){
- throw new jException('jelix~errors.ad.controller.class.unknow',array($this->actionName,$class, $ctrlpath));
- }
-
- $ctrl = new $class($this->request);
- if($ctrl instanceof jIRestController){
- $method = $selector->method = strtolower($_SERVER['REQUEST_METHOD']);
- }elseif(!method_exists($ctrl,$selector->method)){
- throw new jException('jelix~errors.ad.controller.method.unknow',array($this->actionName,$selector->method, $class, $ctrlpath));
- }
- return $ctrl;
- }
-
-
- /**
- * instancy a response object corresponding to the default response type
- * of the current resquest
- * @param boolean $originalResponse TRUE to get the original, non overloaded response
- * @return mixed error string or false
- */
- public function initDefaultResponseOfRequest($originalResponse = false){
- if($originalResponse)
- $responses = &$GLOBALS['gJConfig']->_coreResponses;
- else
- $responses = &$GLOBALS['gJConfig']->responses;
-
- $type= $this->request->defaultResponseType;
-
- if(!isset($responses[$type]))
- return jLocale::get('jelix~errors.default.response.type.unknow',array($this->moduleName.'~'.$this->actionName,$type));
-
- try{
- $respclass = $responses[$type];
- if($originalResponse)
- $path = JELIX_LIB_RESPONSE_PATH.$respclass.'.class.php';
- else
- $path = JELIX_APP_PATH.'responses/'.$respclass.'.class.php';
-
- if(!file_exists($path))
- throw new Exception();
-
- require_once($path);
-
- $this->response = new $respclass();
- return false;
- }
- catch(Exception $e){
- if($originalResponse)
- return jLocale::get('jelix~errors.default.response.not.loaded',array($this->moduleName.'~'.$this->actionName,$type));
- return $this->initDefaultResponseOfRequest(true);
- }
- }
-
- /**
- * gets a given plugin if registered
- * @param string $pluginName the name of the plugin
- * @param boolean $required says if the plugin is required or not. If true, will generate an exception if the plugin is not registered.
- * @return jICoordPlugin
- */
- public function getPlugin ($pluginName, $required = true){
- $pluginName = strtolower ($pluginName);
- if (isset ($this->plugins[$pluginName])){
- $plugin = $this->plugins[$pluginName];
- }else{
- if ($required){
- throw new jException('jelix~errors.plugin.unregister', $pluginName);
- }
- $plugin = null;
- }
- return $plugin;
- }
-
- /**
- * Says if the given plugin $name is enabled
- * @param string $pluginName
- * @return boolean true : plugin is ok
- */
- public function isPluginEnabled ($pluginName){
- return isset ($this->plugins[strtolower ($pluginName)]);
- }
-
- /**
- * Says if the given module $name is enabled
- * @param string $moduleName
- * @return boolean true : module is ok
- */
- public function isModuleEnabled ($moduleName){
- return in_array($moduleName, $GLOBALS['gJConfig']->_trustedModules);
- }
-
- /**
- * return the real path of a module
- * @param string $module a module name
- * @return string the corresponding path
- */
-
- public function getModulePath($module){
- global $gJConfig;
- if(!isset($gJConfig->_modulesPathList[$module])){
- throw new Exception('getModulePath : invalid module name');
- }
- return $gJConfig->_modulesPathList[$module];
- }
- }
- ?>
Documentation generated on Wed, 07 Sep 2011 13:46:50 +0200 by phpDocumentor 1.4.3