DefaultRouteGenerator.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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. namespace Sonata\AdminBundle\Route;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Routing\RouterInterface;
  14. /**
  15. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  16. */
  17. class DefaultRouteGenerator implements RouteGeneratorInterface
  18. {
  19. /**
  20. * @var RouterInterface
  21. */
  22. private $router;
  23. /**
  24. * @var RoutesCache
  25. */
  26. private $cache;
  27. /**
  28. * @var array
  29. */
  30. private $caches = array();
  31. /**
  32. * @var string[]
  33. */
  34. private $loaded = array();
  35. /**
  36. * @param RouterInterface $router
  37. * @param RoutesCache $cache
  38. */
  39. public function __construct(RouterInterface $router, RoutesCache $cache)
  40. {
  41. $this->router = $router;
  42. $this->cache = $cache;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function generate($name, array $parameters = array(), $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
  48. {
  49. return $this->router->generate($name, $parameters, $absolute);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function generateUrl(AdminInterface $admin, $name, array $parameters = array(), $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
  55. {
  56. $arrayRoute = $this->generateMenuUrl($admin, $name, $parameters, $absolute);
  57. return $this->router->generate($arrayRoute['route'], $arrayRoute['routeParameters'], $arrayRoute['routeAbsolute']);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function generateMenuUrl(AdminInterface $admin, $name, array $parameters = array(), $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
  63. {
  64. // if the admin is a child we automatically append the parent's id
  65. if ($admin->isChild() && $admin->hasRequest() && $admin->getRequest()->attributes->has($admin->getParent()->getIdParameter())) {
  66. // twig template does not accept variable hash key ... so cannot use admin.idparameter ...
  67. // switch value
  68. if (isset($parameters['id'])) {
  69. $parameters[$admin->getIdParameter()] = $parameters['id'];
  70. unset($parameters['id']);
  71. }
  72. $parameters[$admin->getParent()->getIdParameter()] = $admin->getRequest()->attributes->get($admin->getParent()->getIdParameter());
  73. }
  74. // if the admin is linked to a parent FieldDescription (ie, embedded widget)
  75. if ($admin->hasParentFieldDescription()) {
  76. // merge link parameter if any provided by the parent field
  77. $parameters = array_merge($parameters, $admin->getParentFieldDescription()->getOption('link_parameters', array()));
  78. $parameters['uniqid'] = $admin->getUniqid();
  79. $parameters['code'] = $admin->getCode();
  80. $parameters['pcode'] = $admin->getParentFieldDescription()->getAdmin()->getCode();
  81. $parameters['puniqid'] = $admin->getParentFieldDescription()->getAdmin()->getUniqid();
  82. }
  83. if ($name == 'update' || substr($name, -7) == '|update') {
  84. $parameters['uniqid'] = $admin->getUniqid();
  85. $parameters['code'] = $admin->getCode();
  86. }
  87. // allows to define persistent parameters
  88. if ($admin->hasRequest()) {
  89. $parameters = array_merge($admin->getPersistentParameters(), $parameters);
  90. }
  91. $code = $this->getCode($admin, $name);
  92. if (!array_key_exists($code, $this->caches)) {
  93. throw new \RuntimeException(sprintf('unable to find the route `%s`', $code));
  94. }
  95. return array(
  96. 'route' => $this->caches[$code],
  97. 'routeParameters' => $parameters,
  98. 'routeAbsolute' => $absolute,
  99. );
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function hasAdminRoute(AdminInterface $admin, $name)
  105. {
  106. return array_key_exists($this->getCode($admin, $name), $this->caches);
  107. }
  108. /**
  109. * @param AdminInterface $admin
  110. * @param string $name
  111. *
  112. * @return string
  113. */
  114. private function getCode(AdminInterface $admin, $name)
  115. {
  116. $this->loadCache($admin);
  117. if ($admin->isChild()) {
  118. return $admin->getBaseCodeRoute().'.'.$name;
  119. }
  120. // someone provide the fullname
  121. if (array_key_exists($name, $this->caches)) {
  122. return $name;
  123. }
  124. // someone provide a code, so it is a child
  125. if (strpos($name, '.')) {
  126. return $admin->getCode().'|'.$name;
  127. }
  128. return $admin->getCode().'.'.$name;
  129. }
  130. /**
  131. * @param AdminInterface $admin
  132. */
  133. private function loadCache(AdminInterface $admin)
  134. {
  135. if ($admin->isChild()) {
  136. $this->loadCache($admin->getParent());
  137. } else {
  138. if (in_array($admin->getCode(), $this->loaded)) {
  139. return;
  140. }
  141. $this->caches = array_merge($this->cache->load($admin), $this->caches);
  142. $this->loaded[] = $admin->getCode();
  143. }
  144. }
  145. }