RouteCollection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * @return void
  40. */
  41. public function add($name, $pattern = null, array $defaults = array(), array $requirements = array(), array $options = array())
  42. {
  43. $pattern = sprintf('%s/%s', $this->baseRoutePattern, $pattern ?: $name);
  44. $code = $this->getCode($name);
  45. $routeName = sprintf('%s_%s', $this->baseRouteName, $name);
  46. if (!isset($defaults['_controller'])) {
  47. $defaults['_controller'] = sprintf('%s:%s', $this->baseControllerName, $this->actionify($code));
  48. }
  49. if (!isset($defaults['_sonata_admin'])) {
  50. $defaults['_sonata_admin'] = $this->baseCodeRoute;
  51. }
  52. $defaults['_sonata_name'] = $routeName;
  53. $this->elements[$this->getCode($name)] = new Route($pattern, $defaults, $requirements, $options);
  54. }
  55. public function getCode($name)
  56. {
  57. if (strrpos($name, '.') !== false) {
  58. return $name;
  59. }
  60. return sprintf('%s.%s', $this->baseCodeRoute, $name);
  61. }
  62. /**
  63. * @param RouteCollection $collection
  64. */
  65. public function addCollection(RouteCollection $collection)
  66. {
  67. foreach ($collection->getElements() as $code => $route) {
  68. $this->elements[$code] = $route;
  69. }
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function getElements()
  75. {
  76. return $this->elements;
  77. }
  78. /**
  79. * @param string $name
  80. * @return bool
  81. */
  82. public function has($name)
  83. {
  84. return array_key_exists($this->getCode($name), $this->elements);
  85. }
  86. /**
  87. * @param string $name
  88. * @return Route
  89. */
  90. public function get($name)
  91. {
  92. if ($this->has($name)) {
  93. return $this->elements[$this->getCode($name)];
  94. }
  95. throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
  96. }
  97. /**
  98. * @param $name
  99. * @return \Sonata\AdminBundle\Route\RouteCollection
  100. */
  101. public function remove($name)
  102. {
  103. unset($this->elements[$this->getCode($name)]);
  104. return $this;
  105. }
  106. /**
  107. * Convert a word in to the format for a symfony action action_name => actionName
  108. *
  109. * @param string $action Word to actionify
  110. * @return string Actionified word
  111. */
  112. public function actionify($action)
  113. {
  114. if (($pos = strrpos($action, '.')) !== false) {
  115. $action = substr($action, $pos + 1);
  116. }
  117. return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
  118. }
  119. /**
  120. * @return string
  121. */
  122. public function getBaseCodeRoute()
  123. {
  124. return $this->baseCodeRoute;
  125. }
  126. /**
  127. * @return string
  128. */
  129. public function getBaseControllerName()
  130. {
  131. return $this->baseControllerName;
  132. }
  133. /**
  134. * @return string
  135. */
  136. public function getBaseRouteName()
  137. {
  138. return $this->baseRouteName;
  139. }
  140. /**
  141. * @return string
  142. */
  143. public function getBaseRoutePattern()
  144. {
  145. return $this->baseRoutePattern;
  146. }
  147. }