| 1 | | <?php |
|---|
| 2 | | /** |
|---|
| 3 | | * Declare all differents classes corresponding to main jelix selectors |
|---|
| 4 | | * |
|---|
| 5 | | * a selector is a string refering to a file or a ressource, by indicating its module and its name. |
|---|
| 6 | | * For example : "moduleName~resourceName". There are several type of selector, depending on the |
|---|
| 7 | | * resource type. Selector objects get the real path of the corresponding file, the name of the |
|---|
| 8 | | * compiler (if the file has to be compile) etc. |
|---|
| 9 | | * So here, there is a selector class for each selector type. |
|---|
| 10 | | * @package jelix |
|---|
| 11 | | * @subpackage core_selector |
|---|
| 12 | | * @author Laurent Jouanneau |
|---|
| 13 | | * @contributor Loic Mathaud |
|---|
| 14 | | * @contributor Rahal |
|---|
| 15 | | * @contributor Thibault PIRONT < nuKs > |
|---|
| 16 | | * @copyright 2005-2007 Laurent Jouanneau, 2007 Loic Mathaud, 2007 Rahal |
|---|
| 17 | | * @copyright 2007 Thibault PIRONT |
|---|
| 18 | | * @link http://www.jelix.org |
|---|
| 19 | | * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html |
|---|
| 20 | | */ |
|---|
| 21 | | |
|---|
| 22 | | /** |
|---|
| 23 | | * Create instance of selectors object |
|---|
| 24 | | * @package jelix |
|---|
| 25 | | * @subpackage core_selector |
|---|
| 26 | | */ |
|---|
| 27 | | class jSelectorFactory { |
|---|
| 28 | | private function __construct(){} |
|---|
| 29 | | |
|---|
| 30 | | /** |
|---|
| 31 | | * Create an instance of a selector object corresponding to the given selector |
|---|
| 32 | | * @param string $selstr the selector. It should be a full selector : "type:module~resource" (not "module~resource") |
|---|
| 33 | | * @return jISelector the corresponding selector |
|---|
| 34 | | */ |
|---|
| 35 | | static public function create ($selstr){ |
|---|
| 36 | | if(preg_match("/^([a-z]{3,5})\:([\w~\/\.]+)$/", $selstr, $m)){ |
|---|
| 37 | | $cname='jSelector'.$m[1]; |
|---|
| 38 | | if(class_exists($cname)){ |
|---|
| 39 | | $sel = new $cname($m[2]); |
|---|
| 40 | | return $sel; |
|---|
| 41 | | } |
|---|
| 42 | | } |
|---|
| 43 | | throw new jExceptionSelector('jelix~errors.selector.invalid.syntax', array($selstr,'')); |
|---|
| 44 | | } |
|---|
| 45 | | } |
|---|
| 46 | | #ifnot ENABLE_PHP_JELIX |
|---|
| 47 | | /** |
|---|
| 48 | | * interface of selector classes |
|---|
| 49 | | * @package jelix |
|---|
| 50 | | * @subpackage core_selector |
|---|
| 51 | | */ |
|---|
| 52 | | interface jISelector { |
|---|
| 53 | | /** |
|---|
| 54 | | * @return string file path corresponding to the resource pointing by the selector |
|---|
| 55 | | */ |
|---|
| 56 | | public function getPath (); |
|---|
| 57 | | /** |
|---|
| 58 | | * @return string file path of the compiled file (if the main file should be compiled by jelix) |
|---|
| 59 | | */ |
|---|
| 60 | | public function getCompiledFilePath (); |
|---|
| 61 | | /** |
|---|
| 62 | | * @return jICompiler the compiler used to compile file |
|---|
| 63 | | */ |
|---|
| 64 | | public function getCompiler(); |
|---|
| 65 | | /** |
|---|
| 66 | | * @return boolean true if the compiler compile many file at one time |
|---|
| 67 | | */ |
|---|
| 68 | | public function useMultiSourceCompiler(); |
|---|
| 69 | | /** |
|---|
| 70 | | * @param boolean $full true if you want a full selector ("type:...") |
|---|
| 71 | | * @return string the selector |
|---|
| 72 | | */ |
|---|
| 73 | | public function toString($full=false); |
|---|
| 74 | | } |
|---|
| 75 | | #endif |
|---|
| 76 | | |
|---|
| 77 | | /** |
|---|
| 78 | | * Exception for selector errors |
|---|
| 79 | | * @package jelix |
|---|
| 80 | | * @subpackage core_selector |
|---|
| 81 | | */ |
|---|
| 82 | | class jExceptionSelector extends jException { } |
|---|
| 83 | | |
|---|
| 84 | | /** |
|---|
| 85 | | * base class for all selector concerning module files |
|---|
| 86 | | * |
|---|
| 87 | | * General syntax for them : "module~resource". |
|---|
| 88 | | * Syntax of resource depend on the selector type. |
|---|
| 89 | | * module is optional. |
|---|
| 90 | | * @package jelix |
|---|
| 91 | | * @subpackage core_selector |
|---|
| 92 | | */ |
|---|
| 93 | | abstract class jSelectorModule implements jISelector { |
|---|
| 94 | | public $module = null; |
|---|
| 95 | | public $resource = null; |
|---|
| 96 | | |
|---|
| 97 | | protected $type = '_module'; |
|---|
| 98 | | protected $_dirname=''; |
|---|
| 99 | | protected $_suffix=''; |
|---|
| 100 | | protected $_cacheSuffix='.php'; |
|---|
| 101 | | protected $_path; |
|---|
| 102 | | protected $_cachePath; |
|---|
| 103 | | protected $_compiler = null; |
|---|
| 104 | | protected $_compilerPath; |
|---|
| 105 | | protected $_useMultiSourceCompiler=false; |
|---|
| 106 | | |
|---|
| 107 | | function __construct($sel){ |
|---|
| 108 | | #if ENABLE_PHP_JELIX |
|---|
| 109 | | if(jelix_scan_module_sel($sel, $this)){ |
|---|
| 110 | | if($this->module ==''){ |
|---|
| 111 | | $this->module = jContext::get (); |
|---|
| 112 | | } |
|---|
| 113 | | #else |
|---|
| 114 | | if(preg_match("/^(([a-zA-Z0-9_\.]+)~)?([a-zA-Z0-9_\.]+)$/", $sel, $m)){ |
|---|
| 115 | | if($m[1]!='' && $m[2]!=''){ |
|---|
| 116 | | $this->module = $m[2]; |
|---|
| 117 | | }else{ |
|---|
| 118 | | $this->module = jContext::get (); |
|---|
| 119 | | } |
|---|
| 120 | | $this->resource = $m[3]; |
|---|
| 121 | | #endif |
|---|
| 122 | | $this->_createPath(); |
|---|
| 123 | | $this->_createCachePath(); |
|---|
| 124 | | }else{ |
|---|
| 125 | | throw new jExceptionSelector('jelix~errors.selector.invalid.syntax', array($sel,$this->type)); |
|---|
| 126 | | } |
|---|
| 127 | | } |
|---|
| 128 | | |
|---|
| 129 | | public function getPath (){ |
|---|
| 130 | | return $this->_path; |
|---|
| 131 | | } |
|---|
| 132 | | |
|---|
| 133 | | public function getCompiledFilePath (){ |
|---|
| 134 | | return $this->_cachePath; |
|---|
| 135 | | } |
|---|
| 136 | | |
|---|
| 137 | | public function getCompiler(){ |
|---|
| 138 | | if($this->_compiler == null) return null; |
|---|
| 139 | | $n = $this->_compiler; |
|---|
| 140 | | require_once($this->_compilerPath); |
|---|
| 141 | | $o = new $n(); |
|---|
| 142 | | return $o; |
|---|
| 143 | | } |
|---|
| 144 | | |
|---|
| 145 | | public function useMultiSourceCompiler(){ |
|---|
| 146 | | return $this->_useMultiSourceCompiler; |
|---|
| 147 | | } |
|---|
| 148 | | |
|---|
| 149 | | public function toString($full=false){ |
|---|
| 150 | | if($full) |
|---|
| 151 | | return $this->type.':'.$this->module.'~'.$this->resource; |
|---|
| 152 | | else |
|---|
| 153 | | return $this->module.'~'.$this->resource; |
|---|
| 154 | | } |
|---|
| 155 | | |
|---|
| 156 | | protected function _createPath(){ |
|---|
| 157 | | global $gJConfig; |
|---|
| 158 | | if(!isset($gJConfig->_modulesPathList[$this->module])){ |
|---|
| 159 | | throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString(true)); |
|---|
| 160 | | } |
|---|
| 161 | | $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$this->resource.$this->_suffix; |
|---|
| 162 | | if (!is_readable ($this->_path)){ |
|---|
| 163 | | if($this->type == 'loc'){ |
|---|
| 164 | | throw new Exception('(202) The file of the locale key "'.$this->toString().'" (charset '.$this->charset.', lang '.$this->locale.') does not exist'); |
|---|
| 165 | | }elseif($this->toString() == 'jelix~errors.selector.invalid.target'){ |
|---|
| 166 | | throw new Exception("Jelix Panic ! don't find localization files to show you an other error message !"); |
|---|
| 167 | | }else{ |
|---|
| 168 | | throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), $this->type)); |
|---|
| 169 | | } |
|---|
| 170 | | } |
|---|
| 171 | | } |
|---|
| 172 | | |
|---|
| 173 | | protected function _createCachePath(){ |
|---|
| 174 | | $this->_cachePath = JELIX_APP_TEMP_PATH.'compiled/'.$this->_dirname.$this->module.'~'.$this->resource.$this->_cacheSuffix; |
|---|
| 175 | | } |
|---|
| 176 | | } |
|---|
| 177 | | |
|---|
| 178 | | /** |
|---|
| 179 | | * Special Action selector for jcoordinator |
|---|
| 180 | | * Don't use it ! Only for internal purpose. |
|---|
| 181 | | * @internal |
|---|
| 182 | | * @package jelix |
|---|
| 183 | | * @subpackage core_selector |
|---|
| 184 | | */ |
|---|
| 185 | | class jSelectorActFast extends jSelectorModule { |
|---|
| 186 | | protected $type = 'act'; |
|---|
| 187 | | public $request = ''; |
|---|
| 188 | | public $controller = ''; |
|---|
| 189 | | public $method=''; |
|---|
| 190 | | protected $_dirname='actions/'; |
|---|
| 191 | | |
|---|
| 192 | | /** |
|---|
| 193 | | */ |
|---|
| 194 | | function __construct($request, $module, $action){ |
|---|
| 195 | | $this->module = $module; |
|---|
| 196 | | #if ENABLE_OLD_ACTION_SELECTOR |
|---|
| 197 | | if($GLOBALS['gJConfig']->enableOldActionSelector == false || strpos($action,':') !== false) |
|---|
| 198 | | $separator = ':'; |
|---|
| 199 | | else |
|---|
| 200 | | $separator = '_'; |
|---|
| 201 | | $r = explode($separator,$action); |
|---|
| 202 | | #else |
|---|
| 203 | | $r = explode(':',$action); |
|---|
| 204 | | #endif |
|---|
| 205 | | if(count($r) == 1){ |
|---|
| 206 | | $this->controller = 'default'; |
|---|
| 207 | | $this->method = $r[0]==''?'index':$r[0]; |
|---|
| 208 | | }else{ |
|---|
| 209 | | $this->controller = $r[0]=='' ? 'default':$r[0]; |
|---|
| 210 | | $this->method = $r[1]==''?'index':$r[1]; |
|---|
| 211 | | } |
|---|
| 212 | | $this->resource = $this->controller.':'.$this->method; |
|---|
| 213 | | $this->request = $request; |
|---|
| 214 | | $this->_createPath(); |
|---|
| 215 | | } |
|---|
| 216 | | |
|---|
| 217 | | protected function _createPath(){ |
|---|
| 218 | | global $gJConfig; |
|---|
| 219 | | if(!isset($gJConfig->_modulesPathList[$this->module])){ |
|---|
| 220 | | throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString()); |
|---|
| 221 | | }else{ |
|---|
| 222 | | $this->_path = $gJConfig->_modulesPathList[$this->module].'controllers/'.$this->controller.'.'.$this->request.'.php'; |
|---|
| 223 | | } |
|---|
| 224 | | } |
|---|
| 225 | | |
|---|
| 226 | | protected function _createCachePath(){ |
|---|
| 227 | | $this->_cachePath = ''; |
|---|
| 228 | | } |
|---|
| 229 | | |
|---|
| 230 | | public function toString($full=false){ |
|---|
| 231 | | if($full) |
|---|
| 232 | | return $this->type.':'.$this->module.'~'.$this->resource.'@'.$this->request; |
|---|
| 233 | | else |
|---|
| 234 | | return $this->module.'~'.$this->resource.'@'.$this->request; |
|---|
| 235 | | } |
|---|
| 236 | | |
|---|
| 237 | | public function getClass(){ |
|---|
| 238 | | $className = $this->controller.'Ctrl'; |
|---|
| 239 | | #if ENABLE_OLD_CLASS_NAMING |
|---|
| 240 | | if(!class_exists($className,false)){ |
|---|
| 241 | | if(class_exists('CT'.$this->controller,false)) |
|---|
| 242 | | $className = 'CT'.$this->controller; |
|---|
| 243 | | } |
|---|
| 244 | | #endif |
|---|
| 245 | | return $className; |
|---|
| 246 | | } |
|---|
| 247 | | |
|---|
| 248 | | } |
|---|
| 249 | | |
|---|
| 250 | | |
|---|
| 251 | | /** |
|---|
| 252 | | * Generic Action selector |
|---|
| 253 | | * |
|---|
| 254 | | * main syntax: "module~action@requestType". module should be a valid module name or # (#=says to get |
|---|
| 255 | | * the module of the current request). action should be an action name (controller:method or controller_method). |
|---|
| 256 | | * all part are optional, but it should have one part at least. |
|---|
| 257 | | * @package jelix |
|---|
| 258 | | * @subpackage core_selector |
|---|
| 259 | | */ |
|---|
| 260 | | class jSelectorAct extends jSelectorActFast { |
|---|
| 261 | | |
|---|
| 262 | | /** |
|---|
| 263 | | * @param string $sel the selector |
|---|
| 264 | | * @param boolean $enableRequestPart true if the selector can contain the request part |
|---|
| 265 | | */ |
|---|
| 266 | | function __construct($sel, $enableRequestPart = false){ |
|---|
| 267 | | global $gJCoord; |
|---|
| 268 | | |
|---|
| 269 | | #if ENABLE_PHP_JELIX |
|---|
| 270 | | #if ENABLE_OLD_ACTION_SELECTOR |
|---|
| 271 | | if($GLOBALS['gJConfig']->enableOldActionSelector == false || strpos($sel,':') !== false) { |
|---|
| 272 | | $res = jelix_scan_action_sel($sel, $this, $gJCoord->actionName); |
|---|
| 273 | | } |
|---|
| 274 | | else{ |
|---|
| 275 | | $res = jelix_scan_old_action_sel($sel, $this, $gJCoord->actionName); |
|---|
| 276 | | } |
|---|
| 277 | | if($res){ |
|---|
| 278 | | #else |
|---|
| 279 | | if(jelix_scan_action_sel($sel, $this, $gJCoord->actionName)){ |
|---|
| 280 | | #endif |
|---|
| 281 | | if($this->module == '#'){ |
|---|
| 282 | | $this->module = $gJCoord->moduleName; |
|---|
| 283 | | }elseif($this->module ==''){ |
|---|
| 284 | | $this->module = jContext::get (); |
|---|
| 285 | | } |
|---|
| 286 | | |
|---|
| 287 | | if($this->request == '') |
|---|
| 288 | | $this->request = $gJCoord->request->type; |
|---|
| 289 | | |
|---|
| 290 | | #else |
|---|
| 291 | | if(preg_match("/^(?:([a-zA-Z0-9_\.]+|\#)~)?([a-zA-Z0-9_:]+|\#)?(?:@([a-zA-Z0-9_]+))?$/", $sel, $m)){ |
|---|
| 292 | | $m=array_pad($m,4,''); |
|---|
| 293 | | if($m[1]!=''){ |
|---|
| 294 | | if($m[1] == '#') |
|---|
| 295 | | $this->module = $gJCoord->moduleName; |
|---|
| 296 | | else |
|---|
| 297 | | $this->module = $m[1]; |
|---|
| 298 | | }else{ |
|---|
| 299 | | $this->module = jContext::get (); |
|---|
| 300 | | } |
|---|
| 301 | | if($m[2] == '#') |
|---|
| 302 | | $this->resource = $gJCoord->actionName; |
|---|
| 303 | | else |
|---|
| 304 | | $this->resource = $m[2]; |
|---|
| 305 | | #if ENABLE_OLD_ACTION_SELECTOR |
|---|
| 306 | | if($GLOBALS['gJConfig']->enableOldActionSelector == false || strpos($this->resource,':') !== false) |
|---|
| 307 | | $r = explode(':',$this->resource); |
|---|
| 308 | | else |
|---|
| 309 | | $r = explode('_',$this->resource); |
|---|
| 310 | | #else |
|---|
| 311 | | $r = explode(':',$this->resource); |
|---|
| 312 | | #endif |
|---|
| 313 | | if(count($r) == 1){ |
|---|
| 314 | | $this->controller = 'default'; |
|---|
| 315 | | $this->method = $r[0]==''?'index':$r[0]; |
|---|
| 316 | | }else{ |
|---|
| 317 | | $this->controller = $r[0]=='' ? 'default':$r[0]; |
|---|
| 318 | | $this->method = $r[1]==''?'index':$r[1]; |
|---|
| 319 | | } |
|---|
| 320 | | $this->resource = $this->controller.':'.$this->method; |
|---|
| 321 | | |
|---|
| 322 | | if($m[3] != '' && $enableRequestPart) |
|---|
| 323 | | $this->request = $m[3]; |
|---|
| 324 | | else |
|---|
| 325 | | $this->request = $gJCoord->request->type; |
|---|
| 326 | | #endif |
|---|
| 327 | | $this->_createPath(); |
|---|
| 328 | | }else{ |
|---|
| 329 | | throw new jExceptionSelector('jelix~errors.selector.invalid.syntax', array($sel,$this->type)); |
|---|
| 330 | | } |
|---|
| 331 | | } |
|---|
| 332 | | } |
|---|
| 333 | | |
|---|
| 334 | | /** |
|---|
| 335 | | * selector for business class |
|---|
| 336 | | * |
|---|
| 337 | | * business class is a class stored in classname.class.php file in the classes/ module directory |
|---|
| 338 | | * or one of its subdirectory. |
|---|
| 339 | | * syntax : "module~classname" or "module~classname. |
|---|
| 340 | | * @package jelix |
|---|
| 341 | | * @subpackage core_selector |
|---|
| 342 | | */ |
|---|
| 343 | | class jSelectorClass extends jSelectorModule { |
|---|
| 344 | | protected $type = 'class'; |
|---|
| 345 | | protected $_dirname = 'classes/'; |
|---|
| 346 | | protected $_suffix = '.class.php'; |
|---|
| 347 | | |
|---|
| 348 | | /** |
|---|
| 349 | | * subpath part in the resource content |
|---|
| 350 | | * @since 1.0b2 |
|---|
| 351 | | */ |
|---|
| 352 | | public $subpath =''; |
|---|
| 353 | | /** |
|---|
| 354 | | * the class name specified in the selector |
|---|
| 355 | | * @since 1.0b2 |
|---|
| 356 | | */ |
|---|
| 357 | | public $className = ''; |
|---|
| 358 | | |
|---|
| 359 | | function __construct($sel){ |
|---|
| 360 | | #if ENABLE_PHP_JELIX |
|---|
| 361 | | if(jelix_scan_class_sel($sel, $this)){ |
|---|
| 362 | | if($this->module ==''){ |
|---|
| 363 | | $this->module = jContext::get (); |
|---|
| 364 | | } |
|---|
| 365 | | #else |
|---|
| 366 | | if(preg_match("/^(([a-zA-Z0-9_\.]+)~)?([a-zA-Z0-9_\.\\/]+)$/", $sel, $m)){ |
|---|
| 367 | | if($m[1]!='' && $m[2]!=''){ |
|---|
| 368 | | $this->module = $m[2]; |
|---|
| 369 | | }else{ |
|---|
| 370 | | $this->module = jContext::get (); |
|---|
| 371 | | } |
|---|
| 372 | | $this->resource = $m[3]; |
|---|
| 373 | | if( ($p=strrpos($m[3], '/')) !== false){ |
|---|
| 374 | | $this->className = substr($m[3],$p+1); |
|---|
| 375 | | $this->subpath = substr($m[3],0,$p+1); |
|---|
| 376 | | }else{ |
|---|
| 377 | | $this->className = $m[3]; |
|---|
| 378 | | $this->subpath =''; |
|---|
| 379 | | } |
|---|
| 380 | | #endif |
|---|
| 381 | | $this->_createPath(); |
|---|
| 382 | | $this->_createCachePath(); |
|---|
| 383 | | }else{ |
|---|
| 384 | | throw new jExceptionSelector('jelix~errors.selector.invalid.syntax', array($sel,$this->type)); |
|---|
| 385 | | } |
|---|
| 386 | | } |
|---|
| 387 | | |
|---|
| 388 | | protected function _createPath(){ |
|---|
| 389 | | global $gJConfig; |
|---|
| 390 | | if (!isset($gJConfig->_modulesPathList[$this->module])) { |
|---|
| 391 | | throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString()); |
|---|
| 392 | | } |
|---|
| 393 | | $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$this->subpath.$this->className.$this->_suffix; |
|---|
| 394 | | |
|---|
| 395 | | if (!file_exists($this->_path) || strpos($this->subpath,'..') !== false ) { // second test for security issues |
|---|
| 396 | | throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), $this->type)); |
|---|
| 397 | | } |
|---|
| 398 | | } |
|---|
| 399 | | |
|---|
| 400 | | protected function _createCachePath(){ |
|---|
| 401 | | $this->_cachePath = ''; |
|---|
| 402 | | } |
|---|
| 403 | | |
|---|
| 404 | | public function toString($full=false){ |
|---|
| 405 | | if($full) |
|---|
| 406 | | return $this->type.':'.$this->module.'~'.$this->subpath.$this->className; |
|---|
| 407 | | else |
|---|
| 408 | | return $this->module.'~'.$this->subpath.$this->className; |
|---|
| 409 | | } |
|---|
| 410 | | } |
|---|
| 411 | | |
|---|
| 412 | | /** |
|---|
| 413 | | * selector for business class |
|---|
| 414 | | * |
|---|
| 415 | | * business class is a class stored in classname.class.php file in the classes/ module directory |
|---|
| 416 | | * or one of its subdirectory. |
|---|
| 417 | | * syntax : "module~classname" or "module~classname. |
|---|
| 418 | | * @package jelix |
|---|
| 419 | | * @subpackage core_selector |
|---|
| 420 | | * @since 1.0b2 |
|---|
| 421 | | */ |
|---|
| 422 | | class jSelectorInterface extends jSelectorClass { |
|---|
| 423 | | protected $type = 'iface'; |
|---|
| 424 | | protected $_dirname = 'classes/'; |
|---|
| 425 | | protected $_suffix = '.iface.php'; |
|---|
| 426 | | } |
|---|
| 427 | | |
|---|
| 428 | | /** |
|---|
| 429 | | * selector for localisation string |
|---|
| 430 | | * |
|---|
| 431 | | * localisation string are stored in file properties. |
|---|
| 432 | | * syntax : "module~prefixFile.keyString". |
|---|
| 433 | | * Corresponding file : locales/xx_XX/prefixFile.CCC.properties. |
|---|
| 434 | | * xx_XX and CCC are lang and charset set in the configuration |
|---|
| 435 | | * |
|---|
| 436 | | * @package jelix |
|---|
| 437 | | * @subpackage core_selector |
|---|
| 438 | | */ |
|---|
| 439 | | class jSelectorLoc extends jSelectorModule { |
|---|
| 440 | | protected $type = 'loc'; |
|---|
| 441 | | public $fileKey = ''; |
|---|
| 442 | | public $messageKey = ''; |
|---|
| 443 | | public $locale =''; |
|---|
| 444 | | public $charset=''; |
|---|
| 445 | | public $_compiler = 'jLocalesCompiler'; |
|---|
| 446 | | protected $_where; |
|---|
| 447 | | |
|---|
| 448 | | function __construct($sel, $locale=null, $charset=null){ |
|---|
| 449 | | global $gJConfig; |
|---|
| 450 | | if ($locale === null){ |
|---|
| 451 | | $locale = $gJConfig->locale; |
|---|
| 452 | | } |
|---|
| 453 | | if ($charset === null){ |
|---|
| 454 | | $charset = $gJConfig->charset; |
|---|
| 455 | | } |
|---|
| 456 | | if(strpos($locale,'_') === false){ |
|---|
| 457 | | $locale.='_'.strtoupper($locale); |
|---|
| 458 | | } |
|---|
| 459 | | $this->locale = $locale; |
|---|
| 460 | | $this->charset = $charset; |
|---|
| 461 | | $this->_dirname = 'locales/' .$locale.'/'; |
|---|
| 462 | | $this->_suffix = '.'.$charset.'.properties'; |
|---|
| 463 | | $this->_cacheSuffix = '.'.$charset.'.php'; |
|---|
| 464 | | $this->_compilerPath=JELIX_LIB_CORE_PATH.'jLocalesCompiler.class.php'; |
|---|
| 465 | | |
|---|
| 466 | | #if ENABLE_PHP_JELIX |
|---|
| 467 | | if(jelix_scan_locale_sel($sel, $this)){ |
|---|
| 468 | | if($this->module ==''){ |
|---|
| 469 | | $this->module = jContext::get (); |
|---|
| 470 | | } |
|---|
| 471 | | #else |
|---|
| 472 | | if(preg_match("/^(([a-zA-Z0-9_\.]+)~)?([a-zA-Z0-9_]+)\.([a-zA-Z0-9_\.]+)$/", $sel, $m)){ |
|---|
| 473 | | if($m[1]!='' && $m[2]!=''){ |
|---|
| 474 | | $this->module = $m[2]; |
|---|
| 475 | | }else{ |
|---|
| 476 | | $this->module = jContext::get (); |
|---|
| 477 | | } |
|---|
| 478 | | $this->resource = $m[3]; |
|---|
| 479 | | $this->fileKey = $m[3]; |
|---|
| 480 | | $this->messageKey = $m[4]; |
|---|
| 481 | | #endif |
|---|
| 482 | | $this->_createPath(); |
|---|
| 483 | | $this->_createCachePath(); |
|---|
| 484 | | }else{ |
|---|
| 485 | | throw new jExceptionSelector('jelix~errors.selector.invalid.syntax', array($sel,$this->type)); |
|---|
| 486 | | } |
|---|
| 487 | | } |
|---|
| 488 | | |
|---|
| 489 | | protected function _createPath(){ |
|---|
| 490 | | global $gJConfig; |
|---|
| 491 | | if(!isset($gJConfig->_modulesPathList[$this->module])){ |
|---|
| 492 | | throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString()); |
|---|
| 493 | | } |
|---|
| 494 | | |
|---|
| 495 | | // on regarde si la locale a été redéfini |
|---|
| 496 | | $overloadedPath = JELIX_APP_VAR_PATH.'overloads/'.$this->module.'/'.$this->_dirname.$this->resource.$this->_suffix; |
|---|
| 497 | | if (is_readable ($overloadedPath)){ |
|---|
| 498 | | $this->_path = $overloadedPath; |
|---|
| 499 | | $this->_where = 'overloaded/'; |
|---|
| 500 | | return; |
|---|
| 501 | | } |
|---|
| 502 | | // et sinon, on regarde si la locale existe dans le module en question |
|---|
| 503 | | $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$this->resource.$this->_suffix; |
|---|
| 504 | | |
|---|
| 505 | | if (!is_readable ($this->_path)){ |
|---|
| 506 | | throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), "locale")); |
|---|
| 507 | | } |
|---|
| 508 | | $this->_where = 'modules/'; |
|---|
| 509 | | } |
|---|
| 510 | | |
|---|
| 511 | | protected function _createCachePath(){ |
|---|
| 512 | | // on ne partage pas le même cache pour tous les emplacements possibles |
|---|
| 513 | | // au cas où un overload était supprimé |
|---|
| 514 | | $this->_cachePath = JELIX_APP_TEMP_PATH.'compiled/locales/'.$this->_where.$this->module.'~'.$this->resource.$this->_cacheSuffix; |
|---|
| 515 | | } |
|---|
| 516 | | |
|---|
| 517 | | public function toString($full=false){ |
|---|
| 518 | | if($full) |
|---|
| 519 | | return $this->type.':'.$this->module.'~'.$this->fileKey.'.'.$this->messageKey; |
|---|
| 520 | | else |
|---|
| 521 | | return $this->module.'~'.$this->fileKey.'.'.$this->messageKey; |
|---|
| 522 | | } |
|---|
| 523 | | } |
|---|
| 524 | | |
|---|
| 525 | | /** |
|---|
| 526 | | * Selector for dao file |
|---|
| 527 | | * syntax : "module~daoName". |
|---|
| 528 | | * file : daos/daoName.dao.xml |
|---|
| 529 | | * @package jelix |
|---|
| 530 | | * @subpackage core_selector |
|---|
| 531 | | */ |
|---|
| 532 | | class jSelectorDao extends jSelectorModule { |
|---|
| 533 | | protected $type = 'dao'; |
|---|
| 534 | | public $driver; |
|---|
| 535 | | protected $_dirname = 'daos/'; |
|---|
| 536 | | protected $_suffix = '.dao.xml'; |
|---|
| 537 | | protected $_where; |
|---|
| 538 | | |
|---|
| 539 | | function __construct($sel, $driver, $isprofil=true){ |
|---|
| 540 | | if($isprofil){ |
|---|
| 541 | | $p = jDb::getProfil($driver); |
|---|
| 542 | | if($p['driver'] == 'pdo'){ |
|---|
| 543 | | $this->driver=substr($p['dsn'],0,strpos($p['dsn'],':')); |
|---|
| 544 | | }else{ |
|---|
| 545 | | $this->driver= $p['driver']; |
|---|
| 546 | | } |
|---|
| 547 | | }else{ |
|---|
| 548 | | $this->driver=$driver; |
|---|
| 549 | | } |
|---|
| 550 | | $this->_compiler='jDaoCompiler'; |
|---|
| 551 | | $this->_compilerPath=JELIX_LIB_DAO_PATH.'jDaoCompiler.class.php'; |
|---|
| 552 | | parent::__construct($sel); |
|---|
| 553 | | } |
|---|
| 554 | | |
|---|
| 555 | | protected function _createPath(){ |
|---|
| 556 | | global $gJConfig; |
|---|
| 557 | | if(!isset($gJConfig->_modulesPathList[$this->module])){ |
|---|
| 558 | | throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString()); |
|---|
| 559 | | } |
|---|
| 560 | | |
|---|
| 561 | | // on regarde si le dao a été redéfini |
|---|
| 562 | | $overloadedPath = JELIX_APP_VAR_PATH.'overloads/'.$this->module.'/'.$this->_dirname.$this->resource.$this->_suffix; |
|---|
| 563 | | if (is_readable ($overloadedPath)){ |
|---|
| 564 | | $this->_path = $overloadedPath; |
|---|
| 565 | | $this->_where = 'overloaded/'; |
|---|
| 566 | | return; |
|---|
| 567 | | } |
|---|
| 568 | | // et sinon, on regarde si le dao existe dans le module en question |
|---|
| 569 | | $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$this->resource.$this->_suffix; |
|---|
| 570 | | |
|---|
| 571 | | if (!is_readable ($this->_path)){ |
|---|
| 572 | | throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), "dao")); |
|---|
| 573 | | } |
|---|
| 574 | | $this->_where = 'modules/'; |
|---|
| 575 | | } |
|---|
| 576 | | |
|---|
| 577 | | protected function _createCachePath(){ |
|---|
| 578 | | // on ne partage pas le même cache pour tous les emplacements possibles |
|---|
| 579 | | // au cas où un overload était supprimé |
|---|
| 580 | | $this->_cachePath = JELIX_APP_TEMP_PATH.'compiled/daos/'.$this->_where.$this->module.'~'.$this->resource.'~'.$this->driver.$this->_cacheSuffix; |
|---|
| 581 | | } |
|---|
| 582 | | |
|---|
| 583 | | public function getDaoClass(){ |
|---|
| 584 | | return 'cDao_'.$this->module.'_Jx_'.$this->resource.'_Jx_'.$this->driver; |
|---|
| 585 | | } |
|---|
| 586 | | public function getDaoRecordClass(){ |
|---|
| 587 | | return 'cDaoRecord_'.$this->module.'_Jx_'.$this->resource.'_Jx_'.$this->driver; |
|---|
| 588 | | } |
|---|
| 589 | | } |
|---|
| 590 | | |
|---|
| 591 | | /** |
|---|
| 592 | | * Template selector |
|---|
| 593 | | * |
|---|
| 594 | | * syntax : "module~tplName". |
|---|
| 595 | | * file : templates/tplName.tpl . |
|---|
| 596 | | * @package jelix |
|---|
| 597 | | * @subpackage core_selector |
|---|
| 598 | | */ |
|---|
| 599 | | class jSelectorTpl extends jSelectorModule { |
|---|
| 600 | | protected $type = 'tpl'; |
|---|
| 601 | | protected $_dirname = 'templates/'; |
|---|
| 602 | | protected $_suffix = '.tpl'; |
|---|
| 603 | | protected $_where; |
|---|
| 604 | | public $outputType=''; |
|---|
| 605 | | public $trusted=true; |
|---|
| 606 | | |
|---|
| 607 | | /** |
|---|
| 608 | | * @param string $sel the template selector |
|---|
| 609 | | * @param string $outputtype the type of output (html, text..) By default, it takes the response type |
|---|
| 610 | | * @param boolean $trusted says if the template file is trusted or not |
|---|
| 611 | | */ |
|---|
| 612 | | function __construct($sel, $outputtype='', $trusted=true){ |
|---|
| 613 | | if($outputtype == '') |
|---|
| 614 | | $this->outputType = $GLOBALS['gJCoord']->response->getFormatType(); |
|---|
| 615 | | else |
|---|
| 616 | | $this->outputType = $outputtype; |
|---|
| 617 | | $this->trusted = $trusted; |
|---|
| 618 | | $this->_compiler='jTplCompiler'; |
|---|
| 619 | | $this->_compilerPath=JELIX_LIB_TPL_PATH.'jTplCompiler.class.php'; |
|---|
| 620 | | parent::__construct($sel); |
|---|
| 621 | | } |
|---|
| 622 | | |
|---|
| 623 | | protected function _createPath(){ |
|---|
| 624 | | global $gJConfig; |
|---|
| 625 | | if(!isset($gJConfig->_modulesPathList[$this->module])){ |
|---|
| 626 | | throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString()); |
|---|
| 627 | | } |
|---|
| 628 | | |
|---|
| 629 | | $path = $this->module.'/'.$this->resource; |
|---|
| 630 | | $lpath = $this->module.'/'.$gJConfig->locale.'/'.$this->resource; |
|---|
| 631 | | |
|---|
| 632 | | if($gJConfig->theme != 'default'){ |
|---|
| 633 | | // on regarde si il y a un template redéfinie pour le theme courant |
|---|
| 634 | | $this->_where = 'themes/'.$gJConfig->theme.'/'.$lpath; |
|---|
| 635 | | $this->_path = JELIX_APP_VAR_PATH.$this->_where.'.tpl'; |
|---|
| 636 | | if (is_readable ($this->_path)){ |
|---|
| 637 | | return; |
|---|
| 638 | | } |
|---|
| 639 | | // on regarde si il y a un template redéfinie pour le theme courant |
|---|
| 640 | | $this->_where = 'themes/'.$gJConfig->theme.'/'.$path; |
|---|
| 641 | | $this->_path = JELIX_APP_VAR_PATH.$this->_where.'.tpl'; |
|---|
| 642 | | if (is_readable ($this->_path)){ |
|---|
| 643 | | return; |
|---|
| 644 | | } |
|---|
| 645 | | } |
|---|
| 646 | | |
|---|
| 647 | | // on regarde si il y a un template redéfinie dans le theme par defaut |
|---|
| 648 | | $this->_where = 'themes/default/'.$lpath; |
|---|
| 649 | | $this->_path = JELIX_APP_VAR_PATH.$this->_where.'.tpl'; |
|---|
| 650 | | if (is_readable ($this->_path)){ |
|---|
| 651 | | return; |
|---|
| 652 | | } |
|---|
| 653 | | |
|---|
| 654 | | $this->_where = 'themes/default/'.$path; |
|---|
| 655 | | $this->_path = JELIX_APP_VAR_PATH.$this->_where.'.tpl'; |
|---|
| 656 | | if (is_readable ($this->_path)){ |
|---|
| 657 | | return; |
|---|
| 658 | | } |
|---|
| 659 | | |
|---|
| 660 | | // et sinon, on regarde si le template existe dans le module en question |
|---|
| 661 | | $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$gJConfig->locale.'/'.$this->resource.'.tpl'; |
|---|
| 662 | | if (is_readable ($this->_path)){ |
|---|
| 663 | | $this->_where = 'modules/'.$lpath; |
|---|
| 664 | | return; |
|---|
| 665 | | } |
|---|
| 666 | | |
|---|
| 667 | | $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$this->resource.'.tpl'; |
|---|
| 668 | | if (is_readable ($this->_path)){ |
|---|
| 669 | | $this->_where = 'modules/'.$path; |
|---|
| 670 | | return; |
|---|
| 671 | | } |
|---|
| 672 | | |
|---|
| 673 | | throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), "template")); |
|---|
| 674 | | |
|---|
| 675 | | } |
|---|
| 676 | | |
|---|
| 677 | | protected function _createCachePath(){ |
|---|
| 678 | | // on ne partage pas le même cache pour tous les emplacements possibles |
|---|
| 679 | | // au cas où un overload était supprimé |
|---|
| 680 | | $this->_cachePath = JELIX_APP_TEMP_PATH.'compiled/templates/'.$this->_where.'_'.$this->outputType.($this->trusted?'_t':'').$this->_cacheSuffix; |
|---|
| 681 | | } |
|---|
| 682 | | } |
|---|
| 683 | | |
|---|
| 684 | | /** |
|---|
| 685 | | * Zone selector |
|---|
| 686 | | * |
|---|
| 687 | | * syntax : "module~zoneName". |
|---|
| 688 | | * file : zones/zoneName.zone.php . |
|---|
| 689 | | * @package jelix |
|---|
| 690 | | * @subpackage core_selector |
|---|
| 691 | | */ |
|---|
| 692 | | class jSelectorZone extends jSelectorModule { |
|---|
| 693 | | protected $type = 'zone'; |
|---|
| 694 | | protected $_dirname = 'zones/'; |
|---|
| 695 | | protected $_suffix = '.zone.php'; |
|---|
| 696 | | |
|---|
| 697 | | protected function _createCachePath(){ |
|---|
| 698 | | $this->_cachePath = ''; |
|---|
| 699 | | } |
|---|
| 700 | | } |
|---|
| 701 | | |
|---|
| 702 | | /** |
|---|
| 703 | | * Form selector |
|---|
| 704 | | * |
|---|
| 705 | | * syntax : "module~formName". |
|---|
| 706 | | * file : forms/formName.form.xml . |
|---|
| 707 | | * @package jelix |
|---|
| 708 | | * @subpackage core_selector |
|---|
| 709 | | */ |
|---|
| 710 | | class jSelectorForm extends jSelectorModule { |
|---|
| 711 | | protected $type = 'form'; |
|---|
| 712 | | protected $_where; |
|---|
| 713 | | protected $_dirname = 'forms/'; |
|---|
| 714 | | protected $_suffix = '.form.xml'; |
|---|
| 715 | | |
|---|
| 716 | | function __construct($sel){ |
|---|
| 717 | | |
|---|
| 718 | | $this->_compiler='jFormsCompiler'; |
|---|
| 719 | | $this->_compilerPath=JELIX_LIB_FORMS_PATH.'jFormsCompiler.class.php'; |
|---|
| 720 | | |
|---|
| 721 | | parent::__construct($sel); |
|---|
| 722 | | } |
|---|
| 723 | | |
|---|
| 724 | | public function getClass(){ |
|---|
| 725 | | return 'cForm_'.$this->module.'_Jx_'.$this->resource; |
|---|
| 726 | | } |
|---|
| 727 | | |
|---|
| 728 | | |
|---|
| 729 | | protected function _createPath(){ |
|---|
| 730 | | global $gJConfig; |
|---|
| 731 | | if(!isset($gJConfig->_modulesPathList[$this->module])){ |
|---|
| 732 | | throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString(true)); |
|---|
| 733 | | } |
|---|
| 734 | | |
|---|
| 735 | | // we see if the forms have been redefined |
|---|
| 736 | | $overloadedPath = JELIX_APP_VAR_PATH.'overloads/'.$this->module.'/'.$this->_dirname.$this->resource.$this->_suffix; |
|---|
| 737 | | if (is_readable ($overloadedPath)){ |
|---|
| 738 | | $this->_path = $overloadedPath; |
|---|
| 739 | | $this->_where = 'overloaded/'; |
|---|
| 740 | | return; |
|---|
| 741 | | } |
|---|
| 742 | | |
|---|
| 743 | | $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$this->resource.$this->_suffix; |
|---|
| 744 | | if (!is_readable ($this->_path)){ |
|---|
| 745 | | throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), $this->type)); |
|---|
| 746 | | } |
|---|
| 747 | | $this->_where = 'modules/'; |
|---|
| 748 | | } |
|---|
| 749 | | |
|---|
| 750 | | protected function _createCachePath(){ |
|---|
| 751 | | // on ne partage pas le même cache pour tous les emplacements possibles |
|---|
| 752 | | // au cas où un overload était supprimé |
|---|
| 753 | | $this->_cachePath = JELIX_APP_TEMP_PATH.'compiled/'.$this->_dirname.$this->_where.$this->module.'~'.$this->resource.$this->_cacheSuffix; |
|---|
| 754 | | } |
|---|
| 755 | | |
|---|
| 756 | | public function getCompiledBuilderFilePath ($type){ |
|---|
| 757 | | return JELIX_APP_TEMP_PATH.'compiled/'.$this->_dirname.$this->_where.$this->module.'~'.$this->resource.'_builder_'.$type.$this->_cacheSuffix; |
|---|
| 758 | | } |
|---|
| 759 | | |
|---|
| 760 | | } |
|---|
| 761 | | |
|---|
| 762 | | /** |
|---|
| 763 | | * base class for simple file selector |
|---|
| 764 | | * @package jelix |
|---|
| 765 | | * @subpackage core_selector |
|---|
| 766 | | */ |
|---|
| 767 | | class jSelectorSimpleFile implements jISelector { |
|---|
| 768 | | protected $type = 'simplefile'; |
|---|
| 769 | | public $file = ''; |
|---|
| 770 | | protected $_path; |
|---|
| 771 | | protected $_basePath=''; |
|---|
| 772 | | |
|---|
| 773 | | function __construct($sel){ |
|---|
| 774 | | if(preg_match("/^([\w\.\/]+)$/", $sel, $m)){ |
|---|
| 775 | | $this->file = $m[1]; |
|---|
| 776 | | $this->_path = $this->_basePath.$m[1]; |
|---|
| 777 | | }else{ |
|---|
| 778 | | throw new jExceptionSelector('jelix~errors.selector.invalid.syntax', array($sel,$this->type)); |
|---|
| 779 | | } |
|---|
| 780 | | } |
|---|
| 781 | | |
|---|
| 782 | | public function getPath (){ |
|---|
| 783 | | return $this->_path; |
|---|
| 784 | | } |
|---|
| 785 | | |
|---|
| 786 | | public function toString($full=false){ |
|---|
| 787 | | if($full) |
|---|
| 788 | | return $this->type.':'.$this->file; |
|---|
| 789 | | else |
|---|
| 790 | | return $this->file; |
|---|
| 791 | | } |
|---|
| 792 | | public function getCompiler(){ return null;} |
|---|
| 793 | | public function useMultiSourceCompiler() { return false;} |
|---|
| 794 | | public function getCompiledFilePath (){ return '';} |
|---|
| 795 | | } |
|---|
| 796 | | |
|---|
| 797 | | /** |
|---|
| 798 | | * Selector for files stored in the var directory |
|---|
| 799 | | * |
|---|
| 800 | | * @package jelix |
|---|
| 801 | | * @subpackage core_selector |
|---|
| 802 | | */ |
|---|
| 803 | | class jSelectorVar extends jSelectorSimpleFile { |
|---|
| 804 | | protected $type = 'var'; |
|---|
| 805 | | function __construct($sel){ |
|---|
| 806 | | $this->_basePath = JELIX_APP_VAR_PATH; |
|---|
| 807 | | parent::__construct($sel); |
|---|
| 808 | | } |
|---|
| 809 | | } |
|---|
| 810 | | |
|---|
| 811 | | /** |
|---|
| 812 | | * Selector for files stored in the config directory |
|---|
| 813 | | * |
|---|
| 814 | | * @package jelix |
|---|
| 815 | | * @subpackage core_selector |
|---|
| 816 | | */ |
|---|
| 817 | | class jSelectorCfg extends jSelectorSimpleFile { |
|---|
| 818 | | protected $type = 'cfg'; |
|---|
| 819 | | function __construct($sel){ |
|---|
| 820 | | $this->_basePath = JELIX_APP_CONFIG_PATH; |
|---|
| 821 | | parent::__construct($sel); |
|---|
| 822 | | } |
|---|
| 823 | | } |
|---|
| 824 | | |
|---|
| 825 | | /** |
|---|
| 826 | | * Selector for files stored in the temp directory |
|---|
| 827 | | * |
|---|
| 828 | | * @package jelix |
|---|
| 829 | | * @subpackage core_selector |
|---|
| 830 | | */ |
|---|
| 831 | | class jSelectorTmp extends jSelectorSimpleFile { |
|---|
| 832 | | protected $type = 'tmp'; |
|---|
| 833 | | function __construct($sel){ |
|---|
| 834 | | $this->_basePath = JELIX_APP_TEMP_PATH; |
|---|
| 835 | | parent::__construct($sel); |
|---|
| 836 | | } |
|---|
| 837 | | } |
|---|
| 838 | | |
|---|
| 839 | | /** |
|---|
| 840 | | * Selector for files stored in the log directory |
|---|
| 841 | | * |
|---|
| 842 | | * @package jelix |
|---|
| 843 | | * @subpackage core_selector |
|---|
| 844 | | */ |
|---|
| 845 | | class jSelectorLog extends jSelectorSimpleFile { |
|---|
| 846 | | protected $type = 'log'; |
|---|
| 847 | | function __construct($sel){ |
|---|
| 848 | | $this->_basePath = JELIX_APP_LOG_PATH; |
|---|
| 849 | | parent::__construct($sel); |
|---|
| 850 | | } |
|---|
| 851 | | } |
|---|
| 852 | | |
|---|
| 853 | | /** |
|---|
| 854 | | * Selector for files stored in the lib directory |
|---|
| 855 | | * |
|---|
| 856 | | * @package jelix |
|---|
| 857 | | * @subpackage core_selector |
|---|
| 858 | | */ |
|---|
| 859 | | class jSelectorLib extends jSelectorSimpleFile { |
|---|
| 860 | | protected $type = 'lib'; |
|---|
| 861 | | function __construct($sel){ |
|---|
| 862 | | $this->_basePath = LIB_PATH; |
|---|
| 863 | | parent::__construct($sel); |
|---|
| 864 | | } |
|---|
| 865 | | } |
|---|
| 866 | | |
|---|
| | 1 | <?php |
|---|
| | 2 | /** |
|---|
| | 3 | * Declare all differents classes corresponding to main jelix selectors |
|---|
| | 4 | * |
|---|
| | 5 | * a selector is a string refering to a file or a ressource, by indicating its module and its name. |
|---|
| | 6 | * For example : "moduleName~resourceName". There are several type of selector, depending on the |
|---|
| | 7 | * resource type. Selector objects get the real path of the corresponding file, the name of the |
|---|
| | 8 | * compiler (if the file has to be compile) etc. |
|---|
| | 9 | * So here, there is a selector class for each selector type. |
|---|
| | 10 | * @package jelix |
|---|
| | 11 | * @subpackage core_selector |
|---|
| | 12 | * @author Laurent Jouanneau |
|---|
| | 13 | * @contributor Loic Mathaud |
|---|
| | 14 | * @contributor Rahal |
|---|
| | 15 | * @contributor Thibault PIRONT < nuKs > |
|---|
| | 16 | * @contributor Julien Issler |
|---|
| | 17 | * @copyright 2005-2007 Laurent Jouanneau, 2007 Loic Mathaud, 2007 Rahal |
|---|
| | 18 | * @copyright 2007 Thibault PIRONT |
|---|
| | 19 | * @copyright 2007 Julien Issler |
|---|
| | 20 | * @link http://www.jelix.org |
|---|
| | 21 | * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html |
|---|
| | 22 | */ |
|---|
| | 23 | |
|---|
| | 24 | /** |
|---|
| | 25 | * Create instance of selectors object |
|---|
| | 26 | * @package jelix |
|---|
| | 27 | * @subpackage core_selector |
|---|
| | 28 | */ |
|---|
| | 29 | class jSelectorFactory { |
|---|
| | 30 | private function __construct(){} |
|---|
| | 31 | |
|---|
| | 32 | /** |
|---|
| | 33 | * Create an instance of a selector object corresponding to the given selector |
|---|
| | 34 | * @param string $selstr the selector. It should be a full selector : "type:module~resource" (not "module~resource") |
|---|
| | 35 | * @return jISelector the corresponding selector |
|---|
| | 36 | */ |
|---|
| | 37 | static public function create ($selstr){ |
|---|
| | 38 | if(preg_match("/^([a-z]{3,5})\:([\w~\/\.]+)$/", $selstr, $m)){ |
|---|
| | 39 | $cname='jSelector'.$m[1]; |
|---|
| | 40 | if(class_exists($cname)){ |
|---|
| | 41 | $sel = new $cname($m[2]); |
|---|
| | 42 | return $sel; |
|---|
| | 43 | } |
|---|
| | 44 | } |
|---|
| | 45 | throw new jExceptionSelector('jelix~errors.selector.invalid.syntax', array($selstr,'')); |
|---|
| | 46 | } |
|---|
| | 47 | } |
|---|
| | 48 | #ifnot ENABLE_PHP_JELIX |
|---|
| | 49 | /** |
|---|
| | 50 | * interface of selector classes |
|---|
| | 51 | * @package jelix |
|---|
| | 52 | * @subpackage core_selector |
|---|
| | 53 | */ |
|---|
| | 54 | interface jISelector { |
|---|
| | 55 | /** |
|---|
| | 56 | * @return string file path corresponding to the resource pointing by the selector |
|---|
| | 57 | */ |
|---|
| | 58 | public function getPath (); |
|---|
| | 59 | /** |
|---|
| | 60 | * @return string file path of the compiled file (if the main file should be compiled by jelix) |
|---|
| | 61 | */ |
|---|
| | 62 | public function getCompiledFilePath (); |
|---|
| | 63 | /** |
|---|
| | 64 | * @return jICompiler the compiler used to compile file |
|---|
| | 65 | */ |
|---|
| | 66 | public function getCompiler(); |
|---|
| | 67 | /** |
|---|
| | 68 | * @return boolean true if the compiler compile many file at one time |
|---|
| | 69 | */ |
|---|
| | 70 | public function useMultiSourceCompiler(); |
|---|
| | 71 | /** |
|---|
| | 72 | * @param boolean $full true if you want a full selector ("type:...") |
|---|
| | 73 | * @return string the selector |
|---|
| | 74 | */ |
|---|
| | 75 | public function toString($full=false); |
|---|
| | 76 | } |
|---|
| | 77 | #endif |
|---|
| | 78 | |
|---|
| | 79 | /** |
|---|
| | 80 | * Exception for selector errors |
|---|
| | 81 | * @package jelix |
|---|
| | 82 | * @subpackage core_selector |
|---|
| | 83 | */ |
|---|
| | 84 | class jExceptionSelector extends jException { } |
|---|
| | 85 | |
|---|
| | 86 | /** |
|---|
| | 87 | * base class for all selector concerning module files |
|---|
| | 88 | * |
|---|
| | 89 | * General syntax for them : "module~resource". |
|---|
| | 90 | * Syntax of resource depend on the selector type. |
|---|
| | 91 | * module is optional. |
|---|
| | 92 | * @package jelix |
|---|
| | 93 | * @subpackage core_selector |
|---|
| | 94 | */ |
|---|
| | 95 | abstract class jSelectorModule implements jISelector { |
|---|
| | 96 | public $module = null; |
|---|
| | 97 | public $resource = null; |
|---|
| | 98 | |
|---|
| | 99 | protected $type = '_module'; |
|---|
| | 100 | protected $_dirname=''; |
|---|
| | 101 | protected $_suffix=''; |
|---|
| | 102 | protected $_cacheSuffix='.php'; |
|---|
| | 103 | protected $_path; |
|---|
| | 104 | protected $_cachePath; |
|---|
| | 105 | protected $_compiler = null; |
|---|
| | 106 | protected $_compilerPath; |
|---|
| | 107 | protected $_useMultiSourceCompiler=false; |
|---|
| | 108 | |
|---|
| | 109 | function __construct($sel){ |
|---|
| | 110 | #if ENABLE_PHP_JELIX |
|---|
| | 111 | if(jelix_scan_module_sel($sel, $this)){ |
|---|
| | 112 | if($this->module ==''){ |
|---|
| | 113 | $this->module = jContext::get (); |
|---|
| | 114 | } |
|---|
| | 115 | #else |
|---|
| | 116 | if(preg_match("/^(([a-zA-Z0-9_\.]+)~)?([a-zA-Z0-9_\.]+)$/", $sel, $m)){ |
|---|
| | 117 | if($m[1]!='' && $m[2]!=''){ |
|---|
| | 118 | $this->module = $m[2]; |
|---|
| | 119 | }else{ |
|---|
| | 120 | $this->module = jContext::get (); |
|---|
| | 121 | } |
|---|
| | 122 | $this->resource = $m[3]; |
|---|
| | 123 | #endif |
|---|
| | 124 | $this->_createPath(); |
|---|
| | 125 | $this->_createCachePath(); |
|---|
| | 126 | }else{ |
|---|
| | 127 | throw new jExceptionSelector('jelix~errors.selector.invalid.syntax', array($sel,$this->type)); |
|---|
| | 128 | } |
|---|
| | 129 | } |
|---|
| | 130 | |
|---|
| | 131 | public function getPath (){ |
|---|
| | 132 | return $this->_path; |
|---|
| | 133 | } |
|---|
| | 134 | |
|---|
| | 135 | public function getCompiledFilePath (){ |
|---|
| | 136 | return $this->_cachePath; |
|---|
| | 137 | } |
|---|
| | 138 | |
|---|
| | 139 | public function getCompiler(){ |
|---|
| | 140 | if($this->_compiler == null) return null; |
|---|
| | 141 | $n = $this->_compiler; |
|---|
| | 142 | require_once($this->_compilerPath); |
|---|
| | 143 | $o = new $n(); |
|---|
| | 144 | return $o; |
|---|
| | 145 | } |
|---|
| | 146 | |
|---|
| | 147 | public function useMultiSourceCompiler(){ |
|---|
| | 148 | return $this->_useMultiSourceCompiler; |
|---|
| | 149 | } |
|---|
| | 150 | |
|---|
| | 151 | public function toString($full=false){ |
|---|
| | 152 | if($full) |
|---|
| | 153 | return $this->type.':'.$this->module.'~'.$this->resource; |
|---|
| | 154 | else |
|---|
| | 155 | return $this->module.'~'.$this->resource; |
|---|
| | 156 | } |
|---|
| | 157 | |
|---|
| | 158 | protected function _createPath(){ |
|---|
| | 159 | global $gJConfig; |
|---|
| | 160 | if(!isset($gJConfig->_modulesPathList[$this->module])){ |
|---|
| | 161 | throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString(true)); |
|---|
| | 162 | } |
|---|
| | 163 | $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$this->resource.$this->_suffix; |
|---|
| | 164 | if (!is_readable ($this->_path)){ |
|---|
| | 165 | if($this->type == 'loc'){ |
|---|
| | 166 | throw new Exception('(202) The file of the locale key "'.$this->toString().'" (charset '.$this->charset.', lang '.$this->locale.') does not exist'); |
|---|
| | 167 | }elseif($this->toString() == 'jelix~errors.selector.invalid.target'){ |
|---|
| | 168 | throw new Exception("Jelix Panic ! don't find localization files to show you an other error message !"); |
|---|
| | 169 | }else{ |
|---|
| | 170 | throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), $this->type)); |
|---|
| | 171 | } |
|---|
| | 172 | } |
|---|
| | 173 | } |
|---|
| | 174 | |
|---|
| | 175 | protected function _createCachePath(){ |
|---|
| | 176 | $this->_cachePath = JELIX_APP_TEMP_PATH.'compiled/'.$this->_dirname.$this->module.'~'.$this->resource.$this->_cacheSuffix; |
|---|
| | 177 | } |
|---|
| | 178 | } |
|---|
| | 179 | |
|---|
| | 180 | /** |
|---|
| | 181 | * Special Action selector for jcoordinator |
|---|
| | 182 | * Don't use it ! Only for internal purpose. |
|---|
| | 183 | * @internal |
|---|
| | 184 | * @package jelix |
|---|
| | 185 | * @subpackage core_selector |
|---|
| | 186 | */ |
|---|
| | 187 | class jSelectorActFast extends jSelectorModule { |
|---|
| | 188 | protected $type = 'act'; |
|---|
| | 189 | public $request = ''; |
|---|
| | 190 | public $controller = ''; |
|---|
| | 191 | public $method=''; |
|---|
| | 192 | protected $_dirname='actions/'; |
|---|
| | 193 | |
|---|
| | 194 | /** |
|---|
| | 195 | */ |
|---|
| | 196 | function __construct($request, $module, $action){ |
|---|
| | 197 | $this->module = $module; |
|---|
| | 198 | #if ENABLE_OLD_ACTION_SELECTOR |
|---|
| | 199 | if($GLOBALS['gJConfig']->enableOldActionSelector == false || strpos($action,':') !== false) |
|---|
| | 200 | $separator = ':'; |
|---|
| | 201 | else |
|---|
| | 202 | $separator = '_'; |
|---|
| | 203 | $r = explode($separator,$action); |
|---|
| | 204 | #else |
|---|
| | 205 | $r = explode(':',$action); |
|---|
| | 206 | #endif |
|---|
| | 207 | if(count($r) == 1){ |
|---|
| | 208 | $this->controller = 'default'; |
|---|
| | 209 | $this->method = $r[0]==''?'index':$r[0]; |
|---|
| | 210 | }else{ |
|---|
| | 211 | $this->controller = $r[0]=='' ? 'default':$r[0]; |
|---|
| | 212 | $this->method = $r[1]==''?'index':$r[1]; |
|---|
| | 213 | } |
|---|
| | 214 | $this->resource = $this->controller.':'.$this->method; |
|---|
| | 215 | $this->request = $request; |
|---|
| | 216 | $this->_createPath(); |
|---|
| | 217 | } |
|---|
| | 218 | |
|---|
| | 219 | protected function _createPath(){ |
|---|
| | 220 | global $gJConfig; |
|---|
| | 221 | if(!isset($gJConfig->_modulesPathList[$this->module])){ |
|---|
| | 222 | throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString()); |
|---|
| | 223 | }else{ |
|---|
| | 224 | $this->_path = $gJConfig->_modulesPathList[$this->module].'controllers/'.$this->controller.'.'.$this->request.'.php'; |
|---|
| | 225 | } |
|---|
| | 226 | } |
|---|
| | 227 | |
|---|
| | 228 | protected function _createCachePath(){ |
|---|
| | 229 | $this->_cachePath = ''; |
|---|
| | 230 | } |
|---|
| | 231 | |
|---|
| | 232 | public function toString($full=false){ |
|---|
| | 233 | if($full) |
|---|
| | 234 | return $this->type.':'.$this->module.'~'.$this->resource.'@'.$this->request; |
|---|
| | 235 | else |
|---|
| | 236 | return $this->module.'~'.$this->resource.'@'.$this->request; |
|---|
| | 237 | } |
|---|
| | 238 | |
|---|
| | 239 | public function getClass(){ |
|---|
| | 240 | $className = $this->controller.'Ctrl'; |
|---|
| | 241 | #if ENABLE_OLD_CLASS_NAMING |
|---|
| | 242 | if(!class_exists($className,false)){ |
|---|
| | 243 | if(class_exists('CT'.$this->controller,false)) |
|---|
| | 244 | $className = 'CT'.$this->controller; |
|---|
| | 245 | } |
|---|
| | 246 | #endif |
|---|
| | 247 | return $className; |
|---|
| | 248 | } |
|---|
| | 249 | |
|---|
| | 250 | } |
|---|
| | 251 | |
|---|
| | 252 | |
|---|
| | 253 | /** |
|---|
| | 254 | * Generic Action selector |
|---|
| | 255 | * |
|---|
| | 256 | * main syntax: "module~action@requestType". module should be a valid module name or # (#=says to get |
|---|
| | 257 | * the module of the current request). action should be an action name (controller:method or controller_method). |
|---|
| | 258 | * all part are optional, but it should have one part at least. |
|---|
| | 259 | * @package jelix |
|---|
| | 260 | * @subpackage core_selector |
|---|
| | 261 | */ |
|---|
| | 262 | class jSelectorAct extends jSelectorActFast { |
|---|
| | 263 | |
|---|
| | 264 | /** |
|---|
| | 265 | * @param string $sel the selector |
|---|
| | 266 | * @param boolean $enableRequestPart true if the selector can contain the request part |
|---|
| | 267 | */ |
|---|
| | 268 | function __construct($sel, $enableRequestPart = false){ |
|---|
| | 269 | global $gJCoord; |
|---|
| | 270 | |
|---|
| | 271 | #if ENABLE_PHP_JELIX |
|---|
| | 272 | #if ENABLE_OLD_ACTION_SELECTOR |
|---|
| | 273 | if($GLOBALS['gJConfig']->enableOldActionSelector == false || strpos($sel,':') !== false) { |
|---|
| | 274 | $res = jelix_scan_action_sel($sel, $this, $gJCoord->actionName); |
|---|
| | 275 | } |
|---|
| | 276 | else{ |
|---|
| | 277 | $res = jelix_scan_old_action_sel($sel, $this, $gJCoord->actionName); |
|---|
| | 278 | } |
|---|
| | 279 | if($res){ |
|---|
| | 280 | #else |
|---|
| | 281 | if(jelix_scan_action_sel($sel, $this, $gJCoord->actionName)){ |
|---|
| | 282 | #endif |
|---|
| | 283 | if($this->module == '#'){ |
|---|
| | 284 | $this->module = $gJCoord->moduleName; |
|---|
| | 285 | }elseif($this->module ==''){ |
|---|
| | 286 | $this->module = jContext::get (); |
|---|
| | 287 | } |
|---|
| | 288 | |
|---|
| | 289 | if($this->request == '') |
|---|
| | 290 | $this->request = $gJCoord->request->type; |
|---|
| | 291 | |
|---|
| | 292 | #else |
|---|
| | 293 | if(preg_match("/^(?:([a-zA-Z0-9_\.]+|\#)~)?([a-zA-Z0-9_:]+|\#)?(?:@([a-zA-Z0-9_]+))?$/", $sel, $m)){ |
|---|
| | 294 | |
|---|