Source for file Route.php

Documentation is available at Route.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Application
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Object.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../Application/IRouter.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * The bidirectional route is responsible for mapping
  30. 30:  * HTTP request to a PresenterRoute object for dispatch and vice-versa.
  31. 31:  *
  32. 32:  * @author     David Grudl
  33. 33:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  34. 34:  * @package    Nette\Application
  35. 35:  */
  36. 36: class Route extends Object implements IRouter
  37. 37: {
  38. 38:     const PRESENTER_KEY = 'presenter';
  39. 39:     const MODULE_KEY = 'module';
  40. 40:  
  41. 41:     /** flag */
  42. 42:     const CASE_SENSITIVE = 256;
  43. 43:  
  44. 44:     /**#@+ uri type */
  45. 45:     const HOST = 1;
  46. 46:     const PATH = 2;
  47. 47:     const RELATIVE = 3;
  48. 48:     /**#@-*/
  49. 49:  
  50. 50:     /**#@+ key used in {@link Route::$styles} */
  51. 51:     const PATTERN = 'pattern';
  52. 52:     const FILTER_IN = 'filterIn';
  53. 53:     const FILTER_OUT = 'filterOut';
  54. 54:     const FILTER_TABLE = 'filterTable';
  55. 55:     /**#@-*/
  56. 56:  
  57. 57:     /**#@+ @ignore internal fixity types - how to handle 'default' value? {@link Route::$metadata} */
  58. 58:     const OPTIONAL 0;
  59. 59:     const PATH_OPTIONAL 1;
  60. 60:     const CONSTANT 2;
  61. 61:     /**#@-*/
  62. 62:  
  63. 63:     /** @var bool */
  64. 64:     public static $defaultFlags 0;
  65. 65:  
  66. 66:     /** @var array */
  67. 67:     public static $styles array(
  68. 68:         '#' => array// default style for path parameters
  69. 69:             self::PATTERN => '[^/]+',
  70. 70:             self::FILTER_IN => 'rawurldecode',
  71. 71:             self::FILTER_OUT => 'rawurlencode',
  72. 72:         ),
  73. 73:         '?#' => array// default style for query parameters
  74. 74:         ),
  75. 75:         'module' => array(
  76. 76:             self::PATTERN => '[a-z][a-z0-9.-]*',
  77. 77:             self::FILTER_IN => array(__CLASS__'path2presenter'),
  78. 78:             self::FILTER_OUT => array(__CLASS__'presenter2path'),
  79. 79:         ),
  80. 80:         'presenter' => array(
  81. 81:             self::PATTERN => '[a-z][a-z0-9.-]*',
  82. 82:             self::FILTER_IN => array(__CLASS__'path2presenter'),
  83. 83:             self::FILTER_OUT => array(__CLASS__'presenter2path'),
  84. 84:         ),
  85. 85:         'action' => array(
  86. 86:             self::PATTERN => '[a-z][a-z0-9-]*',
  87. 87:             self::FILTER_IN => array(__CLASS__'path2action'),
  88. 88:             self::FILTER_OUT => array(__CLASS__'action2path'),
  89. 89:         ),
  90. 90:         '?module' => array(
  91. 91:         ),
  92. 92:         '?presenter' => array(
  93. 93:         ),
  94. 94:         '?action' => array(
  95. 95:         ),
  96. 96:     );
  97. 97:  
  98. 98:     /** @var string */
  99. 99:     private $mask;
  100. 100:  
  101. 101:     /** @var array */
  102. 102:     private $sequence;
  103. 103:  
  104. 104:     /** @var string  regular expression pattern */
  105. 105:     private $re;
  106. 106:  
  107. 107:     /** @var array of [default & fixity, filterIn, filterOut] */
  108. 108:     protected $metadata = array();
  109. 109:  
  110. 110:     /** @var array  */
  111. 111:     protected $xlat;
  112. 112:  
  113. 113:     /** @var int HOST, PATH, RELATIVE */
  114. 114:     protected $type;
  115. 115:  
  116. 116:     /** @var int */
  117. 117:     protected $flags;
  118. 118:  
  119. 119:  
  120. 120:  
  121. 121:     /**
  122. 122:      * @param  string  URL mask, e.g. '<presenter>/<action>/<id \d{1,3}>'
  123. 123:      * @param  array   default values
  124. 124:      * @param  int     flags
  125. 125:      */
  126. 126:     public function __construct($maskarray $defaults array()$flags 0)
  127. 127:     {
  128. 128:         $this->flags = $flags self::$defaultFlags;
  129. 129:         $this->setMask($mask$defaults);
  130. 130:     }
  131. 131:  
  132. 132:  
  133. 133:  
  134. 134:     /**
  135. 135:      * Maps HTTP request to a PresenterRequest object.
  136. 136:      * @param  IHttpRequest 
  137. 137:      * @return PresenterRequest|NULL
  138. 138:      */
  139. 139:     public function match(IHttpRequest $httpRequest)
  140. 140:     {
  141. 141:         // combine with precedence: mask (params in URL-path), fixity, query, (post,) defaults
  142. 142:  
  143. 143:         // 1) URL MASK
  144. 144:         $uri $httpRequest->getUri();
  145. 145:  
  146. 146:         if ($this->type === self::HOST{
  147. 147:             $path '//' $uri->getHost($uri->getPath();
  148. 148:  
  149. 149:         elseif ($this->type === self::RELATIVE{
  150. 150:             $basePath $uri->getBasePath();
  151. 151:             if (strncmp($uri->getPath()$basePathstrlen($basePath)) !== 0{
  152. 152:                 return NULL;
  153. 153:             }
  154. 154:             $path = (string) substr($uri->getPath()strlen($basePath));
  155. 155:  
  156. 156:         else {
  157. 157:             $path $uri->getPath();
  158. 158:         }
  159. 159:  
  160. 160:         if ($path !== ''{
  161. 161:             $path rtrim($path'/''/';
  162. 162:         }
  163. 163:  
  164. 164:         if (!preg_match($this->re$path$matches)) {
  165. 165:             // stop, not matched
  166. 166:             return NULL;
  167. 167:         }
  168. 168:  
  169. 169:         // deletes numeric keys, restore '-' chars
  170. 170:         $params array();
  171. 171:         foreach ($matches as $k => $v{
  172. 172:             if (is_string($k)) {
  173. 173:                 $params[str_replace('___''-'$k)$v// trick
  174. 174:             }
  175. 175:         }
  176. 176:  
  177. 177:  
  178. 178:         // 2) CONSTANT FIXITY
  179. 179:         foreach ($this->metadata as $name => $meta{
  180. 180:             if (isset($params[$name])) {
  181. 181:                 //$params[$name] = $this->flags & self::CASE_SENSITIVE === 0 ? strtolower($params[$name]) : */$params[$name]; // strtolower damages UTF-8
  182. 182:  
  183. 183:             elseif (isset($meta['fixity']&& $meta['fixity'!== self::OPTIONAL{
  184. 184:                 $params[$nameNULL// cannot be overwriten in 3) and detected by isset() in 4)
  185. 185:             }
  186. 186:         }
  187. 187:  
  188. 188:  
  189. 189:         // 3) QUERY
  190. 190:         if ($this->xlat{
  191. 191:             $params += self::renameKeys($httpRequest->getQuery()array_flip($this->xlat));
  192. 192:         else {
  193. 193:             $params += $httpRequest->getQuery();
  194. 194:         }
  195. 195:  
  196. 196:  
  197. 197:         // 4) APPLY FILTERS & FIXITY
  198. 198:         foreach ($this->metadata as $name => $meta{
  199. 199:             if (isset($params[$name])) {
  200. 200:                 if (!is_scalar($params[$name])) {
  201. 201:  
  202. 202:                 elseif (isset($meta[self::FILTER_TABLE][$params[$name]])) // applyies filterTable only to scalar parameters
  203. 203:                     $params[$name$meta[self::FILTER_TABLE][$params[$name]];
  204. 204:  
  205. 205:                 elseif (isset($meta[self::FILTER_IN])) // applyies filterIn only to scalar parameters
  206. 206:                     $params[$namecall_user_func($meta[self::FILTER_IN](string) $params[$name]);
  207. 207:                     if ($params[$name=== NULL && !isset($meta['fixity'])) {
  208. 208:                         return NULL// rejected by filter
  209. 209:                     }
  210. 210:                 }
  211. 211:  
  212. 212:             elseif (isset($meta['fixity'])) {
  213. 213:                 $params[$name$meta['default'];
  214. 214:             }
  215. 215:         }
  216. 216:  
  217. 217:  
  218. 218:         // 5) BUILD PresenterRequest
  219. 219:         if (!isset($params[self::PRESENTER_KEY])) {
  220. 220:             throw new InvalidStateException('Missing presenter in route definition.');
  221. 221:         }
  222. 222:         if (isset($this->metadata[self::MODULE_KEY])) {
  223. 223:             if (!isset($params[self::MODULE_KEY])) {
  224. 224:                 throw new InvalidStateException('Missing module in route definition.');
  225. 225:             }
  226. 226:             $presenter $params[self::MODULE_KEY':' $params[self::PRESENTER_KEY];
  227. 227:             unset($params[self::MODULE_KEY]$params[self::PRESENTER_KEY]);
  228. 228:  
  229. 229:         else {
  230. 230:             $presenter $params[self::PRESENTER_KEY];
  231. 231:             unset($params[self::PRESENTER_KEY]);
  232. 232:         }
  233. 233:  
  234. 234:         return new PresenterRequest(
  235. 235:             $presenter,
  236. 236:             $httpRequest->getMethod(),
  237. 237:             $params,
  238. 238:             $httpRequest->getPost(),
  239. 239:             $httpRequest->getFiles(),
  240. 240:             array(PresenterRequest::SECURED => $httpRequest->isSecured())
  241. 241:         );
  242. 242:     }
  243. 243:  
  244. 244:  
  245. 245:  
  246. 246:     /**
  247. 247:      * Constructs absolute URL from PresenterRequest object.
  248. 248:      * @param  IHttpRequest 
  249. 249:      * @param  PresenterRequest 
  250. 250:      * @return string|NULL
  251. 251:      */
  252. 252:     public function constructUrl(PresenterRequest $appRequestIHttpRequest $httpRequest)
  253. 253:     {
  254. 254:         if ($this->flags self::ONE_WAY{
  255. 255:             return NULL;
  256. 256:         }
  257. 257:  
  258. 258:         $params $appRequest->getParams();
  259. 259:         $metadata $this->metadata;
  260. 260:  
  261. 261:         $presenter $appRequest->getPresenterName();
  262. 262:         if (isset($metadata[self::MODULE_KEY])) {
  263. 263:             if (isset($metadata[self::MODULE_KEY]['fixity'])) {
  264. 264:                 $a strlen($metadata[self::MODULE_KEY]['default']);
  265. 265:                 if (substr($presenter$a1!== ':'{
  266. 266:                     return NULL// module not match
  267. 267:                 }
  268. 268:             else {
  269. 269:                 $a strrpos($presenter':');
  270. 270:             }
  271. 271:             $params[self::MODULE_KEYsubstr($presenter0$a);
  272. 272:             $params[self::PRESENTER_KEYsubstr($presenter$a 1);
  273. 273:         else {
  274. 274:             $params[self::PRESENTER_KEY$presenter;
  275. 275:         }
  276. 276:  
  277. 277:         foreach ($metadata as $name => $meta{
  278. 278:             if (!isset($params[$name])) continue// retains NULL values
  279. 279:  
  280. 280:             if (isset($meta['fixity'])) {
  281. 281:                 if (is_scalar($params[$name]&& strcasecmp($params[$name]$meta['default']=== 0{
  282. 282:                     // remove default values; NULL values are retain
  283. 283:                     unset($params[$name]);
  284. 284:                     continue;
  285. 285:  
  286. 286:                 elseif ($meta['fixity'=== self::CONSTANT{
  287. 287:                     return NULL// missing or wrong parameter '$name'
  288. 288:                 }
  289. 289:             }
  290. 290:  
  291. 291:             if (!is_scalar($params[$name])) {
  292. 292:  
  293. 293:             elseif (isset($meta['filterTable2'][$params[$name]])) {
  294. 294:                 $params[$name$meta['filterTable2'][$params[$name]];
  295. 295:  
  296. 296:             elseif (isset($meta[self::FILTER_OUT])) {
  297. 297:                 $params[$namecall_user_func($meta[self::FILTER_OUT]$params[$name]);
  298. 298:             }
  299. 299:  
  300. 300:             if (isset($meta[self::PATTERN]&& !preg_match($meta[self::PATTERN]$params[$name])) {
  301. 301:                 return NULL// pattern not match
  302. 302:             }
  303. 303:         }
  304. 304:  
  305. 305:         // compositing path
  306. 306:         $sequence $this->sequence;
  307. 307:         $optional TRUE;
  308. 308:         $uri '';
  309. 309:         $i count($sequence1;
  310. 310:         do {
  311. 311:             $uri $sequence[$i$uri;
  312. 312:             if ($i === 0break;
  313. 313:             $i--;
  314. 314:  
  315. 315:             $name $sequence[$i]$i--// parameter name
  316. 316:  
  317. 317:             if ($name[0=== '?'// "foo" parameter
  318. 318:                 continue;
  319. 319:  
  320. 320:             elseif (isset($params[$name]&& $params[$name!= ''// intentionally ==
  321. 321:                 $optional FALSE;
  322. 322:                 $uri $params[$name$uri;
  323. 323:                 unset($params[$name]);
  324. 324:  
  325. 325:             elseif (isset($metadata[$name]['fixity'])) // has default value?
  326. 326:                 if ($optional{
  327. 327:                     $uri '';
  328. 328:  
  329. 329:                 elseif ($metadata[$name]['default'== ''// intentionally ==
  330. 330:                     if ($uri[0=== '/' && substr($sequence[$i]-1=== '/'{
  331. 331:                         return NULL// default value is empty but is required
  332. 332:                     }
  333. 333:  
  334. 334:                 else {
  335. 335:                     $uri $metadata[$name]['defOut'$uri;
  336. 336:                 }
  337. 337:  
  338. 338:             else {
  339. 339:                 return NULL// missing parameter '$name'
  340. 340:             }
  341. 341:         while (TRUE);
  342. 342:  
  343. 343:  
  344. 344:         // build query string
  345. 345:         if ($this->xlat{
  346. 346:             $params self::renameKeys($params$this->xlat);
  347. 347:         }
  348. 348:  
  349. 349:         $sep ini_get('arg_separator.input');
  350. 350:         $query http_build_query($params''$sep $sep[0'&');
  351. 351:         if ($query != ''$uri .= '?' $query// intentionally ==
  352. 352:  
  353. 353:         // absolutize path
  354. 354:         if ($this->type === self::RELATIVE{
  355. 355:             $uri '//' $httpRequest->getUri()->getAuthority($httpRequest->getUri()->getBasePath($uri;
  356. 356:  
  357. 357:         elseif ($this->type === self::PATH{
  358. 358:             $uri '//' $httpRequest->getUri()->getAuthority($uri;
  359. 359:         }
  360. 360:  
  361. 361:         $uri ($this->flags self::SECURED 'https:' 'http:'$uri;
  362. 362:  
  363. 363:         return $uri;
  364. 364:     }
  365. 365:  
  366. 366:  
  367. 367:  
  368. 368:     /**
  369. 369:      * Parse mask and array of default values; initializes object.
  370. 370:      * @param  string 
  371. 371:      * @param  array 
  372. 372:      * @return void 
  373. 373:      */
  374. 374:     private function setMask($maskarray $defaults)
  375. 375:     {
  376. 376:         $this->mask $mask;
  377. 377:  
  378. 378:         // detect '//host/path' vs. '/abs. path' vs. 'relative path'
  379. 379:         if (substr($mask02=== '//'{
  380. 380:             $this->type = self::HOST;
  381. 381:  
  382. 382:         elseif (substr($mask01=== '/'{
  383. 383:             $this->type = self::PATH;
  384. 384:  
  385. 385:         else {
  386. 386:             $this->type self::RELATIVE;
  387. 387:         }
  388. 388:  
  389. 389:         $metadata array();
  390. 390:         foreach ($defaults as $name => $def{
  391. 391:             $metadata[$namearray(
  392. 392:                 'default' => $def,
  393. 393:                 'fixity' => self::CONSTANT
  394. 394:             );
  395. 395:         }
  396. 396:  
  397. 397:  
  398. 398:         // 1) PARSE QUERY PART OF MASK
  399. 399:         $this->xlat array();
  400. 400:         $pos strpos($mask' ? ');
  401. 401:         if ($pos !== FALSE{
  402. 402:             preg_match_all(
  403. 403:                 '/(?:([a-zA-Z0-9_.-]+)=)?<([^># ]+) *([^>#]*)(#?[^>]*)>/'// name=<parameter-name [pattern][#class]>
  404. 404:                 substr($mask$pos 1),
  405. 405:                 $matches,
  406. 406:                 PREG_SET_ORDER
  407. 407:             );
  408. 408:             $mask rtrim(substr($mask0$pos));
  409. 409:  
  410. 410:             foreach ($matches as $match{
  411. 411:                 list($param$name$pattern$class$match;  // $pattern is unsed
  412. 412:  
  413. 413:                 if ($class !== ''{
  414. 414:                     if (!isset(self::$styles[$class])) {
  415. 415:                         throw new InvalidStateException("Parameter '$name' has '$class' flag, but Route::\$styles['$class'] is not set.");
  416. 416:                     }
  417. 417:                     $meta self::$styles[$class];
  418. 418:  
  419. 419:                 elseif (isset(self::$styles['?' $name])) {
  420. 420:                     $meta self::$styles['?' $name];
  421. 421:  
  422. 422:                 else {
  423. 423:                     $meta self::$styles['?#'];
  424. 424:                 }
  425. 425:  
  426. 426:                 if (isset($metadata[$name])) {
  427. 427:                     $meta $meta $metadata[$name];
  428. 428:                 }
  429. 429:  
  430. 430:                 if (array_key_exists('default'$meta)) {
  431. 431:                     $meta['fixity'self::OPTIONAL;
  432. 432:                 }
  433. 433:  
  434. 434:                 unset($meta['pattern']);
  435. 435:                 $meta['filterTable2'empty($meta[self::FILTER_TABLE]NULL array_flip($meta[self::FILTER_TABLE]);
  436. 436:  
  437. 437:                 $metadata[$name$meta;
  438. 438:                 if ($param !== ''{
  439. 439:                     $this->xlat[$name$param;
  440. 440:                 }
  441. 441:             }
  442. 442:         }
  443. 443:  
  444. 444:  
  445. 445:         // 2) PARSE URI-PATH PART OF MASK
  446. 446:         $parts preg_split(
  447. 447:             '/<([^># ]+) *([^>#]*)(#?[^>]*)>/',  // <parameter-name [pattern][#class]>
  448. 448:             $mask,
  449. 449:             -1,
  450. 450:             PREG_SPLIT_DELIM_CAPTURE
  451. 451:         );
  452. 452:  
  453. 453:         $optional TRUE;
  454. 454:         $sequence array();
  455. 455:         $i count($parts1;
  456. 456:         $re '';
  457. 457:         do {
  458. 458:             array_unshift($sequence$parts[$i]);
  459. 459:             $re preg_quote($parts[$i]'#'$re;
  460. 460:             if ($i === 0break;
  461. 461:             $i--;
  462. 462:  
  463. 463:             $class $parts[$i]$i--// validation class
  464. 464:             $pattern $parts[$i]$i--// validation condition (as regexp)
  465. 465:             $name $parts[$i]$i--// parameter name
  466. 466:             array_unshift($sequence$name);
  467. 467:  
  468. 468:             if ($name[0=== '?'// "foo" parameter
  469. 469:                 $re '(?:' preg_quote(substr($name1)'#''|' $pattern ')' $re;
  470. 470:                 $sequence[1substr($name1$sequence[1];
  471. 471:                 continue;
  472. 472:             }
  473. 473:  
  474. 474:             // check name (limitation by regexp)
  475. 475:             if (preg_match('#[^a-z0-9_-]#i'$name)) {
  476. 476:                 throw new InvalidArgumentException("Parameter name must be alphanumeric string due to limitations of PCRE, '$name' given.");
  477. 477:             }
  478. 478:  
  479. 479:             // pattern, condition & metadata
  480. 480:             if ($class !== ''{
  481. 481:                 if (!isset(self::$styles[$class])) {
  482. 482:                     throw new InvalidStateException("Parameter '$name' has '$class' flag, but Route::\$styles['$class'] is not set.");
  483. 483:                 }
  484. 484:                 $meta self::$styles[$class];
  485. 485:  
  486. 486:             elseif (isset(self::$styles[$name])) {
  487. 487:                 $meta self::$styles[$name];
  488. 488:  
  489. 489:             else {
  490. 490:                 $meta self::$styles['#'];
  491. 491:             }
  492. 492:  
  493. 493:             if (isset($metadata[$name])) {
  494. 494:                 $meta $meta $metadata[$name];
  495. 495:             }
  496. 496:  
  497. 497:             if ($pattern == '' && isset($meta[self::PATTERN])) {
  498. 498:                 $pattern $meta[self::PATTERN];
  499. 499:             }
  500. 500:  
  501. 501:             $meta['filterTable2'empty($meta[self::FILTER_TABLE]NULL array_flip($meta[self::FILTER_TABLE]);
  502. 502:             if (isset($meta['default'])) {
  503. 503:                 if (isset($meta['filterTable2'][$meta['default']])) {
  504. 504:                     $meta['defOut'$meta['filterTable2'][$meta['default']];
  505. 505:  
  506. 506:                 elseif (isset($meta[self::FILTER_OUT])) {
  507. 507:                     $meta['defOut'call_user_func($meta[self::FILTER_OUT]$meta['default']);
  508. 508:  
  509. 509:                 else {
  510. 510:                     $meta['defOut'$meta['default'];
  511. 511:                 }
  512. 512:             }
  513. 513:             $meta[self::PATTERN"#(?:$pattern)$#A($this->flags self::CASE_SENSITIVE '' 'i');
  514. 514:             $metadata[$name$meta;
  515. 515:  
  516. 516:             // include in expression
  517. 517:             $tmp str_replace('-''___'$name)// dirty trick to enable '-' in parameter name
  518. 518:             if (isset($meta['fixity'])) // has default value?
  519. 519:                 if (!$optional{
  520. 520:                     throw new InvalidArgumentException("Parameter '$name' must not be optional because parameters standing on the right side are not optional.");
  521. 521:                 }
  522. 522:                 $re '(?:(?P<' $tmp '>' $pattern ')' $re ')?';
  523. 523:                 $metadata[$name]['fixity'self::PATH_OPTIONAL;
  524. 524:  
  525. 525:             else {
  526. 526:                 $optional FALSE;
  527. 527:                 $re '(?P<' $tmp '>' $pattern ')' $re;
  528. 528:             }
  529. 529:         while (TRUE);
  530. 530:  
  531. 531:         $this->re '#' $re '/?$#A' ($this->flags self::CASE_SENSITIVE '' 'i');
  532. 532:         $this->metadata $metadata;
  533. 533:         $this->sequence $sequence;
  534. 534:     }
  535. 535:  
  536. 536:  
  537. 537:  
  538. 538:     /**
  539. 539:      * Returns mask.
  540. 540:      * @return string 
  541. 541:      */
  542. 542:     public function getMask()
  543. 543:     {
  544. 544:         return $this->mask;
  545. 545:     }
  546. 546:  
  547. 547:  
  548. 548:  
  549. 549:     /**
  550. 550:      * Returns default values.
  551. 551:      * @return array 
  552. 552:      */
  553. 553:     public function getDefaults()
  554. 554:     {
  555. 555:         $defaults array();
  556. 556:         foreach ($this->metadata as $name => $meta{
  557. 557:             if (isset($meta['fixity'])) {
  558. 558:                 $defaults[$name$meta['default'];
  559. 559:             }
  560. 560:         }
  561. 561:         return $defaults;
  562. 562:     }
  563. 563:  
  564. 564:  
  565. 565:  
  566. 566:     /********************* Utilities ****************d*g**/
  567. 567:  
  568. 568:  
  569. 569:  
  570. 570:     /**
  571. 571:      * Proprietary cache aim.
  572. 572:      * @return string|FALSE
  573. 573:      */
  574. 574:     public function getTargetPresenter()
  575. 575:     {
  576. 576:         if ($this->flags self::ONE_WAY{
  577. 577:             return FALSE;
  578. 578:         }
  579. 579:  
  580. 580:         $m $this->metadata;
  581. 581:         $module '';
  582. 582:  
  583. 583:         if (isset($m[self::MODULE_KEY])) {
  584. 584:             if (isset($m[self::MODULE_KEY]['fixity']&& $m[self::MODULE_KEY]['fixity'=== self::CONSTANT{
  585. 585:                 $module $m[self::MODULE_KEY]['default'':';
  586. 586:             else {
  587. 587:                 return NULL;
  588. 588:             }
  589. 589:         }
  590. 590:  
  591. 591:         if (isset($m[self::PRESENTER_KEY]['fixity']&& $m[self::PRESENTER_KEY]['fixity'=== self::CONSTANT{
  592. 592:             return $module $m[self::PRESENTER_KEY]['default'];
  593. 593:         }
  594. 594:         return NULL;
  595. 595:     }
  596. 596:  
  597. 597:  
  598. 598:  
  599. 599:     /**
  600. 600:      * Rename keys in array.
  601. 601:      * @param  array 
  602. 602:      * @param  array 
  603. 603:      * @return array 
  604. 604:      */
  605. 605:     private static function renameKeys($arr$xlat)
  606. 606:     {
  607. 607:         if (empty($xlat)) return $arr;
  608. 608:  
  609. 609:         $res array();
  610. 610:         $occupied array_flip($xlat);
  611. 611:         foreach ($arr as $k => $v{
  612. 612:             if (isset($xlat[$k])) {
  613. 613:                 $res[$xlat[$k]] $v;
  614. 614:  
  615. 615:             elseif (!isset($occupied[$k])) {
  616. 616:                 $res[$k$v;
  617. 617:             }
  618. 618:         }
  619. 619:         return $res;
  620. 620:     }
  621. 621:  
  622. 622:  
  623. 623:  
  624. 624:     /********************* Inflectors ****************d*g**/
  625. 625:  
  626. 626:  
  627. 627:  
  628. 628:     /**
  629. 629:      * camelCaseAction name -> dash-separated.
  630. 630:      * @param  string 
  631. 631:      * @return string 
  632. 632:      */
  633. 633:     private static function action2path($s)
  634. 634:     {
  635. 635:         $s preg_replace('#(.)(?=[A-Z])#''$1-'$s);
  636. 636:         $s strtolower($s);
  637. 637:         $s rawurlencode($s);
  638. 638:         return $s;
  639. 639:     }
  640. 640:  
  641. 641:  
  642. 642:  
  643. 643:     /**
  644. 644:      * dash-separated -> camelCaseAction name.
  645. 645:      * @param  string 
  646. 646:      * @return string 
  647. 647:      */
  648. 648:     private static function path2action($s)
  649. 649:     {
  650. 650:         $s strtolower($s);
  651. 651:         $s preg_replace('#-(?=[a-z])#'' '$s);
  652. 652:         $s substr(ucwords('x' $s)1);
  653. 653:         //$s = lcfirst(ucwords($s));
  654. 654:         $s str_replace(' '''$s);
  655. 655:         return $s;
  656. 656:     }
  657. 657:  
  658. 658:  
  659. 659:  
  660. 660:     /**
  661. 661:      * PascalCase:Presenter name -> dash-and-dot-separated.
  662. 662:      * @param  string 
  663. 663:      * @return string 
  664. 664:      */
  665. 665:     private static function presenter2path($s)
  666. 666:     {
  667. 667:         $s strtr($s':''.');
  668. 668:         $s preg_replace('#([^.])(?=[A-Z])#''$1-'$s);
  669. 669:         $s strtolower($s);
  670. 670:         $s rawurlencode($s);
  671. 671:         return $s;
  672. 672:     }
  673. 673:  
  674. 674:  
  675. 675:  
  676. 676:     /**
  677. 677:      * dash-and-dot-separated -> PascalCase:Presenter name.
  678. 678:      * @param  string 
  679. 679:      * @return string 
  680. 680:      */
  681. 681:     private static function path2presenter($s)
  682. 682:     {
  683. 683:         $s strtolower($s);
  684. 684:         $s preg_replace('#([.-])(?=[a-z])#''$1 '$s);
  685. 685:         $s ucwords($s);
  686. 686:         $s str_replace('. '':'$s);
  687. 687:         $s str_replace('- '''$s);
  688. 688:         return $s;
  689. 689:     }
  690. 690:  
  691. 691:  
  692. 692:  
  693. 693:     /********************* Route::$styles manipulator ****************d*g**/
  694. 694:  
  695. 695:  
  696. 696:  
  697. 697:     /**
  698. 698:      * Creates new style.
  699. 699:      * @param  string  style name (#style, urlParameter, ?queryParameter)
  700. 700:      * @param  string  optional parent style name
  701. 701:      * @param  void 
  702. 702:      */
  703. 703:     public static function addStyle($style$parent '#')
  704. 704:     {
  705. 705:         if (isset(self::$styles[$style])) {
  706. 706:             throw new InvalidArgumentException("Style '$style' already exists.");
  707. 707:         }
  708. 708:  
  709. 709:         if ($parent !== NULL{
  710. 710:             if (!isset(self::$styles[$parent])) {
  711. 711:                 throw new InvalidArgumentException("Parent style '$parent' doesn't exist.");
  712. 712:             }
  713. 713:             self::$styles[$styleself::$styles[$parent];
  714. 714:  
  715. 715:         else {
  716. 716:             self::$styles[$stylearray();
  717. 717:         }
  718. 718:     }
  719. 719:  
  720. 720:  
  721. 721:  
  722. 722:     /**
  723. 723:      * Changes style property value.
  724. 724:      * @param  string  style name (#style, urlParameter, ?queryParameter)
  725. 725:      * @param  string  property name (Route::PATTERN, Route::FILTER_IN, Route::FILTER_OUT, Route::FILTER_TABLE)
  726. 726:      * @param  mixed   property value
  727. 727:      * @param  void 
  728. 728:      */
  729. 729:     public static function setStyleProperty($style$key$value)
  730. 730:     {
  731. 731:         if (!isset(self::$styles[$style])) {
  732. 732:             throw new InvalidArgumentException("Style '$style' doesn't exist.");
  733. 733:         }
  734. 734:         self::$styles[$style][$key$value;
  735. 735:     }
  736. 736: