RouteCollectionBuilder.php 9.8 KB

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