GlobalVariables.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. protected $container;
  21. /**
  22. * @param ContainerInterface $container
  23. */
  24. public function __construct(ContainerInterface $container)
  25. {
  26. $this->container = $container;
  27. }
  28. /**
  29. * @return Pool
  30. */
  31. public function getAdminPool()
  32. {
  33. return $this->container->get('sonata.admin.pool');
  34. }
  35. /**
  36. * @param string $code
  37. * @param string $action
  38. * @param array $parameters
  39. * @param mixed $absolute
  40. *
  41. * @return string
  42. */
  43. public function url($code, $action, $parameters = array(), $absolute = false)
  44. {
  45. list($action, $code) = $this->getCodeAction($code, $action);
  46. return $this->getAdminPool()->getAdminByAdminCode($code)->generateUrl($action, $parameters, $absolute);
  47. }
  48. /**
  49. * @param string $code
  50. * @param string $action
  51. * @param mixed $object
  52. * @param array $parameters
  53. * @param mixed $absolute
  54. *
  55. * @return string
  56. */
  57. public function objectUrl($code, $action, $object, $parameters = array(), $absolute = false)
  58. {
  59. list($action, $code) = $this->getCodeAction($code, $action);
  60. return $this->getAdminPool()->getAdminByAdminCode($code)->generateObjectUrl($action, $object, $parameters, $absolute);
  61. }
  62. /**
  63. * @param $code
  64. * @param $action
  65. *
  66. * @return array
  67. */
  68. private function getCodeAction($code, $action)
  69. {
  70. if ($pipe = strpos('|', $code)) {
  71. // convert code=sonata.page.admin.page|sonata.page.admin.snapshot, action=list
  72. // to => sonata.page.admin.page|sonata.page.admin.snapshot.list
  73. $action = $code.'.'.$action;
  74. $code = substr($code, 0, $pipe);
  75. }
  76. return array($action, $code);
  77. }
  78. }