PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/routing/Route.php

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