RouteCollection.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 void
  41. */
  42. public function add($name, $pattern = null, array $defaults = array(), array $requirements = array(), array $options = array())
  43. {
  44. $pattern = sprintf('%s/%s', $this->baseRoutePattern, $pattern ?: $name);
  45. $code = $this->getCode($name);
  46. $routeName = sprintf('%s_%s', $this->baseRouteName, $name);
  47. if (!isset($defaults['_controller'])) {
  48. $defaults['_controller'] = sprintf('%s:%s', $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)] = new Route($pattern, $defaults, $requirements, $options);
  55. }
  56. /**
  57. * @param string $name
  58. *
  59. * @return string
  60. */
  61. public function getCode($name)
  62. {
  63. if (strrpos($name, '.') !== false) {
  64. return $name;
  65. }
  66. return sprintf('%s.%s', $this->baseCodeRoute, $name);
  67. }
  68. /**
  69. * @param RouteCollection $collection
  70. */
  71. public function addCollection(RouteCollection $collection)
  72. {
  73. foreach ($collection->getElements() as $code => $route) {
  74. $this->elements[$code] = $route;
  75. }
  76. }
  77. /**
  78. * @return array
  79. */
  80. public function getElements()
  81. {
  82. return $this->elements;
  83. }
  84. /**
  85. * @param string $name
  86. *
  87. * @return bool
  88. */
  89. public function has($name)
  90. {
  91. return array_key_exists($this->getCode($name), $this->elements);
  92. }
  93. /**
  94. * @param string $name
  95. *
  96. * @throws \InvalidArgumentException
  97. *
  98. * @return Route
  99. */
  100. public function get($name)
  101. {
  102. if ($this->has($name)) {
  103. return $this->elements[$this->getCode($name)];
  104. }
  105. throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
  106. }
  107. /**
  108. * @param string $name
  109. *
  110. * @return \Sonata\AdminBundle\Route\RouteCollection
  111. */
  112. public function remove($name)
  113. {
  114. unset($this->elements[$this->getCode($name)]);
  115. return $this;
  116. }
  117. /**
  118. * Remove all routes except routes in $routeList
  119. *
  120. * @param array $routeList
  121. *
  122. * @return \Sonata\AdminBundle\Route\RouteCollection
  123. */
  124. public function clearExcept(array $routeList)
  125. {
  126. $routeCodeList = array();
  127. foreach ($routeList as $name) {
  128. $routeCodeList[] = $this->getCode($name);
  129. }
  130. $elements = $this->elements;
  131. foreach ($elements as $key => $element) {
  132. if (!in_array($key, $routeCodeList)) {
  133. unset($this->elements[$key]);
  134. }
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Remove all routes
  140. *
  141. * @return \Sonata\AdminBundle\Route\RouteCollection
  142. */
  143. public function clear()
  144. {
  145. $this->elements = array();
  146. return $this;
  147. }
  148. /**
  149. * Convert a word in to the format for a symfony action action_name => actionName
  150. *
  151. * @param string $action Word to actionify
  152. *
  153. * @return string Actionified word
  154. */
  155. public function actionify($action)
  156. {
  157. if (($pos = strrpos($action, '.')) !== false) {
  158. $action = substr($action, $pos + 1);
  159. }
  160. // if this is a service rather than just a controller name, the suffix
  161. // Action is not automatically appended to the method name
  162. if (strpos($this->baseControllerName, ':') === false) {
  163. $action .= 'Action';
  164. }
  165. return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
  166. }
  167. /**
  168. * @return string
  169. */
  170. public function getBaseCodeRoute()
  171. {
  172. return $this->baseCodeRoute;
  173. }
  174. /**
  175. * @return string
  176. */
  177. public function getBaseControllerName()
  178. {
  179. return $this->baseControllerName;
  180. }
  181. /**
  182. * @return string
  183. */
  184. public function getBaseRouteName()
  185. {
  186. return $this->baseRouteName;
  187. }
  188. /**
  189. * @return string
  190. */
  191. public function getBaseRoutePattern()
  192. {
  193. return $this->baseRoutePattern;
  194. }
  195. }