RouteCollection.php 6.7 KB

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