説明なし

Route.php 14KB

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