菜谱项目

RouteCollectionBuilder.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. use Symfony\Component\Config\Exception\FileLoaderLoadException;
  12. use Symfony\Component\Config\Loader\LoaderInterface;
  13. use Symfony\Component\Config\Resource\ResourceInterface;
  14. /**
  15. * Helps add and import routes into a RouteCollection.
  16. *
  17. * @author Ryan Weaver <ryan@knpuniversity.com>
  18. */
  19. class RouteCollectionBuilder
  20. {
  21. /**
  22. * @var Route[]|RouteCollectionBuilder[]
  23. */
  24. private $routes = array();
  25. private $loader;
  26. private $defaults = array();
  27. private $prefix;
  28. private $host;
  29. private $condition;
  30. private $requirements = array();
  31. private $options = array();
  32. private $schemes;
  33. private $methods;
  34. private $resources = array();
  35. public function __construct(LoaderInterface $loader = null)
  36. {
  37. $this->loader = $loader;
  38. }
  39. /**
  40. * Import an external routing resource and returns the RouteCollectionBuilder.
  41. *
  42. * $routes->import('blog.yml', '/blog');
  43. *
  44. * @param mixed $resource
  45. * @param string|null $prefix
  46. * @param string $type
  47. *
  48. * @return self
  49. *
  50. * @throws FileLoaderLoadException
  51. */
  52. public function import($resource, $prefix = '/', $type = null)
  53. {
  54. /** @var RouteCollection[] $collection */
  55. $collections = $this->load($resource, $type);
  56. // create a builder from the RouteCollection
  57. $builder = $this->createBuilder();
  58. foreach ($collections as $collection) {
  59. if (null === $collection) {
  60. continue;
  61. }
  62. foreach ($collection->all() as $name => $route) {
  63. $builder->addRoute($route, $name);
  64. }
  65. foreach ($collection->getResources() as $resource) {
  66. $builder->addResource($resource);
  67. }
  68. // mount into this builder
  69. $this->mount($prefix, $builder);
  70. }
  71. return $builder;
  72. }
  73. /**
  74. * Adds a route and returns it for future modification.
  75. *
  76. * @param string $path The route path
  77. * @param string $controller The route's controller
  78. * @param string|null $name The name to give this route
  79. *
  80. * @return Route
  81. */
  82. public function add($path, $controller, $name = null)
  83. {
  84. $route = new Route($path);
  85. $route->setDefault('_controller', $controller);
  86. $this->addRoute($route, $name);
  87. return $route;
  88. }
  89. /**
  90. * Returns a RouteCollectionBuilder that can be configured and then added with mount().
  91. *
  92. * @return self
  93. */
  94. public function createBuilder()
  95. {
  96. return new self($this->loader);
  97. }
  98. /**
  99. * Add a RouteCollectionBuilder.
  100. *
  101. * @param string $prefix
  102. * @param RouteCollectionBuilder $builder
  103. */
  104. public function mount($prefix, RouteCollectionBuilder $builder)
  105. {
  106. $builder->prefix = trim(trim($prefix), '/');
  107. $this->routes[] = $builder;
  108. }
  109. /**
  110. * Adds a Route object to the builder.
  111. *
  112. * @param Route $route
  113. * @param string|null $name
  114. *
  115. * @return $this
  116. */
  117. public function addRoute(Route $route, $name = null)
  118. {
  119. if (null === $name) {
  120. // used as a flag to know which routes will need a name later
  121. $name = '_unnamed_route_'.spl_object_hash($route);
  122. }
  123. $this->routes[$name] = $route;
  124. return $this;
  125. }
  126. /**
  127. * Sets the host on all embedded routes (unless already set).
  128. *
  129. * @param string $pattern
  130. *
  131. * @return $this
  132. */
  133. public function setHost($pattern)
  134. {
  135. $this->host = $pattern;
  136. return $this;
  137. }
  138. /**
  139. * Sets a condition on all embedded routes (unless already set).
  140. *
  141. * @param string $condition
  142. *
  143. * @return $this
  144. */
  145. public function setCondition($condition)
  146. {
  147. $this->condition = $condition;
  148. return $this;
  149. }
  150. /**
  151. * Sets a default value that will be added to all embedded routes (unless that
  152. * default value is already set).
  153. *
  154. * @param string $key
  155. * @param mixed $value
  156. *
  157. * @return $this
  158. */
  159. public function setDefault($key, $value)
  160. {
  161. $this->defaults[$key] = $value;
  162. return $this;
  163. }
  164. /**
  165. * Sets a requirement that will be added to all embedded routes (unless that
  166. * requirement is already set).
  167. *
  168. * @param string $key
  169. * @param mixed $regex
  170. *
  171. * @return $this
  172. */
  173. public function setRequirement($key, $regex)
  174. {
  175. $this->requirements[$key] = $regex;
  176. return $this;
  177. }
  178. /**
  179. * Sets an option that will be added to all embedded routes (unless that
  180. * option is already set).
  181. *
  182. * @param string $key
  183. * @param mixed $value
  184. *
  185. * @return $this
  186. */
  187. public function setOption($key, $value)
  188. {
  189. $this->options[$key] = $value;
  190. return $this;
  191. }
  192. /**
  193. * Sets the schemes on all embedded routes (unless already set).
  194. *
  195. * @param array|string $schemes
  196. *
  197. * @return $this
  198. */
  199. public function setSchemes($schemes)
  200. {
  201. $this->schemes = $schemes;
  202. return $this;
  203. }
  204. /**
  205. * Sets the methods on all embedded routes (unless already set).
  206. *
  207. * @param array|string $methods
  208. *
  209. * @return $this
  210. */
  211. public function setMethods($methods)
  212. {
  213. $this->methods = $methods;
  214. return $this;
  215. }
  216. /**
  217. * Adds a resource for this collection.
  218. *
  219. * @param ResourceInterface $resource
  220. *
  221. * @return $this
  222. */
  223. private function addResource(ResourceInterface $resource)
  224. {
  225. $this->resources[] = $resource;
  226. return $this;
  227. }
  228. /**
  229. * Creates the final RouteCollection and returns it.
  230. *
  231. * @return RouteCollection
  232. */
  233. public function build()
  234. {
  235. $routeCollection = new RouteCollection();
  236. foreach ($this->routes as $name => $route) {
  237. if ($route instanceof Route) {
  238. $route->setDefaults(array_merge($this->defaults, $route->getDefaults()));
  239. $route->setOptions(array_merge($this->options, $route->getOptions()));
  240. foreach ($this->requirements as $key => $val) {
  241. if (!$route->hasRequirement($key)) {
  242. $route->setRequirement($key, $val);
  243. }
  244. }
  245. if (null !== $this->prefix) {
  246. $route->setPath('/'.$this->prefix.$route->getPath());
  247. }
  248. if (!$route->getHost()) {
  249. $route->setHost($this->host);
  250. }
  251. if (!$route->getCondition()) {
  252. $route->setCondition($this->condition);
  253. }
  254. if (!$route->getSchemes()) {
  255. $route->setSchemes($this->schemes);
  256. }
  257. if (!$route->getMethods()) {
  258. $route->setMethods($this->methods);
  259. }
  260. // auto-generate the route name if it's been marked
  261. if ('_unnamed_route_' === substr($name, 0, 15)) {
  262. $name = $this->generateRouteName($route);
  263. }
  264. $routeCollection->add($name, $route);
  265. } else {
  266. /* @var self $route */
  267. $subCollection = $route->build();
  268. $subCollection->addPrefix($this->prefix);
  269. $routeCollection->addCollection($subCollection);
  270. }
  271. }
  272. foreach ($this->resources as $resource) {
  273. $routeCollection->addResource($resource);
  274. }
  275. return $routeCollection;
  276. }
  277. /**
  278. * Generates a route name based on details of this route.
  279. *
  280. * @return string
  281. */
  282. private function generateRouteName(Route $route)
  283. {
  284. $methods = implode('_', $route->getMethods()).'_';
  285. $routeName = $methods.$route->getPath();
  286. $routeName = str_replace(array('/', ':', '|', '-'), '_', $routeName);
  287. $routeName = preg_replace('/[^a-z0-9A-Z_.]+/', '', $routeName);
  288. // Collapse consecutive underscores down into a single underscore.
  289. $routeName = preg_replace('/_+/', '_', $routeName);
  290. return $routeName;
  291. }
  292. /**
  293. * Finds a loader able to load an imported resource and loads it.
  294. *
  295. * @param mixed $resource A resource
  296. * @param string|null $type The resource type or null if unknown
  297. *
  298. * @return RouteCollection[]
  299. *
  300. * @throws FileLoaderLoadException If no loader is found
  301. */
  302. private function load($resource, $type = null)
  303. {
  304. if (null === $this->loader) {
  305. throw new \BadMethodCallException('Cannot import other routing resources: you must pass a LoaderInterface when constructing RouteCollectionBuilder.');
  306. }
  307. if ($this->loader->supports($resource, $type)) {
  308. $collections = $this->loader->load($resource, $type);
  309. return is_array($collections) ? $collections : array($collections);
  310. }
  311. if (null === $resolver = $this->loader->getResolver()) {
  312. throw new FileLoaderLoadException($resource, null, null, null, $type);
  313. }
  314. if (false === $loader = $resolver->resolve($resource, $type)) {
  315. throw new FileLoaderLoadException($resource, null, null, null, $type);
  316. }
  317. $collections = $loader->load($resource, $type);
  318. return is_array($collections) ? $collections : array($collections);
  319. }
  320. }