Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

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