GlobalVariables.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Twig;
  11. use Sonata\AdminBundle\Admin\Pool;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. /**
  14. * Class GlobalVariables.
  15. *
  16. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  17. */
  18. class GlobalVariables
  19. {
  20. /**
  21. * @var ContainerInterface
  22. */
  23. protected $container;
  24. /**
  25. * @param ContainerInterface $container
  26. */
  27. public function __construct(ContainerInterface $container)
  28. {
  29. $this->container = $container;
  30. }
  31. /**
  32. * @return Pool
  33. */
  34. public function getAdminPool()
  35. {
  36. return $this->container->get('sonata.admin.pool');
  37. }
  38. /**
  39. * @param string $code
  40. * @param string $action
  41. * @param array $parameters
  42. * @param mixed $absolute
  43. *
  44. * @return string
  45. */
  46. public function url($code, $action, $parameters = array(), $absolute = false)
  47. {
  48. list($action, $code) = $this->getCodeAction($code, $action);
  49. return $this->getAdminPool()->getAdminByAdminCode($code)->generateUrl($action, $parameters, $absolute);
  50. }
  51. /**
  52. * @param string $code
  53. * @param string $action
  54. * @param mixed $object
  55. * @param array $parameters
  56. * @param mixed $absolute
  57. *
  58. * @return string
  59. */
  60. public function objectUrl($code, $action, $object, $parameters = array(), $absolute = false)
  61. {
  62. list($action, $code) = $this->getCodeAction($code, $action);
  63. return $this->getAdminPool()->getAdminByAdminCode($code)->generateObjectUrl($action, $object, $parameters, $absolute);
  64. }
  65. /**
  66. * @param $code
  67. * @param $action
  68. *
  69. * @return array
  70. */
  71. private function getCodeAction($code, $action)
  72. {
  73. if ($pipe = strpos('|', $code)) {
  74. // convert code=sonata.page.admin.page|sonata.page.admin.snapshot, action=list
  75. // to => sonata.page.admin.page|sonata.page.admin.snapshot.list
  76. $action = $code.'.'.$action;
  77. $code = substr($code, 0, $pipe);
  78. }
  79. return array($action, $code);
  80. }
  81. }