Route.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. if (isset($defaults['_locale']) && $this->isLocalized()) {
  342. unset($defaults['_locale']);
  343. }
  344. foreach ($defaults as $name => $default) {
  345. $this->defaults[$name] = $default;
  346. }
  347. $this->compiled = null;
  348. return $this;
  349. }
  350. /**
  351. * Gets a default value.
  352. *
  353. * @param string $name A variable name
  354. *
  355. * @return mixed The default value or null when not given
  356. */
  357. public function getDefault($name)
  358. {
  359. return isset($this->defaults[$name]) ? $this->defaults[$name] : null;
  360. }
  361. /**
  362. * Checks if a default value is set for the given variable.
  363. *
  364. * @param string $name A variable name
  365. *
  366. * @return bool true if the default value is set, false otherwise
  367. */
  368. public function hasDefault($name)
  369. {
  370. return \array_key_exists($name, $this->defaults);
  371. }
  372. /**
  373. * Sets a default value.
  374. *
  375. * @param string $name A variable name
  376. * @param mixed $default The default value
  377. *
  378. * @return $this
  379. */
  380. public function setDefault($name, $default)
  381. {
  382. if ('_locale' === $name && $this->isLocalized()) {
  383. return $this;
  384. }
  385. $this->defaults[$name] = $default;
  386. $this->compiled = null;
  387. return $this;
  388. }
  389. /**
  390. * Returns the requirements.
  391. *
  392. * @return array The requirements
  393. */
  394. public function getRequirements()
  395. {
  396. return $this->requirements;
  397. }
  398. /**
  399. * Sets the requirements.
  400. *
  401. * This method implements a fluent interface.
  402. *
  403. * @param array $requirements The requirements
  404. *
  405. * @return $this
  406. */
  407. public function setRequirements(array $requirements)
  408. {
  409. $this->requirements = [];
  410. return $this->addRequirements($requirements);
  411. }
  412. /**
  413. * Adds requirements.
  414. *
  415. * This method implements a fluent interface.
  416. *
  417. * @param array $requirements The requirements
  418. *
  419. * @return $this
  420. */
  421. public function addRequirements(array $requirements)
  422. {
  423. if (isset($requirements['_locale']) && $this->isLocalized()) {
  424. unset($requirements['_locale']);
  425. }
  426. foreach ($requirements as $key => $regex) {
  427. $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
  428. }
  429. $this->compiled = null;
  430. return $this;
  431. }
  432. /**
  433. * Returns the requirement for the given key.
  434. *
  435. * @param string $key The key
  436. *
  437. * @return string|null The regex or null when not given
  438. */
  439. public function getRequirement($key)
  440. {
  441. return isset($this->requirements[$key]) ? $this->requirements[$key] : null;
  442. }
  443. /**
  444. * Checks if a requirement is set for the given key.
  445. *
  446. * @param string $key A variable name
  447. *
  448. * @return bool true if a requirement is specified, false otherwise
  449. */
  450. public function hasRequirement($key)
  451. {
  452. return \array_key_exists($key, $this->requirements);
  453. }
  454. /**
  455. * Sets a requirement for the given key.
  456. *
  457. * @param string $key The key
  458. * @param string $regex The regex
  459. *
  460. * @return $this
  461. */
  462. public function setRequirement($key, $regex)
  463. {
  464. if ('_locale' === $key && $this->isLocalized()) {
  465. return $this;
  466. }
  467. $this->requirements[$key] = $this->sanitizeRequirement($key, $regex);
  468. $this->compiled = null;
  469. return $this;
  470. }
  471. /**
  472. * Returns the condition.
  473. *
  474. * @return string The condition
  475. */
  476. public function getCondition()
  477. {
  478. return $this->condition;
  479. }
  480. /**
  481. * Sets the condition.
  482. *
  483. * This method implements a fluent interface.
  484. *
  485. * @param string $condition The condition
  486. *
  487. * @return $this
  488. */
  489. public function setCondition($condition)
  490. {
  491. $this->condition = (string) $condition;
  492. $this->compiled = null;
  493. return $this;
  494. }
  495. /**
  496. * Compiles the route.
  497. *
  498. * @return CompiledRoute A CompiledRoute instance
  499. *
  500. * @throws \LogicException If the Route cannot be compiled because the
  501. * path or host pattern is invalid
  502. *
  503. * @see RouteCompiler which is responsible for the compilation process
  504. */
  505. public function compile()
  506. {
  507. if (null !== $this->compiled) {
  508. return $this->compiled;
  509. }
  510. $class = $this->getOption('compiler_class');
  511. return $this->compiled = $class::compile($this);
  512. }
  513. private function sanitizeRequirement(string $key, $regex)
  514. {
  515. if (!\is_string($regex)) {
  516. throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
  517. }
  518. if ('' !== $regex && '^' === $regex[0]) {
  519. $regex = (string) substr($regex, 1); // returns false for a single character
  520. }
  521. if ('$' === substr($regex, -1)) {
  522. $regex = substr($regex, 0, -1);
  523. }
  524. if ('' === $regex) {
  525. throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));
  526. }
  527. return $regex;
  528. }
  529. private function isLocalized(): bool
  530. {
  531. return isset($this->defaults['_locale']) && isset($this->defaults['_canonical_route']) && ($this->requirements['_locale'] ?? null) === preg_quote($this->defaults['_locale'], RouteCompiler::REGEX_DELIMITER);
  532. }
  533. }