Route.php 14 KB

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