GlobalVariables.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  15. */
  16. class GlobalVariables
  17. {
  18. /**
  19. * @var ContainerInterface
  20. *
  21. * @deprecated Since version 3.5, will be removed in 4.0.
  22. * NEXT_MAJOR : remove this property
  23. */
  24. protected $container;
  25. /**
  26. * @var Pool
  27. */
  28. protected $adminPool;
  29. /**
  30. * @param ContainerInterface|Pool $adminPool
  31. */
  32. public function __construct($adminPool)
  33. {
  34. // NEXT_MAJOR : remove this block and set adminPool from parameter.
  35. if ($adminPool instanceof ContainerInterface) {
  36. @trigger_error(
  37. 'Using an instance of Symfony\Component\DependencyInjection\ContainerInterface is deprecated since
  38. version 3.5 and will be removed in 4.0. Use Sonata\AdminBundle\Admin\Pool instead.',
  39. E_USER_DEPRECATED
  40. );
  41. $this->adminPool = $adminPool->get('sonata.admin.pool');
  42. } elseif ($adminPool instanceof Pool) {
  43. $this->adminPool = $adminPool;
  44. } else {
  45. throw new \InvalidArgumentException(
  46. '$adminPool should be an instance of Sonata\AdminBundle\Admin\Pool'
  47. );
  48. }
  49. }
  50. /**
  51. * @return Pool
  52. */
  53. public function getAdminPool()
  54. {
  55. return $this->adminPool;
  56. }
  57. /**
  58. * @param string $code
  59. * @param string $action
  60. * @param array $parameters
  61. * @param mixed $absolute
  62. *
  63. * @return string
  64. */
  65. public function url($code, $action, $parameters = array(), $absolute = false)
  66. {
  67. list($action, $code) = $this->getCodeAction($code, $action);
  68. return $this->getAdminPool()->getAdminByAdminCode($code)->generateUrl($action, $parameters, $absolute);
  69. }
  70. /**
  71. * @param string $code
  72. * @param string $action
  73. * @param mixed $object
  74. * @param array $parameters
  75. * @param mixed $absolute
  76. *
  77. * @return string
  78. */
  79. public function objectUrl($code, $action, $object, $parameters = array(), $absolute = false)
  80. {
  81. list($action, $code) = $this->getCodeAction($code, $action);
  82. return $this->getAdminPool()->getAdminByAdminCode($code)->generateObjectUrl($action, $object, $parameters, $absolute);
  83. }
  84. /**
  85. * @param $code
  86. * @param $action
  87. *
  88. * @return array
  89. */
  90. private function getCodeAction($code, $action)
  91. {
  92. if ($pipe = strpos($code, '|')) {
  93. // convert code=sonata.page.admin.page|sonata.page.admin.snapshot, action=list
  94. // to => sonata.page.admin.page|sonata.page.admin.snapshot.list
  95. $action = $code.'.'.$action;
  96. $code = substr($code, 0, $pipe);
  97. }
  98. return array($action, $code);
  99. }
  100. }