RouteCollection.php 6.0 KB

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