GlobalVariables.php 2.3 KB

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