DefaultRouteGenerator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * @throws \RuntimeException
  37. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  38. * @param $name
  39. * @param array $parameter
  40. * @param bool $absolute
  41. * @return string
  42. */
  43. public function generateUrl(AdminInterface $admin, $name, array $parameters = array(), $absolute = false)
  44. {
  45. if (!$admin->isChild()) {
  46. if (strpos($name, '.')) {
  47. $name = $admin->getCode().'|'.$name;
  48. } else {
  49. $name = $admin->getCode().'.'.$name;
  50. }
  51. }
  52. // if the admin is a child we automatically append the parent's id
  53. else if ($admin->isChild()) {
  54. $name = $admin->getBaseCodeRoute().'.'.$name;
  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()->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. $route = $admin->getRoute($name);
  81. if (!$route) {
  82. throw new \RuntimeException(sprintf('unable to find the route `%s`', $name));
  83. }
  84. return $this->router->generate($route->getDefault('_sonata_name'), $parameters, $absolute);
  85. }
  86. }