RouteCollection.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Route;
  11. use Symfony\Component\Routing\Route;
  12. /**
  13. * Class RouteCollection.
  14. *
  15. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  16. */
  17. class RouteCollection
  18. {
  19. /**
  20. * @var Route[]
  21. */
  22. protected $elements = array();
  23. /**
  24. * @var string
  25. */
  26. protected $baseCodeRoute;
  27. /**
  28. * @var string
  29. */
  30. protected $baseRouteName;
  31. /**
  32. * @var string
  33. */
  34. protected $baseControllerName;
  35. /**
  36. * @var string
  37. */
  38. protected $baseRoutePattern;
  39. /**
  40. * @param string $baseCodeRoute
  41. * @param string $baseRouteName
  42. * @param string $baseRoutePattern
  43. * @param string $baseControllerName
  44. */
  45. public function __construct($baseCodeRoute, $baseRouteName, $baseRoutePattern, $baseControllerName)
  46. {
  47. $this->baseCodeRoute = $baseCodeRoute;
  48. $this->baseRouteName = $baseRouteName;
  49. $this->baseRoutePattern = $baseRoutePattern;
  50. $this->baseControllerName = $baseControllerName;
  51. }
  52. /**
  53. * Add route.
  54. *
  55. * @param string $name Name
  56. * @param string $pattern Pattern (will be automatically combined with @see $this->baseRoutePattern and $name
  57. * @param array $defaults Defaults
  58. * @param array $requirements Requirements
  59. * @param array $options Options
  60. * @param string $host Host
  61. * @param array $schemes Schemes
  62. * @param array $methods Methods
  63. * @param string $condition Condition
  64. *
  65. * @return RouteCollection
  66. */
  67. public function add($name, $pattern = null, array $defaults = array(), array $requirements = array(), array $options = array(), $host = '', array $schemes = array(), array $methods = array(), $condition = '')
  68. {
  69. $pattern = $this->baseRoutePattern.'/'.($pattern ?: $name);
  70. $code = $this->getCode($name);
  71. $routeName = $this->baseRouteName.'_'.$name;
  72. if (!isset($defaults['_controller'])) {
  73. $defaults['_controller'] = $this->baseControllerName.':'.$this->actionify($code);
  74. }
  75. if (!isset($defaults['_sonata_admin'])) {
  76. $defaults['_sonata_admin'] = $this->baseCodeRoute;
  77. }
  78. $defaults['_sonata_name'] = $routeName;
  79. $this->elements[$this->getCode($name)] = function () use (
  80. $pattern, $defaults, $requirements, $options, $host, $schemes, $methods, $condition) {
  81. return new Route($pattern, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
  82. };
  83. return $this;
  84. }
  85. /**
  86. * @param string $name
  87. *
  88. * @return string
  89. */
  90. public function getCode($name)
  91. {
  92. if (strrpos($name, '.') !== false) {
  93. return $name;
  94. }
  95. return $this->baseCodeRoute.'.'.$name;
  96. }
  97. /**
  98. * @param RouteCollection $collection
  99. *
  100. * @return RouteCollection
  101. */
  102. public function addCollection(RouteCollection $collection)
  103. {
  104. foreach ($collection->getElements() as $code => $route) {
  105. $this->elements[$code] = $route;
  106. }
  107. return $this;
  108. }
  109. /**
  110. * @param $element
  111. *
  112. * @return Route
  113. */
  114. private function resolve($element)
  115. {
  116. if (is_callable($element)) {
  117. return call_user_func($element);
  118. }
  119. return $element;
  120. }
  121. /**
  122. * @return Route[]
  123. */
  124. public function getElements()
  125. {
  126. foreach ($this->elements as $name => $element) {
  127. $this->elements[$name] = $this->resolve($element);
  128. }
  129. return $this->elements;
  130. }
  131. /**
  132. * @param string $name
  133. *
  134. * @return bool
  135. */
  136. public function has($name)
  137. {
  138. return array_key_exists($this->getCode($name), $this->elements);
  139. }
  140. /**
  141. * @param string $name
  142. *
  143. * @throws \InvalidArgumentException
  144. *
  145. * @return Route
  146. */
  147. public function get($name)
  148. {
  149. if ($this->has($name)) {
  150. $code = $this->getCode($name);
  151. $this->elements[$code] = $this->resolve($this->elements[$code]);
  152. return $this->elements[$code];
  153. }
  154. throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
  155. }
  156. /**
  157. * @param string $name
  158. *
  159. * @return RouteCollection
  160. */
  161. public function remove($name)
  162. {
  163. unset($this->elements[$this->getCode($name)]);
  164. return $this;
  165. }
  166. /**
  167. * Remove all routes except routes in $routeList.
  168. *
  169. * @param array $routeList
  170. *
  171. * @return RouteCollection
  172. */
  173. public function clearExcept(array $routeList)
  174. {
  175. $routeCodeList = array();
  176. foreach ($routeList as $name) {
  177. $routeCodeList[] = $this->getCode($name);
  178. }
  179. $elements = $this->elements;
  180. foreach ($elements as $key => $element) {
  181. if (!in_array($key, $routeCodeList)) {
  182. unset($this->elements[$key]);
  183. }
  184. }
  185. return $this;
  186. }
  187. /**
  188. * Remove all routes.
  189. *
  190. * @return RouteCollection
  191. */
  192. public function clear()
  193. {
  194. $this->elements = array();
  195. return $this;
  196. }
  197. /**
  198. * Convert a word in to the format for a symfony action action_name => actionName.
  199. *
  200. * @param string $action Word to actionify
  201. *
  202. * @return string Actionified word
  203. */
  204. public function actionify($action)
  205. {
  206. if (($pos = strrpos($action, '.')) !== false) {
  207. $action = substr($action, $pos + 1);
  208. }
  209. // if this is a service rather than just a controller name, the suffix
  210. // Action is not automatically appended to the method name
  211. if (strpos($this->baseControllerName, ':') === false) {
  212. $action .= 'Action';
  213. }
  214. return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
  215. }
  216. /**
  217. * @return string
  218. */
  219. public function getBaseCodeRoute()
  220. {
  221. return $this->baseCodeRoute;
  222. }
  223. /**
  224. * @return string
  225. */
  226. public function getBaseControllerName()
  227. {
  228. return $this->baseControllerName;
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getBaseRouteName()
  234. {
  235. return $this->baseRouteName;
  236. }
  237. /**
  238. * @return string
  239. */
  240. public function getBaseRoutePattern()
  241. {
  242. return $this->baseRoutePattern;
  243. }
  244. }