RouteCollection.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. * @return Route
  97. */
  98. public function get($name)
  99. {
  100. if ($this->has($name)) {
  101. return $this->elements[$this->getCode($name)];
  102. }
  103. throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
  104. }
  105. /**
  106. * @param string $name
  107. *
  108. * @return \Sonata\AdminBundle\Route\RouteCollection
  109. */
  110. public function remove($name)
  111. {
  112. unset($this->elements[$this->getCode($name)]);
  113. return $this;
  114. }
  115. /**
  116. * Convert a word in to the format for a symfony action action_name => actionName
  117. *
  118. * @param string $action Word to actionify
  119. *
  120. * @return string Actionified word
  121. */
  122. public function actionify($action)
  123. {
  124. if (($pos = strrpos($action, '.')) !== false) {
  125. $action = substr($action, $pos + 1);
  126. }
  127. // if this is a service rather than just a controller name, the suffix
  128. // Action is not automatically appended to the method name
  129. if (strpos($this->baseControllerName, ':') === false) {
  130. $action .= 'Action';
  131. }
  132. return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
  133. }
  134. /**
  135. * @return string
  136. */
  137. public function getBaseCodeRoute()
  138. {
  139. return $this->baseCodeRoute;
  140. }
  141. /**
  142. * @return string
  143. */
  144. public function getBaseControllerName()
  145. {
  146. return $this->baseControllerName;
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function getBaseRouteName()
  152. {
  153. return $this->baseRouteName;
  154. }
  155. /**
  156. * @return string
  157. */
  158. public function getBaseRoutePattern()
  159. {
  160. return $this->baseRoutePattern;
  161. }
  162. }