DefaultRouteGenerator.php 5.2 KB

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