DefaultRouteGenerator.php 4.8 KB

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