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