PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Routing/Route.php

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