Dashboard sipadu mbip
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Route.php 14KB

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