RouteCollection.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. * @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 string[]|string $routeList
  158. *
  159. * @return RouteCollection
  160. */
  161. public function clearExcept($routeList)
  162. {
  163. if (!is_array($routeList)) {
  164. $routeList = array($routeList);
  165. }
  166. $routeCodeList = array();
  167. foreach ($routeList as $name) {
  168. $routeCodeList[] = $this->getCode($name);
  169. }
  170. $elements = $this->elements;
  171. foreach ($elements as $key => $element) {
  172. if (!in_array($key, $routeCodeList)) {
  173. unset($this->elements[$key]);
  174. }
  175. }
  176. return $this;
  177. }
  178. /**
  179. * Remove all routes.
  180. *
  181. * @return RouteCollection
  182. */
  183. public function clear()
  184. {
  185. $this->elements = array();
  186. return $this;
  187. }
  188. /**
  189. * Convert a word in to the format for a symfony action action_name => actionName.
  190. *
  191. * @param string $action Word to actionify
  192. *
  193. * @return string Actionified word
  194. */
  195. public function actionify($action)
  196. {
  197. if (($pos = strrpos($action, '.')) !== false) {
  198. $action = substr($action, $pos + 1);
  199. }
  200. // if this is a service rather than just a controller name, the suffix
  201. // Action is not automatically appended to the method name
  202. if (strpos($this->baseControllerName, ':') === false) {
  203. $action .= 'Action';
  204. }
  205. return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
  206. }
  207. /**
  208. * @return string
  209. */
  210. public function getBaseCodeRoute()
  211. {
  212. return $this->baseCodeRoute;
  213. }
  214. /**
  215. * @return string
  216. */
  217. public function getBaseControllerName()
  218. {
  219. return $this->baseControllerName;
  220. }
  221. /**
  222. * @return string
  223. */
  224. public function getBaseRouteName()
  225. {
  226. return $this->baseRouteName;
  227. }
  228. /**
  229. * @return string
  230. */
  231. public function getBaseRoutePattern()
  232. {
  233. return $this->baseRoutePattern;
  234. }
  235. /**
  236. * @param $element
  237. *
  238. * @return Route
  239. */
  240. private function resolve($element)
  241. {
  242. if (is_callable($element)) {
  243. return call_user_func($element);
  244. }
  245. return $element;
  246. }
  247. }