vendor/sonata-project/admin-bundle/src/Twig/GlobalVariables.php line 80

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\AdminBundle\Twig;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. /**
  16.  * @final since sonata-project/admin-bundle 3.52
  17.  *
  18.  * NEXT_MAJOR: Remove this class.
  19.  *
  20.  * @deprecated since sonata-project/admin-bundle 3.83, will be removed in 4.0.
  21.  *
  22.  * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  23.  */
  24. class GlobalVariables
  25. {
  26.     /**
  27.      * @var ContainerInterface
  28.      *
  29.      * @deprecated since sonata-project/admin-bundle 3.5, will be removed in 4.0.
  30.      * NEXT_MAJOR : remove this property
  31.      */
  32.     protected $container;
  33.     /**
  34.      * @var Pool
  35.      */
  36.     protected $adminPool;
  37.     /**
  38.      * @var string|null
  39.      */
  40.     private $mosaicBackground;
  41.     /**
  42.      * @param ContainerInterface|Pool $adminPool
  43.      */
  44.     public function __construct($adminPool, ?string $mosaicBackground null)
  45.     {
  46.         $this->mosaicBackground $mosaicBackground;
  47.         // NEXT_MAJOR : remove this block and set adminPool from parameter.
  48.         if ($adminPool instanceof ContainerInterface) {
  49.             @trigger_error(sprintf(
  50.                 'Using an instance of %s is deprecated since version 3.5 and will be removed in 4.0. Use %s instead.',
  51.                 ContainerInterface::class,
  52.                 Pool::class
  53.             ), \E_USER_DEPRECATED);
  54.             /** @psalm-suppress UndefinedMethod */
  55.             $adminPool $adminPool->get('sonata.admin.pool');
  56.         }
  57.         if ($adminPool instanceof Pool) {
  58.             $this->adminPool $adminPool;
  59.             return;
  60.         }
  61.         throw new \InvalidArgumentException(sprintf('$adminPool should be an instance of %s'Pool::class));
  62.     }
  63.     /**
  64.      * @return Pool
  65.      */
  66.     public function getAdminPool()
  67.     {
  68.         if ('sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) {
  69.             @trigger_error(sprintf(
  70.                 'Method "%s()" is deprecated since sonata-project/admin-bundle 3.83 and will be removed in version 4.0.',
  71.                 __METHOD__
  72.             ), \E_USER_DEPRECATED);
  73.         }
  74.         return $this->adminPool;
  75.     }
  76.     /**
  77.      * @param string $code
  78.      * @param string $action
  79.      * @param array  $parameters
  80.      * @param int    $referenceType
  81.      *
  82.      * @return string
  83.      */
  84.     public function url($code$action$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_PATH)
  85.     {
  86.         @trigger_error(sprintf(
  87.             'Method "%s()" is deprecated since sonata-project/admin-bundle 3.83 and will be removed in version 4.0.',
  88.             __METHOD__
  89.         ), \E_USER_DEPRECATED);
  90.         [$action$code] = $this->getCodeAction($code$action);
  91.         return $this->getAdminPool()->getAdminByAdminCode($code)->generateUrl($action$parameters$referenceType);
  92.     }
  93.     /**
  94.      * @param string $code
  95.      * @param string $action
  96.      * @param object $object
  97.      * @param array  $parameters
  98.      * @param int    $referenceType
  99.      *
  100.      * @return string
  101.      */
  102.     public function objectUrl($code$action$object$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_PATH)
  103.     {
  104.         @trigger_error(sprintf(
  105.             'Method "%s()" is deprecated since sonata-project/admin-bundle 3.83 and will be removed in version 4.0.',
  106.             __METHOD__
  107.         ), \E_USER_DEPRECATED);
  108.         [$action$code] = $this->getCodeAction($code$action);
  109.         return $this->getAdminPool()->getAdminByAdminCode($code)->generateObjectUrl($action$object$parameters$referenceType);
  110.     }
  111.     public function getMosaicBackground(): ?string
  112.     {
  113.         @trigger_error(sprintf(
  114.             'Method "%s()" is deprecated since sonata-project/admin-bundle 3.83 and will be removed in version 4.0.'
  115.             .' Use "sonata_config.getOption(\'mosaic_background\')" instead.',
  116.             __METHOD__
  117.         ), \E_USER_DEPRECATED);
  118.         return $this->mosaicBackground;
  119.     }
  120.     /**
  121.      * @return string[]
  122.      */
  123.     private function getCodeAction(string $codestring $action): array
  124.     {
  125.         if ($pipe strpos($code'|')) {
  126.             // convert code=sonata.page.admin.page|sonata.page.admin.snapshot, action=list
  127.             // to => sonata.page.admin.page|sonata.page.admin.snapshot.list
  128.             $action sprintf('%s.%s'$code$action);
  129.             $code substr($code0$pipe);
  130.         }
  131.         return [$action$code];
  132.     }
  133. }