RouteCollection.php 6.6 KB

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