DefaultRouteGenerator.php 4.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\AdminInterface;
  13. use Symfony\Component\Routing\RouterInterface;
  14. class DefaultRouteGenerator implements RouteGeneratorInterface
  15. {
  16. private $router;
  17. /**
  18. * @param \Symfony\Component\Routing\RouterInterface $router
  19. */
  20. public function __construct(RouterInterface $router)
  21. {
  22. $this->router = $router;
  23. }
  24. /**
  25. * @param string $name
  26. * @param array $parameters
  27. * @param bool $absolute
  28. *
  29. * @return string
  30. */
  31. public function generate($name, array $parameters = array(), $absolute = false)
  32. {
  33. return $this->router->generate($name, $parameters, $absolute);
  34. }
  35. /**
  36. *
  37. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  38. * @param string $name
  39. * @param array $parameters
  40. * @param bool $absolute
  41. * @throws \RuntimeException
  42. * @return string
  43. */
  44. public function generateUrl(AdminInterface $admin, $name, array $parameters = array(), $absolute = false)
  45. {
  46. $arrayRoute = $this->generateMenuUrl($admin, $name, $parameters, $absolute);
  47. return $this->router->generate($arrayRoute['route'], $arrayRoute['routeParameters'], $arrayRoute['routeAbsolute']);
  48. }
  49. /**
  50. * Generates KNPMenu array parameters for menu route
  51. *
  52. * @param AdminInterface $admin
  53. * @param string $name
  54. * @param array $parameters
  55. * @param bool $absolute
  56. *
  57. * @return array
  58. * @throws \RuntimeException
  59. */
  60. public function generateMenuUrl(AdminInterface $admin, $name, array $parameters = array(), $absolute = false)
  61. {
  62. if (!$admin->isChild()) {
  63. if (strpos($name, '.')) {
  64. $name = $admin->getCode().'|'.$name;
  65. } else {
  66. $name = $admin->getCode().'.'.$name;
  67. }
  68. }
  69. // if the admin is a child we automatically append the parent's id
  70. else {
  71. $name = $admin->getBaseCodeRoute().'.'.$name;
  72. // twig template does not accept variable hash key ... so cannot use admin.idparameter ...
  73. // switch value
  74. if (isset($parameters['id'])) {
  75. $parameters[$admin->getIdParameter()] = $parameters['id'];
  76. unset($parameters['id']);
  77. }
  78. $parameters[$admin->getParent()->getIdParameter()] = $admin->getRequest()->get($admin->getParent()->getIdParameter());
  79. }
  80. // if the admin is linked to a parent FieldDescription (ie, embedded widget)
  81. if ($admin->hasParentFieldDescription()) {
  82. // merge link parameter if any provided by the parent field
  83. $parameters = array_merge($parameters, $admin->getParentFieldDescription()->getOption('link_parameters', array()));
  84. $parameters['uniqid'] = $admin->getUniqid();
  85. $parameters['code'] = $admin->getCode();
  86. $parameters['pcode'] = $admin->getParentFieldDescription()->getAdmin()->getCode();
  87. $parameters['puniqid'] = $admin->getParentFieldDescription()->getAdmin()->getUniqid();
  88. }
  89. if ($name == 'update' || substr($name, -7) == '|update') {
  90. $parameters['uniqid'] = $admin->getUniqid();
  91. $parameters['code'] = $admin->getCode();
  92. }
  93. // allows to define persistent parameters
  94. if ($admin->hasRequest()) {
  95. $parameters = array_merge($admin->getPersistentParameters(), $parameters);
  96. }
  97. $route = $admin->getRoute($name);
  98. if (!$route) {
  99. throw new \RuntimeException(sprintf('unable to find the route `%s`', $name));
  100. }
  101. return array(
  102. 'route' => $route->getDefault('_sonata_name'),
  103. 'routeParameters' => $parameters,
  104. 'routeAbsolute' => $absolute
  105. );
  106. }
  107. }