Route.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing;
  11. /**
  12. * A Route describes a route and its parameters.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @author Tobias Schultze <http://tobion.de>
  16. */
  17. class Route implements \Serializable
  18. {
  19. private $path = '/';
  20. private $host = '';
  21. private $schemes = array();
  22. private $methods = array();
  23. private $defaults = array();
  24. private $requirements = array();
  25. private $options = array();
  26. private $condition = '';
  27. /**
  28. * @var null|CompiledRoute
  29. */
  30. private $compiled;
  31. /**
  32. * Constructor.
  33. *
  34. * Available options:
  35. *
  36. * * compiler_class: A class name able to compile this route instance (RouteCompiler by default)
  37. * * utf8: Whether UTF-8 matching is enforced ot not
  38. *
  39. * @param string $path The path pattern to match
  40. * @param array $defaults An array of default parameter values
  41. * @param array $requirements An array of requirements for parameters (regexes)
  42. * @param array $options An array of options
  43. * @param string $host The host pattern to match
  44. * @param string|string[] $schemes A required URI scheme or an array of restricted schemes
  45. * @param string|string[] $methods A required HTTP method or an array of restricted methods
  46. * @param string $condition A condition that should evaluate to true for the route to match
  47. */
  48. public function __construct($path, array $defaults = array(), array $requirements = array(), array $options = array(), $host = '', $schemes = array(), $methods = array(), $condition = '')
  49. {
  50. $this->setPath($path);
  51. $this->setDefaults($defaults);
  52. $this->setRequirements($requirements);
  53. $this->setOptions($options);
  54. $this->setHost($host);
  55. $this->setSchemes($schemes);
  56. $this->setMethods($methods);
  57. $this->setCondition($condition);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function serialize()
  63. {
  64. return serialize(array(
  65. 'path' => $this->path,
  66. 'host' => $this->host,
  67. 'defaults' => $this->defaults,
  68. 'requirements' => $this->requirements,
  69. 'options' => $this->options,
  70. 'schemes' => $this->schemes,
  71. 'methods' => $this->methods,
  72. 'condition' => $this->condition,
  73. 'compiled' => $this->compiled,
  74. ));
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function unserialize($serialized)
  80. {
  81. $data = unserialize($serialized);
  82. $this->path = $data['path'];
  83. $this->host = $data['host'];
  84. $this->defaults = $data['defaults'];
  85. $this->requirements = $data['requirements'];
  86. $this->options = $data['options'];
  87. $this->schemes = $data['schemes'];
  88. $this->methods = $data['methods'];
  89. if (isset($data['condition'])) {
  90. $this->condition = $data['condition'];
  91. }
  92. if (isset($data['compiled'])) {
  93. $this->compiled = $data['compiled'];
  94. }
  95. }
  96. /**
  97. * Returns the pattern for the path.
  98. *
  99. * @return string The path pattern
  100. */
  101. public function getPath()
  102. {
  103. return $this->path;
  104. }
  105. /**
  106. * Sets the pattern for the path.
  107. *
  108. * This method implements a fluent interface.
  109. *
  110. * @param string $pattern The path pattern
  111. *
  112. * @return $this
  113. */
  114. public function setPath($pattern)
  115. {
  116. // A pattern must start with a slash and must not have multiple slashes at the beginning because the
  117. // generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
  118. $this->path = '/'.ltrim(trim($pattern), '/');
  119. $this->compiled = null;
  120. return $this;
  121. }
  122. /**
  123. * Returns the pattern for the host.
  124. *
  125. * @return string The host pattern
  126. */
  127. public function getHost()
  128. {
  129. return $this->host;
  130. }
  131. /**
  132. * Sets the pattern for the host.
  133. *
  134. * This method implements a fluent interface.
  135. *
  136. * @param string $pattern The host pattern
  137. *
  138. * @return $this
  139. */
  140. public function setHost($pattern)
  141. {
  142. $this->host = (string) $pattern;
  143. $this->compiled = null;
  144. return $this;
  145. }
  146. /**
  147. * Returns the lowercased schemes this route is restricted to.
  148. * So an empty array means that any scheme is allowed.
  149. *
  150. * @return string[] The schemes
  151. */
  152. public function getSchemes()
  153. {
  154. return $this->schemes;
  155. }
  156. /**
  157. * Sets the schemes (e.g. 'https') this route is restricted to.
  158. * So an empty array means that any scheme is allowed.
  159. *
  160. * This method implements a fluent interface.
  161. *
  162. * @param string|string[] $schemes The scheme or an array of schemes
  163. *
  164. * @return $this
  165. */
  166. public function setSchemes($schemes)
  167. {
  168. $this->schemes = array_map('strtolower', (array) $schemes);
  169. $this->compiled = null;
  170. return $this;
  171. }
  172. /**
  173. * Checks if a scheme requirement has been set.
  174. *
  175. * @param string $scheme
  176. *
  177. * @return bool true if the scheme requirement exists, otherwise false
  178. */
  179. public function hasScheme($scheme)
  180. {
  181. return in_array(strtolower($scheme), $this->schemes, true);
  182. }
  183. /**
  184. * Returns the uppercased HTTP methods this route is restricted to.
  185. * So an empty array means that any method is allowed.
  186. *
  187. * @return string[] The methods
  188. */
  189. public function getMethods()
  190. {
  191. return $this->methods;
  192. }
  193. /**
  194. * Sets the HTTP methods (e.g. 'POST') this route is restricted to.
  195. * So an empty array means that any method is allowed.
  196. *
  197. * This method implements a fluent interface.
  198. *
  199. * @param string|string[] $methods The method or an array of methods
  200. *
  201. * @return $this
  202. */
  203. public function setMethods($methods)
  204. {
  205. $this->methods = array_map('strtoupper', (array) $methods);
  206. $this->compiled = null;
  207. return $this;
  208. }
  209. /**
  210. * Returns the options.
  211. *
  212. * @return array The options
  213. */
  214. public function getOptions()
  215. {
  216. return $this->options;
  217. }
  218. /**
  219. * Sets the options.
  220. *
  221. * This method implements a fluent interface.
  222. *
  223. * @param array $options The options
  224. *
  225. * @return $this
  226. */
  227. public function setOptions(array $options)
  228. {
  229. $this->options = array(
  230. 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
  231. );
  232. return $this->addOptions($options);
  233. }
  234. /**
  235. * Adds options.
  236. *
  237. * This method implements a fluent interface.
  238. *
  239. * @param array $options The options
  240. *
  241. * @return $this
  242. */
  243. public function addOptions(array $options)
  244. {
  245. foreach ($options as $name => $option) {
  246. $this->options[$name] = $option;
  247. }
  248. $this->compiled = null;
  249. return $this;
  250. }
  251. /**
  252. * Sets an option value.
  253. *
  254. * This method implements a fluent interface.
  255. *
  256. * @param string $name An option name
  257. * @param mixed $value The option value
  258. *
  259. * @return $this
  260. */
  261. public function setOption($name, $value)
  262. {
  263. $this->options[$name] = $value;
  264. $this->compiled = null;
  265. return $this;
  266. }
  267. /**
  268. * Get an option value.
  269. *
  270. * @param string $name An option name
  271. *
  272. * @return mixed The option value or null when not given
  273. */
  274. public function getOption($name)
  275. {
  276. return isset($this->options[$name]) ? $this->options[$name] : null;
  277. }
  278. /**
  279. * Checks if an option has been set.
  280. *
  281. * @param string $name An option name
  282. *
  283. * @return bool true if the option is set, false otherwise
  284. */
  285. public function hasOption($name)
  286. {
  287. return array_key_exists($name, $this->options);
  288. }
  289. /**
  290. * Returns the defaults.
  291. *
  292. * @return array The defaults
  293. */
  294. public function getDefaults()
  295. {
  296. return $this->defaults;
  297. }
  298. /**
  299. * Sets the defaults.
  300. *
  301. * This method implements a fluent interface.
  302. *
  303. * @param array $defaults The defaults
  304. *
  305. * @return $this
  306. */
  307. public function setDefaults(array $defaults)
  308. {
  309. $this->defaults = array();
  310. return $this->addDefaults($defaults);
  311. }
  312. /**
  313. * Adds defaults.
  314. *
  315. * This method implements a fluent interface.
  316. *
  317. * @param array $defaults The defaults
  318. *
  319. * @return $this
  320. */
  321. public function addDefaults(array $defaults)
  322. {
  323. foreach ($defaults as $name => $default) {
  324. $this->defaults[$name] = $default;
  325. }
  326. $this->compiled = null;
  327. return $this;
  328. }
  329. /**
  330. * Gets a default value.
  331. *
  332. * @param string $name A variable name
  333. *
  334. * @return mixed The default value or null when not given
  335. */
  336. public function getDefault($name)
  337. {
  338. return isset($this->defaults[$name]) ? $this->defaults[$name] : null;
  339. }
  340. /**
  341. * Checks if a default value is set for the given variable.
  342. *
  343. * @param string $name A variable name
  344. *
  345. * @return bool true if the default value is set, false otherwise
  346. */
  347. public function hasDefault($name)
  348. {
  349. return array_key_exists($name, $this->defaults);
  350. }
  351. /**
  352. * Sets a default value.
  353. *
  354. * @param string $name A variable name
  355. * @param mixed $default The default value
  356. *
  357. * @return $this
  358. */
  359. public function setDefault($name, $default)
  360. {
  361. $this->defaults[$name] = $default;
  362. $this->compiled = null;
  363. return $this;
  364. }
  365. /**
  366. * Returns the requirements.
  367. *
  368. * @return array The requirements
  369. */
  370. public function getRequirements()
  371. {
  372. return $this->requirements;
  373. }
  374. /**
  375. * Sets the requirements.
  376. *
  377. * This method implements a fluent interface.
  378. *
  379. * @param array $requirements The requirements
  380. *
  381. * @return $this
  382. */
  383. public function setRequirements(array $requirements)
  384. {
  385. $this->requirements = array();
  386. return $this->addRequirements($requirements);
  387. }
  388. /**
  389. * Adds requirements.
  390. *
  391. * This method implements a fluent interface.
  392. *
  393. * @param array $requirements The requirements
  394. *
  395. * @return $this
  396. */
  397. public function addRequirements(array $requirements)
  398. {
  399. foreach ($requirements as $key => $regex) {
  400. $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
  401. }
  402. $this->compiled = null;
  403. return $this;
  404. }
  405. /**
  406. * Returns the requirement for the given key.
  407. *
  408. * @param string $key The key
  409. *
  410. * @return string|null The regex or null when not given
  411. */
  412. public function getRequirement($key)
  413. {
  414. return isset($this->requirements[$key]) ? $this->requirements[$key] : null;
  415. }
  416. /**
  417. * Checks if a requirement is set for the given key.
  418. *
  419. * @param string $key A variable name
  420. *
  421. * @return bool true if a requirement is specified, false otherwise
  422. */
  423. public function hasRequirement($key)
  424. {
  425. return array_key_exists($key, $this->requirements);
  426. }
  427. /**
  428. * Sets a requirement for the given key.
  429. *
  430. * @param string $key The key
  431. * @param string $regex The regex
  432. *
  433. * @return $this
  434. */
  435. public function setRequirement($key, $regex)
  436. {
  437. $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
  438. $this->compiled = null;
  439. return $this;
  440. }
  441. /**
  442. * Returns the condition.
  443. *
  444. * @return string The condition
  445. */
  446. public function getCondition()
  447. {
  448. return $this->condition;
  449. }
  450. /**
  451. * Sets the condition.
  452. *
  453. * This method implements a fluent interface.
  454. *
  455. * @param string $condition The condition
  456. *
  457. * @return $this
  458. */
  459. public function setCondition($condition)
  460. {
  461. $this->condition = (string) $condition;
  462. $this->compiled = null;
  463. return $this;
  464. }
  465. /**
  466. * Compiles the route.
  467. *
  468. * @return CompiledRoute A CompiledRoute instance
  469. *
  470. * @throws \LogicException If the Route cannot be compiled because the
  471. * path or host pattern is invalid
  472. *
  473. * @see RouteCompiler which is responsible for the compilation process
  474. */
  475. public function compile()
  476. {
  477. if (null !== $this->compiled) {
  478. return $this->compiled;
  479. }
  480. $class = $this->getOption('compiler_class');
  481. return $this->compiled = $class::compile($this);
  482. }
  483. private function sanitizeRequirement($key, $regex)
  484. {
  485. if (!is_string($regex)) {
  486. throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
  487. }
  488. if ('' !== $regex && '^' === $regex[0]) {
  489. $regex = (string) substr($regex, 1); // returns false for a single character
  490. }
  491. if ('$' === substr($regex, -1)) {
  492. $regex = substr($regex, 0, -1);
  493. }
  494. if ('' === $regex) {
  495. throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));
  496. }
  497. return $regex;
  498. }
  499. }