4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing;
15 * A Route describes a route and its parameters.
17 * @author Fabien Potencier <fabien@symfony.com>
18 * @author Tobias Schultze <http://tobion.de>
20 class Route implements \Serializable
35 private $schemes = array();
40 private $methods = array();
45 private $defaults = array();
50 private $requirements = array();
55 private $options = array();
58 * @var null|CompiledRoute
65 private $condition = '';
72 * * compiler_class: A class name able to compile this route instance (RouteCompiler by default)
73 * * utf8: Whether UTF-8 matching is enforced ot not
75 * @param string $path The path pattern to match
76 * @param array $defaults An array of default parameter values
77 * @param array $requirements An array of requirements for parameters (regexes)
78 * @param array $options An array of options
79 * @param string $host The host pattern to match
80 * @param string|array $schemes A required URI scheme or an array of restricted schemes
81 * @param string|array $methods A required HTTP method or an array of restricted methods
82 * @param string $condition A condition that should evaluate to true for the route to match
84 public function __construct($path, array $defaults = array(), array $requirements = array(), array $options = array(), $host = '', $schemes = array(), $methods = array(), $condition = '')
86 $this->setPath($path);
87 $this->setDefaults($defaults);
88 $this->setRequirements($requirements);
89 $this->setOptions($options);
90 $this->setHost($host);
91 $this->setSchemes($schemes);
92 $this->setMethods($methods);
93 $this->setCondition($condition);
99 public function serialize()
101 return serialize(array(
102 'path' => $this->path,
103 'host' => $this->host,
104 'defaults' => $this->defaults,
105 'requirements' => $this->requirements,
106 'options' => $this->options,
107 'schemes' => $this->schemes,
108 'methods' => $this->methods,
109 'condition' => $this->condition,
110 'compiled' => $this->compiled,
117 public function unserialize($serialized)
119 $data = unserialize($serialized);
120 $this->path = $data['path'];
121 $this->host = $data['host'];
122 $this->defaults = $data['defaults'];
123 $this->requirements = $data['requirements'];
124 $this->options = $data['options'];
125 $this->schemes = $data['schemes'];
126 $this->methods = $data['methods'];
128 if (isset($data['condition'])) {
129 $this->condition = $data['condition'];
131 if (isset($data['compiled'])) {
132 $this->compiled = $data['compiled'];
137 * Returns the pattern for the path.
139 * @return string The path pattern
141 public function getPath()
147 * Sets the pattern for the path.
149 * This method implements a fluent interface.
151 * @param string $pattern The path pattern
155 public function setPath($pattern)
157 // A pattern must start with a slash and must not have multiple slashes at the beginning because the
158 // generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
159 $this->path = '/'.ltrim(trim($pattern), '/');
160 $this->compiled = null;
166 * Returns the pattern for the host.
168 * @return string The host pattern
170 public function getHost()
176 * Sets the pattern for the host.
178 * This method implements a fluent interface.
180 * @param string $pattern The host pattern
184 public function setHost($pattern)
186 $this->host = (string) $pattern;
187 $this->compiled = null;
193 * Returns the lowercased schemes this route is restricted to.
194 * So an empty array means that any scheme is allowed.
196 * @return array The schemes
198 public function getSchemes()
200 return $this->schemes;
204 * Sets the schemes (e.g. 'https') this route is restricted to.
205 * So an empty array means that any scheme is allowed.
207 * This method implements a fluent interface.
209 * @param string|array $schemes The scheme or an array of schemes
213 public function setSchemes($schemes)
215 $this->schemes = array_map('strtolower', (array) $schemes);
216 $this->compiled = null;
222 * Checks if a scheme requirement has been set.
224 * @param string $scheme
226 * @return bool true if the scheme requirement exists, otherwise false
228 public function hasScheme($scheme)
230 return in_array(strtolower($scheme), $this->schemes, true);
234 * Returns the uppercased HTTP methods this route is restricted to.
235 * So an empty array means that any method is allowed.
237 * @return array The methods
239 public function getMethods()
241 return $this->methods;
245 * Sets the HTTP methods (e.g. 'POST') this route is restricted to.
246 * So an empty array means that any method is allowed.
248 * This method implements a fluent interface.
250 * @param string|array $methods The method or an array of methods
254 public function setMethods($methods)
256 $this->methods = array_map('strtoupper', (array) $methods);
257 $this->compiled = null;
263 * Returns the options.
265 * @return array The options
267 public function getOptions()
269 return $this->options;
275 * This method implements a fluent interface.
277 * @param array $options The options
281 public function setOptions(array $options)
283 $this->options = array(
284 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
287 return $this->addOptions($options);
293 * This method implements a fluent interface.
295 * @param array $options The options
299 public function addOptions(array $options)
301 foreach ($options as $name => $option) {
302 $this->options[$name] = $option;
304 $this->compiled = null;
310 * Sets an option value.
312 * This method implements a fluent interface.
314 * @param string $name An option name
315 * @param mixed $value The option value
319 public function setOption($name, $value)
321 $this->options[$name] = $value;
322 $this->compiled = null;
328 * Get an option value.
330 * @param string $name An option name
332 * @return mixed The option value or null when not given
334 public function getOption($name)
336 return isset($this->options[$name]) ? $this->options[$name] : null;
340 * Checks if an option has been set.
342 * @param string $name An option name
344 * @return bool true if the option is set, false otherwise
346 public function hasOption($name)
348 return array_key_exists($name, $this->options);
352 * Returns the defaults.
354 * @return array The defaults
356 public function getDefaults()
358 return $this->defaults;
364 * This method implements a fluent interface.
366 * @param array $defaults The defaults
370 public function setDefaults(array $defaults)
372 $this->defaults = array();
374 return $this->addDefaults($defaults);
380 * This method implements a fluent interface.
382 * @param array $defaults The defaults
386 public function addDefaults(array $defaults)
388 foreach ($defaults as $name => $default) {
389 $this->defaults[$name] = $default;
391 $this->compiled = null;
397 * Gets a default value.
399 * @param string $name A variable name
401 * @return mixed The default value or null when not given
403 public function getDefault($name)
405 return isset($this->defaults[$name]) ? $this->defaults[$name] : null;
409 * Checks if a default value is set for the given variable.
411 * @param string $name A variable name
413 * @return bool true if the default value is set, false otherwise
415 public function hasDefault($name)
417 return array_key_exists($name, $this->defaults);
421 * Sets a default value.
423 * @param string $name A variable name
424 * @param mixed $default The default value
428 public function setDefault($name, $default)
430 $this->defaults[$name] = $default;
431 $this->compiled = null;
437 * Returns the requirements.
439 * @return array The requirements
441 public function getRequirements()
443 return $this->requirements;
447 * Sets the requirements.
449 * This method implements a fluent interface.
451 * @param array $requirements The requirements
455 public function setRequirements(array $requirements)
457 $this->requirements = array();
459 return $this->addRequirements($requirements);
465 * This method implements a fluent interface.
467 * @param array $requirements The requirements
471 public function addRequirements(array $requirements)
473 foreach ($requirements as $key => $regex) {
474 $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
476 $this->compiled = null;
482 * Returns the requirement for the given key.
484 * @param string $key The key
486 * @return string|null The regex or null when not given
488 public function getRequirement($key)
490 return isset($this->requirements[$key]) ? $this->requirements[$key] : null;
494 * Checks if a requirement is set for the given key.
496 * @param string $key A variable name
498 * @return bool true if a requirement is specified, false otherwise
500 public function hasRequirement($key)
502 return array_key_exists($key, $this->requirements);
506 * Sets a requirement for the given key.
508 * @param string $key The key
509 * @param string $regex The regex
513 public function setRequirement($key, $regex)
515 $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
516 $this->compiled = null;
522 * Returns the condition.
524 * @return string The condition
526 public function getCondition()
528 return $this->condition;
532 * Sets the condition.
534 * This method implements a fluent interface.
536 * @param string $condition The condition
540 public function setCondition($condition)
542 $this->condition = (string) $condition;
543 $this->compiled = null;
549 * Compiles the route.
551 * @return CompiledRoute A CompiledRoute instance
553 * @throws \LogicException If the Route cannot be compiled because the
554 * path or host pattern is invalid
556 * @see RouteCompiler which is responsible for the compilation process
558 public function compile()
560 if (null !== $this->compiled) {
561 return $this->compiled;
564 $class = $this->getOption('compiler_class');
566 return $this->compiled = $class::compile($this);
569 private function sanitizeRequirement($key, $regex)
571 if (!is_string($regex)) {
572 throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
575 if ('' !== $regex && '^' === $regex[0]) {
576 $regex = (string) substr($regex, 1); // returns false for a single character
579 if ('$' === substr($regex, -1)) {
580 $regex = substr($regex, 0, -1);
584 throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));