No Description

Route.php 16KB

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