DefaultRouteGenerator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 $name
  26. * @param array $parameters
  27. * @param bool $absolute
  28. * @return string
  29. */
  30. public function generate($name, array $parameters = array(), $absolute = false)
  31. {
  32. return $this->router->generate($name, $parameters, $absolute);
  33. }
  34. /**
  35. * @throws \RuntimeException
  36. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  37. * @param $name
  38. * @param array $parameter
  39. * @param bool $absolute
  40. * @return string
  41. */
  42. public function generateUrl(AdminInterface $admin, $name, array $parameters = array(), $absolute = false)
  43. {
  44. if (!$admin->isChild()) {
  45. if (strpos($name, '.')) {
  46. $name = $admin->getCode().'|'.$name;
  47. } else {
  48. $name = $admin->getCode().'.'.$name;
  49. }
  50. }
  51. // if the admin is a child we automatically append the parent's id
  52. else if ($admin->isChild()) {
  53. $name = $admin->getBaseCodeRoute().'.'.$name;
  54. // twig template does not accept variable hash key ... so cannot use admin.idparameter ...
  55. // switch value
  56. if (isset($parameters['id'])) {
  57. $parameters[$admin->getIdParameter()] = $parameters['id'];
  58. unset($parameters['id']);
  59. }
  60. $parameters[$admin->getParent()->getIdParameter()] = $admin->getRequest()->get($admin->getParent()->getIdParameter());
  61. }
  62. // if the admin is linked to a parent FieldDescription (ie, embedded widget)
  63. if ($admin->hasParentFieldDescription()) {
  64. // merge link parameter if any provided by the parent field
  65. $parameters = array_merge($parameters, $admin->getParentFieldDescription()->getOption('link_parameters', array()));
  66. $parameters['uniqid'] = $admin->getUniqid();
  67. $parameters['code'] = $admin->getCode();
  68. $parameters['pcode'] = $admin->getParentFieldDescription()->getAdmin()->getCode();
  69. $parameters['puniqid'] = $admin->getParentFieldDescription()->getAdmin()->getUniqid();
  70. }
  71. if ($name == 'update' || substr($name, -7) == '|update') {
  72. $parameters['uniqid'] = $admin->getUniqid();
  73. $parameters['code'] = $admin->getCode();
  74. }
  75. // allows to define persistent parameters
  76. if ($admin->hasRequest()) {
  77. $parameters = array_merge($admin->getPersistentParameters(), $parameters);
  78. }
  79. $route = $admin->getRoute($name);
  80. if (!$route) {
  81. throw new \RuntimeException(sprintf('unable to find the route `%s`', $name));
  82. }
  83. return $this->router->generate($route->getDefault('_sonata_name'), $parameters, $absolute);
  84. }
  85. }