"Fossies" - the Fresh Open Source Software Archive

Member "contao-4.11.5/core-bundle/src/Picker/AbstractPickerProvider.php" (23 Jun 2021, 4283 Bytes) of package /linux/www/contao-4.11.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractPickerProvider.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 /*
    6  * This file is part of Contao.
    7  *
    8  * (c) Leo Feyer
    9  *
   10  * @license LGPL-3.0-or-later
   11  */
   12 
   13 namespace Contao\CoreBundle\Picker;
   14 
   15 use Contao\BackendUser;
   16 use Knp\Menu\FactoryInterface;
   17 use Symfony\Component\Routing\RouterInterface;
   18 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
   19 use Symfony\Contracts\Translation\TranslatorInterface;
   20 
   21 abstract class AbstractPickerProvider implements PickerProviderInterface
   22 {
   23     /**
   24      * @var FactoryInterface
   25      */
   26     private $menuFactory;
   27 
   28     /**
   29      * @var RouterInterface
   30      */
   31     private $router;
   32 
   33     /**
   34      * @var TranslatorInterface|null
   35      */
   36     private $translator;
   37 
   38     /**
   39      * @var TokenStorageInterface
   40      */
   41     private $tokenStorage;
   42 
   43     public function __construct(FactoryInterface $menuFactory, RouterInterface $router, TranslatorInterface $translator = null)
   44     {
   45         $this->menuFactory = $menuFactory;
   46         $this->router = $router;
   47         $this->translator = $translator;
   48     }
   49 
   50     public function getUrl(PickerConfig $config)
   51     {
   52         return $this->generateUrl($config, false);
   53     }
   54 
   55     public function createMenuItem(PickerConfig $config)
   56     {
   57         $name = $this->getName();
   58 
   59         if (null === $this->translator) {
   60             trigger_deprecation('contao/core-bundle', '4.4', 'Using a picker provider without injecting the translator service has been deprecated and will no longer work in Contao 5.0.');
   61             $label = $GLOBALS['TL_LANG']['MSC'][$name];
   62         } else {
   63             $label = $this->translator->trans('MSC.'.$name, [], 'contao_default');
   64         }
   65 
   66         return $this->menuFactory->createItem(
   67             $name,
   68             [
   69                 'label' => $label ?: $name,
   70                 'linkAttributes' => ['class' => $name],
   71                 'current' => $this->isCurrent($config),
   72                 'uri' => $this->generateUrl($config, true),
   73             ]
   74         );
   75     }
   76 
   77     /**
   78      * @deprecated Deprecated since Contao 4.8, to be removed in Contao 5.0;
   79      *             use Symfony security instead
   80      */
   81     public function setTokenStorage(TokenStorageInterface $tokenStorage): void
   82     {
   83         trigger_deprecation('contao/core-bundle', '4.8', 'Using "Contao\CoreBundle\Picker\AbstractPickerProvider::setTokenStorage()" has been deprecated and will no longer work in Contao 5.0. Use Symfony security instead.');
   84 
   85         $this->tokenStorage = $tokenStorage;
   86     }
   87 
   88     public function isCurrent(PickerConfig $config)
   89     {
   90         return $config->getCurrent() === $this->getName();
   91     }
   92 
   93     /**
   94      * Returns the back end user object.
   95      *
   96      * @throws \RuntimeException
   97      *
   98      * @deprecated Deprecated since Contao 4.8, to be removed in Contao 5.0;
   99      *             use Symfony security instead
  100      */
  101     protected function getUser(): BackendUser
  102     {
  103         trigger_deprecation('contao/core-bundle', '4.8', 'Using "Contao\CoreBundle\Picker\AbstractPickerProvider::getUser()" has been deprecated and will no longer work in Contao 5.0. Use Symfony security instead.');
  104 
  105         if (null === $this->tokenStorage) {
  106             throw new \RuntimeException('No token storage provided');
  107         }
  108 
  109         $token = $this->tokenStorage->getToken();
  110 
  111         if (null === $token) {
  112             throw new \RuntimeException('No token provided');
  113         }
  114 
  115         $user = $token->getUser();
  116 
  117         if (!$user instanceof BackendUser) {
  118             throw new \RuntimeException('The token does not contain a back end user object');
  119         }
  120 
  121         return $user;
  122     }
  123 
  124     /**
  125      * Returns the routing parameters for the back end picker.
  126      *
  127      * @return array<string,string|int>
  128      */
  129     abstract protected function getRouteParameters(PickerConfig $config = null);
  130 
  131     /**
  132      * Generates the URL for the picker.
  133      */
  134     private function generateUrl(PickerConfig $config, bool $ignoreValue): ?string
  135     {
  136         $params = array_merge(
  137             $this->getRouteParameters($ignoreValue ? null : $config),
  138             [
  139                 'popup' => '1',
  140                 'picker' => $config->cloneForCurrent($this->getName())->urlEncode(),
  141             ]
  142         );
  143 
  144         return $this->router->generate('contao_backend', $params);
  145     }
  146 }