Changeset 235
- Timestamp:
- 08/10/06 12:27:54 (2 years ago)
- Files:
-
- trunk/build/manifests/jelix-lib.mn (modified) (2 diffs)
- trunk/lib/jelix/core/jController.class.php (modified) (3 diffs)
- trunk/lib/jelix/core/jCoordinator.class.php (modified) (15 diffs)
- trunk/lib/jelix/core/jLocale.class.php (modified) (8 diffs)
- trunk/lib/jelix/core/jRequest.class.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/build/manifests/jelix-lib.mn
r223 r235 64 64 jResponseRdf.class.php 65 65 jResponseXml.class.php 66 jResponseZip.class.php 66 67 67 68 cd lib/jelix/core/url … … 172 173 jDatatype.class.php 173 174 jCmdUtils.class.php 175 jZipCreator.class.php 174 176 175 177 cd lib/json trunk/lib/jelix/core/jController.class.php
r156 r235 4 4 * @subpackage core 5 5 * @version $Id:$ 6 * @author Laurent Jouanneau6 * @author Laurent Jouanneau 7 7 * @contributor Loic Mathaud 8 * @copyright 2001-2005 CopixTeam,2005-2006 Laurent Jouanneau8 * @copyright 2005-2006 Laurent Jouanneau 9 9 * @link http://www.jelix.org 10 10 * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 11 11 * 12 * Classe orginellement issue du framework Copix 2.3dev20050901. http://www.copix.org (CopixActionGroup)13 * Une partie du code est sous Copyright 2001-2005 CopixTeam (licence LGPL)14 * Auteurs initiaux : Gerald Croes et Laurent Jouanneau15 * Adapt�et am�or�pour Jelix par Laurent Jouanneau16 12 */ 17 13 14 /** 15 * interface for controllers used for RESTFull request/response 16 */ 18 17 interface jIRestController{ 19 18 public function get(); … … 23 22 } 24 23 25 class jController{26 24 25 /** 26 * class base for controllers 27 * 28 * A controller is used to implement one or many actions, one method for each action. 29 */ 30 abstract class jController{ 31 32 /** 33 * parameters for plugins 34 * 35 * this array should contains all parameters needed by installed plugins for 36 * each action, see the documentation of each plugins to know this parameters. 37 * keys : name of an action or * for parameters to all action 38 * values : array that contains all plugin parameters 39 * @var array 40 */ 27 41 public $pluginParams=array(); 42 43 /** 44 * the request object 45 * @var jRequest 46 */ 28 47 protected $request; 29 48 30 49 /** 31 *32 * @param33 */50 * 51 * @param jRequest $request the current request object 52 */ 34 53 function __construct ( $request){ 35 54 $this->request = $request; … … 46 65 47 66 /** 48 * Gets the value of a request variable. If not defined, gets its default value. 49 * @param string $varName the name of the request variable 50 * @param mixed $varDefaultValue the default value of the request variable 51 * @return mixed the request variable value 67 * Gets the value of a request parameter. If not defined, gets its default value. 68 * @param string $parName the name of the request parameter 69 * @param mixed $parDefaultValue the default returned value if the parameter doesn't exists 70 * @param boolean $useDefaultIfEmpty true: says to return the default value the value is "" 71 * @return mixed the request parameter value 52 72 */ 53 protected function param ($ varName, $varDefaultValue=null, $useDefaultIfEmpty=false){54 return $this->request->getParam($ varName, $varDefaultValue, $useDefaultIfEmpty);73 protected function param ($parName, $parDefaultValue=null, $useDefaultIfEmpty=false){ 74 return $this->request->getParam($parName, $parDefaultValue, $useDefaultIfEmpty); 55 75 } 56 76 77 /** 78 * same as param(), but convert the value to an integer value. If it isn't 79 * a numerical value, return null. 80 * @param string $parName the name of the request parameter 81 * @param mixed $parDefaultValue the default returned value if the parameter doesn't exists 82 * @param boolean $useDefaultIfEmpty true: says to return the default value the value is "" 83 * @return integer the request parameter value 84 */ 85 protected function intParam ($parName, $parDefaultValue=null, $useDefaultIfEmpty=false){ 86 $value = $this->request->getParam($parName, $parDefaultValue, $useDefaultIfEmpty); 87 if(is_numeric($value)) 88 return intval($value); 89 else 90 return null; 91 } 92 93 /** 94 * same as param(), but convert the value to a float value. If it isn't 95 * a numerical value, return null. 96 * @param string $parName the name of the request parameter 97 * @param mixed $parDefaultValue the default returned value if the parameter doesn't exists 98 * @param boolean $useDefaultIfEmpty true: says to return the default value the value is "" 99 * @return float the request parameter value 100 */ 101 protected function floatParam ($parName, $parDefaultValue=null, $useDefaultIfEmpty=false){ 102 $value = $this->request->getParam($parName, $parDefaultValue, $useDefaultIfEmpty); 103 if(is_numeric($value)) 104 return floatval($value); 105 else 106 return null; 107 } 108 109 /** 110 * same as param(), but convert the value to a boolean value. If it isn't 111 * a numerical value, return null. 112 * @param string $parName the name of the request parameter 113 * @param mixed $parDefaultValue the default returned value if the parameter doesn't exists 114 * @param boolean $useDefaultIfEmpty true: says to return the default value the value is "" 115 * @return boolean the request parameter value 116 */ 117 protected function boolParam ($parName, $parDefaultValue=null, $useDefaultIfEmpty=false){ 118 $value = $this->request->getParam($parName, $parDefaultValue, $useDefaultIfEmpty); 119 if($value=="true" || $value == "1" || $value=="on" || $value=="yes") 120 return true; 121 elseif($value=="false" || $value == "0" || $value=="off" || $value=="no") 122 return false; 123 else 124 return null; 125 } 126 127 /** 128 * @return array all request parameters 129 */ 130 protected function params(){ return $this->request->params; } 131 132 /** 133 * get a response object. 134 * @param string $name the name of the response type (ex: "html") 135 * @param boolean $useOriginal true:don't use the response object redefined by the application 136 * @return jResponse the response object 137 */ 57 138 protected function getResponse($name, $useOriginal=false){ 58 139 return $this->request->getResponse($name, $useOriginal); 59 140 } 60 141 61 protected function params(){ return $this->request->params; }62 63 142 } 64 143 ?> trunk/lib/jelix/core/jCoordinator.class.php
r231 r235 11 11 */ 12 12 13 13 /** 14 * the main class of the jelix core 15 * 16 * this is the "chief orchestra" of the framework. It's goal is 17 * to load the configuration, to get the request parameters 18 * used to instancie the correspondant controllers and to run the right method. 19 */ 14 20 class jCoordinator { 15 21 16 22 /** 17 * liste des plugins utilis� * @var array 23 * plugin list 24 * @var array 18 25 */ 19 26 public $plugins=array(); 20 27 21 28 /** 22 * Reponse courante29 * current response object 23 30 * @var jResponse 24 31 */ … … 26 33 27 34 /** 35 * current request object 28 36 * @var jRequest 29 37 */ … … 31 39 32 40 /** 33 * @var jActionDesc 41 * the selector of the current action 42 * @var jSelectorAct 34 43 */ 35 44 public $action = null; 36 /** 45 46 /** 47 * the current module name 37 48 * @var string 38 49 */ 39 private $configFile;40 41 /**42 * @var string43 */44 50 public $moduleName; 45 /** 51 52 /** 53 * the current action name 46 54 * @var string 47 55 */ … … 69 77 } 70 78 // load configuration data 71 $this->configFile = $configFile;72 79 $gJConfig = jConfig::load($configFile); 73 80 … … 84 91 85 92 /** 86 * instanciation des plugins93 * load the plugins and their configuration file 87 94 */ 88 95 private function _loadPlugins(){ … … 105 112 106 113 /** 107 * stocke un message d'erreur/warning/notice �rendre en compte par les r�nses 108 * @param string $type type d'erreur 'error', 'warning', 'notice' 109 * @param integer $code code d'erreur 110 * @param string $message le message d'erreur 111 * @param string $file nom du fichier o�st produite l'erreur 112 * @param integer $line ligne o�st produite l'erreur 113 * @return boolean true= arret immediat ordonn�false = on laisse le gestionnaire d'erreur agir en cons�ence 114 * Store an error/warning/notice message. Responses object should take care 115 * of the errorMessages properties to display them. 116 * @param string $type error type : 'error', 'warning', 'notice' 117 * @param integer $code error code 118 * @param string $message error message 119 * @param string $file the file name where the error appear 120 * @param integer $line the line number where the error appear 121 * @return boolean true= the process should stop now, false = the error manager do its job 114 122 */ 115 123 public function addErrorMsg($type, $code, $message, $file, $line){ … … 123 131 124 132 /** 125 * Fonction principale du coordinateur �ppeler dans le index.php pour d�rrer 126 * le traitement de l'action 127 * @param jRequest $request the request data 133 * main method : launch the execution of the action. 134 * 135 * This method should be called in a entry point. 136 * @param jRequest $request the request object 128 137 */ 129 138 public function process ($request){ … … 180 189 181 190 if($this->response == null){ 182 trigger_error(jLocale::get('jelix~errors.response.missing',$this->action->toString()), E_USER_ERROR); 183 return; 191 throw new jException('jelix~errors.response.missing',$this->action->toString()); 184 192 } 185 193 … … 200 208 } 201 209 210 /** 211 * get the controller corresponding to the selector 212 * @param jSelectorAct $selector 213 */ 202 214 private function getController($selector){ 203 215 … … 206 218 207 219 if(!file_exists($ctrlpath)){ 208 trigger_error(jLocale::get('jelix~errors.ad.controller.file.unknow',array($this->actionName,$ctrlpath)),E_USER_ERROR); 209 return; 220 throw new jException('jelix~errors.ad.controller.file.unknow',array($this->actionName,$ctrlpath)); 210 221 } 211 222 require_once($ctrlpath); 212 223 if(!class_exists($class,false)){ 213 trigger_error(jLocale::get('jelix~errors.ad.controller.class.unknow',array($this->actionName,$class, $ctrlpath)),E_USER_ERROR); 214 return; 224 throw new jException('jelix~errors.ad.controller.class.unknow',array($this->actionName,$class, $ctrlpath)); 215 225 } 216 226 … … 219 229 $method = $selector->method = strtolower($_SERVER['REQUEST_METHOD']); 220 230 }elseif(!method_exists($ctrl,$selector->method)){ 221 trigger_error(jLocale::get('jelix~errors.ad.controller.method.unknow',array($this->actionName,$selector->method, $class, $ctrlpath)),E_USER_ERROR); 222 return; 231 throw new jException('jelix~errors.ad.controller.method.unknow',array($this->actionName,$selector->method, $class, $ctrlpath)); 223 232 } 224 233 return $ctrl; … … 227 236 228 237 /** 229 * 230 * @param string $name 238 * instancy a response object corresponding to the default response type 239 * of the current resquest 240 * @return mixed error string or false 231 241 */ 232 242 public function initDefaultResponseOfRequest(){ … … 253 263 } 254 264 255 256 /*257 * permet �n traitement exterieur (page, zone) de recuperer un element de configuration d'un plugin258 * @param string $plugin_name nom du plugin259 * @param string $plugin_parameter_name nom de la propriete de l'objet de configuration du plugin260 */261 /*function getPluginConf ($pluginName , $plugin_parameter_name){262 $pluginName = strtolower ($pluginName);263 if (isset ($this->plugins[$pluginName])&& isset($this->plugins[$pluginName]->config->$plugin_parameter_name) ) {264 return $this->plugins[$pluginName]->config->$plugin_parameter_name;265 }266 return null;267 }*/268 269 265 /** 270 266 * gets a given plugin if registered 271 * @param string $plugin_name nom du plugin 272 * @param boolean $required if the plugin is required or not. If true, will trigger a fatal_error if the plugin is not registered. 267 * @param string $pluginName the name of the plugin 268 * @param boolean $required says if the plugin is required or not. If true, will generate an exception if the plugin is not registered. 269 * @return jIPlugin 273 270 */ 274 271 function getPlugin ($pluginName, $required = true){ … … 278 275 }else{ 279 276 if ($required){ 280 t rigger_error (jLocale::get ('jelix~errors.plugin.unregister', $pluginName), E_USER_ERROR);277 throw new jException('jelix~errors.plugin.unregister', $pluginName); 281 278 } 282 279 $plugin = null; … … 297 294 * Says if the given module $name is enabled 298 295 * @param string $moduleName 299 * @return boolean true : pluginis ok296 * @return boolean true : module is ok 300 297 */ 301 298 public function isModuleEnabled ($moduleName){ trunk/lib/jelix/core/jLocale.class.php
r231 r235 18 18 19 19 20 /* 21 * Contient un ensemble de chaines concernant une locale donn� 22 * et pour tous les charsets 20 /** 21 * a bundle content all readed properties ina a given langage, and for all charsets 23 22 */ 24 23 class jBundle { … … 31 30 /** 32 31 * constructor 33 * @param jSelector $file 34 * @param string $locale 32 * @param jSelector $file selector of a properties file 33 * @param string $locale the code lang 35 34 */ 36 35 function __construct ($file, $locale){ … … 140 139 }else{ 141 140 if($match[4] != '' && substr($match[4],0,1) != '#'){ 142 t rigger_error('Syntaxe error in file properties '.$fichier.' line '.$linenumber, E_USER_NOTICE);141 throw new Exception('Syntaxe error in file properties '.$fichier.' line '.$linenumber); 143 142 } 144 143 } 145 144 }else { 146 t rigger_error('Syntaxe error in file properties '.$fichier.' line '.$linenumber, E_USER_NOTICE);145 throw new Exception('Syntaxe error in file properties '.$fichier.' line '.$linenumber); 147 146 } 148 147 } … … 150 149 fclose ($f); 151 150 }else{ 152 t rigger_error ('Cannot load the resource '.$fichier, E_USER_ERROR);151 throw new Exception('Cannot load the resource '.$fichier); 153 152 } 154 153 } … … 156 155 157 156 158 157 /** 158 * static class to get a localized string 159 */ 159 160 class jLocale { 160 161 static $bundles = array(); 161 162 162 163 /** 163 * gets the current lang 164 */ 165 function getCurrentLang(){ 164 * gets the current lang 165 * @return string 166 */ 167 static function getCurrentLang(){ 166 168 $s=$GLOBALS['gJConfig']->defaultLocale; 167 169 return substr($s,0, strpos($s,'_')); 168 170 } 169 171 /** 170 * gets the current country. 171 */ 172 function getCurrentCountry (){ 172 * gets the current country. 173 * @return string 174 */ 175 static function getCurrentCountry (){ 173 176 $s=$GLOBALS['gJConfig']->defaultLocale; 174 177 return substr($s,strpos($s,'_')+1); … … 179 182 * if it can't get the correct language, it will try to gets the string 180 183 * from the default language. 181 * if both fails, it will raise a fatal_error. 184 * if both fails, it will raise an exception. 185 * @param string $key the key of the localized string 186 * @param array $args arguments to apply to the localized string with sprintf 187 * @param string $locale the lang code. if null, use the default language 188 * @param string $charset the charset code. if null, use the default charset 189 * @return string the localized string 182 190 */ 183 191 static function get ($key, $args=null, $locale=null, $charset=null) { … … 201 209 }catch(jExceptionSelector $e){ 202 210 if($key == 'jelix~errors.locale.key.selector.invalid'){ 203 return '(200)The given locale key "'.$args[0].'" is invalid (for module '.$args[1].', charset '.$args[2].', lang '.$args[3].') (internal error ?)';211 throw new Exception('(200)The given locale key "'.$args[0].'" is invalid (for module '.$args[1].', charset '.$args[2].', lang '.$args[3].') (internal error ?)'); 204 212 }else{ 205 t rigger_error (jLocale::get ('jelix~errors.locale.key.selector.invalid', array($key,$file->module, $charset, $locale)), E_USER_ERROR);206 return '(200)Invalid Local Key "'.$args[0].'"'; // au cas o�trigger error n'�it pas trapp�}213 throw new jException('jelix~errors.locale.key.selector.invalid', array($key,$file->module, $charset, $locale)); 214 } 207 215 } 208 216 … … 219 227 if ($locale == $gJConfig->defaultLocale){ 220 228 if ($key == 'jelix~errors.locale.key.unknow'){ 221 $msg = 'Can\'t find message key (which should actually be THIS message): '.$key;229 throw new Exception('(210)The given locale key "'.$args[0].'" from module "'.$args[1].'" does not exists in the default lang for the '.$args[2].' charset (and the jelix~errors.locale.key.unknow key cannot be found too)'); 222 230 }else{ 223 $msg = jLocale::get('jelix~errors.locale.key.unknow',array($key,$file->module, $charset, $locale));231 throw new jException('jelix~errors.locale.key.unknow',array($key,$file->module, $charset, $locale)); 224 232 } 225 trigger_error ($msg, E_USER_ERROR);226 return null;227 233 } 228 234 return jLocale::get ($key, $args, $gJConfig->defaultLocale); trunk/lib/jelix/core/jRequest.class.php
r190 r235 10 10 * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 11 11 * 12 * Some parts of this file are took from Copix Framework v2.3dev20050901, CopixCoordinator.class.php,13 * copyrighted by CopixTeam and released under GNU Lesser General Public Licence14 * author : Gerald Croes, Laurent Jouanneau15 * http://www.copix.org16 12 */ 17 13 18 14 19 15 /** 16 * base class for object which retrieve all parameters of an http request. The 17 * process depends on the type of request (ex: xmlrpc..) 18 * 19 * @copyright line codes which set the url_* properties are took from Copix Framework v2.3dev20050901, CopixCoordinator.class.php, 20 * copyrighted by CopixTeam and released under GNU Lesser General Public Licence 21 * author : Gerald Croes, Laurent Jouanneau 22 * http://www.copix.org 23 */ 20 24 abstract class jRequest { 21 25 22 26 /** 23 * liste des param�es en entr� * @var array 27 * request parameters 28 * could set from $_GET, $_POST, or from data processing of $HTTP_RAW_POST_DATA 29 * @var array 24 30 */ 25 31 public $params; 26 32 33 /** 34 * the request type code 35 * @var string 36 */ 27 37 public $type; 28 38 39 /** 40 * the type of the default response 41 * @var string 42 */ 29 43 public $defaultResponseType = ''; 30 44 31 /* param�es de l'url courante */ 45 /** 46 * the path to the entry point in the url 47 * @var string 48 */ 32 49 public $url_script_path; 50 51 /** 52 * the name of the entry point 53 * @var string 54 */ 33 55 public $url_script_name; 56 57 /** 58 * the pathinfo part of the url 59 * @var string 60 */ 34 61 public $url_path_info; 35 62 63 64 /** 65 * 66 * @var jUrl 67 */ 36 68 public $url; 37 69 38 70 function __construct(){ } 39 71 40 function init(){ 72 /** 73 * initialize the request : analyse of http request etc.. 74 */ 75 public function init(){ 41 76 $this->_initUrlDatas(); 42 77 $this->_initParams(); 43 78 } 44 79 45 80 /** 81 * analyse the http request and set the params property 82 */ 46 83 abstract protected function _initParams(); 47 84 85 /** 86 * inits the url_* properties 87 */ 48 88 protected function _initUrlDatas(){ 49 89 global $gJConfig; … … 74 114 } 75 115 116 /** 117 * Gets the value of a request parameter. If not defined, gets its default value. 118 * @param string $name the name of the request parameter 119 * @param mixed $defaultValue the default returned value if the parameter doesn't exists 120 * @param boolean $useDefaultIfEmpty true: says to return the default value the value is "" 121 * @return mixed the request parameter value 122 */ 76 123 public function getParam($name, $defaultValue=null, $useDefaultIfEmpty=false){ 77 124 … … 88 135 89 136 /** 90 * indique la liste des classes de reponses autoris� pour le type de requete91 * si renvoi false : autorise n'importe quoi92 * @see j ActionDesc::getResponse137 * return a list of class name of allowed response corresponding to the request 138 * @return array the list, or false which means everything 139 * @see jRequest::getResponse() 93 140 */ 94 141 public function allowedResponses(){ return false;} 95 142 143 /** 144 * @param string $respclass the name of a response class 145 */ 96 146 public function isAllowedResponse($respclass){ 97 147 if($ar=$this->allowedResponses()){ … … 101 151 } 102 152 153 /** 154 * get a response object. 155 * @param string $name the name of the response type (ex: "html") 156 * @param boolean $useOriginal true:don't use the response object redefined by the application 157 * @return jResponse the response object 158 */ 103 159 public function getResponse($type='', $useOriginal = false){ 104 160 global $gJCoord, $gJConfig; … … 109 165 if($useOriginal){ 110 166 if(!isset($gJConfig->_coreResponses[$type])){ 111 trigger_error(jLocale::get('jelix~errors.ad.response.type.unknow',array($gJCoord->action->resource,$type,$gJCoord->action->getPath())),E_USER_ERROR); 112 return null; 167 throw new jException('jelix~errors.ad.response.type.unknow',array($gJCoord->action->resource,$type,$gJCoord->action->getPath())); 113 168 } 114 169 $respclass = $gJConfig->_coreResponses[$type]; 115 170 }else{ 116 171 if(!isset($gJConfig->responses[$type])){ 117 trigger_error(jLocale::get('jelix~errors.ad.response.type.unknow',array($gJCoord->action->resource,$type,$gJCoord->action->getPath())),E_USER_ERROR); 118 return null; 172 throw new jException('jelix~errors.ad.response.type.unknow',array($gJCoord->action->resource,$type,$gJCoord->action->getPath())); 119 173 } 120 174 $respclass = $gJConfig->responses[$type]; … … 125 179 require_once ($path); 126 180 }else{ 127 trigger_error(jLocale::get('jelix~errors.ad.response.not.loaded',array($gJCoord->action->resource,$type,$gJCoord->action->getPath())),E_USER_ERROR); 128 return null; 181 throw new jException('jelix~errors.ad.response.not.loaded',array($gJCoord->action->resource,$type,$gJCoord->action->getPath())); 129 182 } 130 183 131 184 if(!$this->isAllowedResponse($respclass)){ 132 trigger_error(jLocale::get('jelix~errors.ad.response.type.notallowed',array($gJCoord->action->resource,$type,$gJCoord->action->getPath())),E_USER_ERROR); 133 return null; 185 throw new jException('jelix~errors.ad.response.type.notallowed',array($gJCoord->action->resource,$type,$gJCoord->action->getPath())); 134 186 } 135 187
