1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\Routing;
13:
14: 15: 16: 17: 18: 19:
20: class Route implements \Serializable
21: {
22: 23: 24:
25: private $path = '/';
26:
27: 28: 29:
30: private $host = '';
31:
32: 33: 34:
35: private $schemes = array();
36:
37: 38: 39:
40: private $methods = array();
41:
42: 43: 44:
45: private $defaults = array();
46:
47: 48: 49:
50: private $requirements = array();
51:
52: 53: 54:
55: private $options = array();
56:
57: 58: 59:
60: private $compiled;
61:
62: 63: 64:
65: private $condition = '';
66:
67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82:
83: public function __construct($path, array $defaults = array(), array $requirements = array(), array $options = array(), $host = '', $schemes = array(), $methods = array(), $condition = '')
84: {
85: $this->setPath($path);
86: $this->setDefaults($defaults);
87: $this->setRequirements($requirements);
88: $this->setOptions($options);
89: $this->setHost($host);
90:
91:
92: if ($schemes) {
93: $this->setSchemes($schemes);
94: }
95: if ($methods) {
96: $this->setMethods($methods);
97: }
98: $this->setCondition($condition);
99: }
100:
101: 102: 103:
104: public function serialize()
105: {
106: return serialize(array(
107: 'path' => $this->path,
108: 'host' => $this->host,
109: 'defaults' => $this->defaults,
110: 'requirements' => $this->requirements,
111: 'options' => $this->options,
112: 'schemes' => $this->schemes,
113: 'methods' => $this->methods,
114: 'condition' => $this->condition,
115: 'compiled' => $this->compiled,
116: ));
117: }
118:
119: 120: 121:
122: public function unserialize($serialized)
123: {
124: $data = unserialize($serialized);
125: $this->path = $data['path'];
126: $this->host = $data['host'];
127: $this->defaults = $data['defaults'];
128: $this->requirements = $data['requirements'];
129: $this->options = $data['options'];
130: $this->schemes = $data['schemes'];
131: $this->methods = $data['methods'];
132:
133: if (isset($data['condition'])) {
134: $this->condition = $data['condition'];
135: }
136: if (isset($data['compiled'])) {
137: $this->compiled = $data['compiled'];
138: }
139: }
140:
141: 142: 143: 144: 145: 146: 147:
148: public function getPattern()
149: {
150: @trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getPath() method instead.', E_USER_DEPRECATED);
151:
152: return $this->path;
153: }
154:
155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165:
166: public function setPattern($pattern)
167: {
168: @trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the setPath() method instead.', E_USER_DEPRECATED);
169:
170: return $this->setPath($pattern);
171: }
172:
173: 174: 175: 176: 177:
178: public function getPath()
179: {
180: return $this->path;
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190: 191:
192: public function setPath($pattern)
193: {
194:
195:
196: $this->path = '/'.ltrim(trim($pattern), '/');
197: $this->compiled = null;
198:
199: return $this;
200: }
201:
202: 203: 204: 205: 206:
207: public function getHost()
208: {
209: return $this->host;
210: }
211:
212: 213: 214: 215: 216: 217: 218: 219: 220:
221: public function setHost($pattern)
222: {
223: $this->host = (string) $pattern;
224: $this->compiled = null;
225:
226: return $this;
227: }
228:
229: 230: 231: 232: 233: 234:
235: public function getSchemes()
236: {
237: return $this->schemes;
238: }
239:
240: 241: 242: 243: 244: 245: 246: 247: 248: 249:
250: public function setSchemes($schemes)
251: {
252: $this->schemes = array_map('strtolower', (array) $schemes);
253:
254:
255: if ($this->schemes) {
256: $this->requirements['_scheme'] = implode('|', $this->schemes);
257: } else {
258: unset($this->requirements['_scheme']);
259: }
260:
261: $this->compiled = null;
262:
263: return $this;
264: }
265:
266: 267: 268: 269: 270: 271: 272:
273: public function hasScheme($scheme)
274: {
275: return in_array(strtolower($scheme), $this->schemes, true);
276: }
277:
278: 279: 280: 281: 282: 283:
284: public function getMethods()
285: {
286: return $this->methods;
287: }
288:
289: 290: 291: 292: 293: 294: 295: 296: 297: 298:
299: public function setMethods($methods)
300: {
301: $this->methods = array_map('strtoupper', (array) $methods);
302:
303:
304: if ($this->methods) {
305: $this->requirements['_method'] = implode('|', $this->methods);
306: } else {
307: unset($this->requirements['_method']);
308: }
309:
310: $this->compiled = null;
311:
312: return $this;
313: }
314:
315: 316: 317: 318: 319:
320: public function getOptions()
321: {
322: return $this->options;
323: }
324:
325: 326: 327: 328: 329: 330: 331: 332: 333:
334: public function setOptions(array $options)
335: {
336: $this->options = array(
337: 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
338: );
339:
340: return $this->addOptions($options);
341: }
342:
343: 344: 345: 346: 347: 348: 349: 350: 351:
352: public function addOptions(array $options)
353: {
354: foreach ($options as $name => $option) {
355: $this->options[$name] = $option;
356: }
357: $this->compiled = null;
358:
359: return $this;
360: }
361:
362: 363: 364: 365: 366: 367: 368: 369: 370: 371:
372: public function setOption($name, $value)
373: {
374: $this->options[$name] = $value;
375: $this->compiled = null;
376:
377: return $this;
378: }
379:
380: 381: 382: 383: 384: 385: 386:
387: public function getOption($name)
388: {
389: return isset($this->options[$name]) ? $this->options[$name] : null;
390: }
391:
392: 393: 394: 395: 396: 397: 398:
399: public function hasOption($name)
400: {
401: return array_key_exists($name, $this->options);
402: }
403:
404: 405: 406: 407: 408:
409: public function getDefaults()
410: {
411: return $this->defaults;
412: }
413:
414: 415: 416: 417: 418: 419: 420: 421: 422:
423: public function setDefaults(array $defaults)
424: {
425: $this->defaults = array();
426:
427: return $this->addDefaults($defaults);
428: }
429:
430: 431: 432: 433: 434: 435: 436: 437: 438:
439: public function addDefaults(array $defaults)
440: {
441: foreach ($defaults as $name => $default) {
442: $this->defaults[$name] = $default;
443: }
444: $this->compiled = null;
445:
446: return $this;
447: }
448:
449: 450: 451: 452: 453: 454: 455:
456: public function getDefault($name)
457: {
458: return isset($this->defaults[$name]) ? $this->defaults[$name] : null;
459: }
460:
461: 462: 463: 464: 465: 466: 467:
468: public function hasDefault($name)
469: {
470: return array_key_exists($name, $this->defaults);
471: }
472:
473: 474: 475: 476: 477: 478: 479: 480:
481: public function setDefault($name, $default)
482: {
483: $this->defaults[$name] = $default;
484: $this->compiled = null;
485:
486: return $this;
487: }
488:
489: 490: 491: 492: 493:
494: public function getRequirements()
495: {
496: return $this->requirements;
497: }
498:
499: 500: 501: 502: 503: 504: 505: 506: 507:
508: public function setRequirements(array $requirements)
509: {
510: $this->requirements = array();
511:
512: return $this->addRequirements($requirements);
513: }
514:
515: 516: 517: 518: 519: 520: 521: 522: 523:
524: public function addRequirements(array $requirements)
525: {
526: foreach ($requirements as $key => $regex) {
527: $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
528: }
529: $this->compiled = null;
530:
531: return $this;
532: }
533:
534: 535: 536: 537: 538: 539: 540:
541: public function getRequirement($key)
542: {
543: if ('_scheme' === $key) {
544: @trigger_error('The "_scheme" requirement is deprecated since version 2.2 and will be removed in 3.0. Use getSchemes() instead.', E_USER_DEPRECATED);
545: } elseif ('_method' === $key) {
546: @trigger_error('The "_method" requirement is deprecated since version 2.2 and will be removed in 3.0. Use getMethods() instead.', E_USER_DEPRECATED);
547: }
548:
549: return isset($this->requirements[$key]) ? $this->requirements[$key] : null;
550: }
551:
552: 553: 554: 555: 556: 557: 558:
559: public function hasRequirement($key)
560: {
561: return array_key_exists($key, $this->requirements);
562: }
563:
564: 565: 566: 567: 568: 569: 570: 571:
572: public function setRequirement($key, $regex)
573: {
574: $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
575: $this->compiled = null;
576:
577: return $this;
578: }
579:
580: 581: 582: 583: 584:
585: public function getCondition()
586: {
587: return $this->condition;
588: }
589:
590: 591: 592: 593: 594: 595: 596: 597: 598:
599: public function setCondition($condition)
600: {
601: $this->condition = (string) $condition;
602: $this->compiled = null;
603:
604: return $this;
605: }
606:
607: 608: 609: 610: 611: 612: 613: 614: 615: 616:
617: public function compile()
618: {
619: if (null !== $this->compiled) {
620: return $this->compiled;
621: }
622:
623: $class = $this->getOption('compiler_class');
624:
625: return $this->compiled = $class::compile($this);
626: }
627:
628: private function sanitizeRequirement($key, $regex)
629: {
630: if (!is_string($regex)) {
631: throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
632: }
633:
634: if ('' !== $regex && '^' === $regex[0]) {
635: $regex = (string) substr($regex, 1);
636: }
637:
638: if ('$' === substr($regex, -1)) {
639: $regex = substr($regex, 0, -1);
640: }
641:
642: if ('' === $regex) {
643: throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));
644: }
645:
646:
647: if ('_scheme' === $key) {
648: @trigger_error('The "_scheme" requirement is deprecated since version 2.2 and will be removed in 3.0. Use the setSchemes() method instead.', E_USER_DEPRECATED);
649:
650: $this->setSchemes(explode('|', $regex));
651: } elseif ('_method' === $key) {
652: @trigger_error('The "_method" requirement is deprecated since version 2.2 and will be removed in 3.0. Use the setMethods() method instead.', E_USER_DEPRECATED);
653:
654: $this->setMethods(explode('|', $regex));
655: }
656:
657: return $regex;
658: }
659: }
660: