DefaultRouteGenerator.php 4.9 KB

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