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