Route.php 14 KB

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