菜谱项目

Route.php 14KB

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