RouteCollection.php 5.5 KB

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