RouteCollection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Sonata\AdminBundle\Admin\FieldDescription;
  13. use Symfony\Component\Routing\Route;
  14. class RouteCollection
  15. {
  16. protected $elements = array();
  17. protected $baseCodeRoute;
  18. protected $baseRouteName;
  19. protected $baseControllerName;
  20. protected $baseRoutePattern;
  21. /**
  22. * @param string $baseCodeRoute
  23. * @param string $baseRouteName
  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 = sprintf('%s.%s', $this->baseCodeRoute, $name);
  45. $name = 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'] = $name;
  53. $this->elements[$code] = new Route($pattern, $defaults, $requirements, $options);;
  54. }
  55. /**
  56. * @param RouteCollection
  57. */
  58. public function addCollection(RouteCollection $collection)
  59. {
  60. foreach($collection->getElements() as $name => $route) {
  61. $this->elements[$name] = $route;
  62. }
  63. }
  64. /**
  65. * @return array
  66. */
  67. public function getElements()
  68. {
  69. return $this->elements;
  70. }
  71. /**
  72. * @param string $name
  73. * @return bool
  74. */
  75. public function has($name)
  76. {
  77. return array_key_exists($name, $this->elements);
  78. }
  79. /**
  80. * @param string $name
  81. * @return Route
  82. */
  83. public function get($name)
  84. {
  85. if ($this->has($name)) {
  86. return $this->elements[$name];
  87. }
  88. throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
  89. }
  90. /**
  91. * Convert a word in to the format for a symfony action action_name => actionName
  92. *
  93. * @param string $word Word to actionify
  94. * @return string $word Actionified word
  95. */
  96. public function actionify($action)
  97. {
  98. if(($pos = strrpos($action, '.')) !== false) {
  99. $action = substr($action, $pos + 1);
  100. }
  101. return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
  102. }
  103. }