RouteCollection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 $baseControllerName
  24. */
  25. public function __construct($baseCodeRoute, $baseRouteName, $baseRoutePattern, $baseControllerName)
  26. {
  27. $this->baseCodeRoute = $baseCodeRoute;
  28. $this->baseRouteName = $baseRouteName;
  29. $this->baseRoutePattern = $baseRoutePattern;
  30. $this->baseControllerName = $baseControllerName;
  31. }
  32. /**
  33. * @param string $name
  34. * @param string $pattern
  35. * @param array $defaults
  36. * @param array $requirements
  37. * @param array $options
  38. * @return void
  39. */
  40. public function add($name, $pattern = null, array $defaults = array(), array $requirements = array(), array $options = array())
  41. {
  42. $pattern = sprintf('%s/%s', $this->baseRoutePattern, $pattern ?: $name);
  43. $code = sprintf('%s.%s', $this->baseCodeRoute, $name);
  44. $name = sprintf('%s_%s', $this->baseRouteName, $name);
  45. if (!isset($defaults['_controller'])) {
  46. $defaults['_controller'] = sprintf('%s:%s', $this->baseControllerName, $this->actionify($code));
  47. }
  48. if (!isset($defaults['_sonata_admin'])) {
  49. $defaults['_sonata_admin'] = $this->baseCodeRoute;
  50. }
  51. $defaults['_sonata_name'] = $name;
  52. $this->elements[$code] = new Route($pattern, $defaults, $requirements, $options);;
  53. }
  54. /**
  55. * @param RouteCollection
  56. */
  57. public function addCollection(RouteCollection $collection)
  58. {
  59. foreach ($collection->getElements() as $name => $route) {
  60. $this->elements[$name] = $route;
  61. }
  62. }
  63. /**
  64. * @return array
  65. */
  66. public function getElements()
  67. {
  68. return $this->elements;
  69. }
  70. /**
  71. * @param string $name
  72. * @return bool
  73. */
  74. public function has($name)
  75. {
  76. return array_key_exists($name, $this->elements);
  77. }
  78. /**
  79. * @param string $name
  80. * @return Route
  81. */
  82. public function get($name)
  83. {
  84. if ($this->has($name)) {
  85. return $this->elements[$name];
  86. }
  87. throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
  88. }
  89. /**
  90. * Convert a word in to the format for a symfony action action_name => actionName
  91. *
  92. * @param string $word Word to actionify
  93. * @return string $word Actionified word
  94. */
  95. public function actionify($action)
  96. {
  97. if (($pos = strrpos($action, '.')) !== false) {
  98. $action = substr($action, $pos + 1);
  99. }
  100. return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
  101. }
  102. }