RouteCollection.php 6.0 KB

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