"Fossies" - the Fresh Open Source Software Archive

Member "contao-4.4.48/core-bundle/src/Picker/AbstractPickerProvider.php" (2 Apr 2020, 3552 Bytes) of package /linux/www/contao-4.4.48.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 and the last Fossies "Diffs" side-by-side code changes report: 4.8.8_vs_4.9.0.

    1 <?php
    2 
    3 /*
    4  * This file is part of Contao.
    5  *
    6  * (c) Leo Feyer
    7  *
    8  * @license LGPL-3.0-or-later
    9  */
   10 
   11 namespace Contao\CoreBundle\Picker;
   12 
   13 use Contao\BackendUser;
   14 use Knp\Menu\FactoryInterface;
   15 use Symfony\Component\Routing\RouterInterface;
   16 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
   17 
   18 /**
   19  * Abstract class for picker providers.
   20  *
   21  * @author Andreas Schempp <https://github.com/aschempp>
   22  */
   23 abstract class AbstractPickerProvider implements PickerProviderInterface
   24 {
   25     /**
   26      * @var FactoryInterface
   27      */
   28     private $menuFactory;
   29 
   30     /**
   31      * @var RouterInterface
   32      */
   33     private $router;
   34 
   35     /**
   36      * @var TokenStorageInterface
   37      */
   38     private $tokenStorage;
   39 
   40     /**
   41      * Constructor.
   42      *
   43      * @param FactoryInterface $menuFactory
   44      * @param RouterInterface  $router
   45      */
   46     public function __construct(FactoryInterface $menuFactory, RouterInterface $router)
   47     {
   48         $this->menuFactory = $menuFactory;
   49         $this->router = $router;
   50     }
   51 
   52     /**
   53      * {@inheritdoc}
   54      */
   55     public function getUrl(PickerConfig $config)
   56     {
   57         return $this->generateUrl($config, false);
   58     }
   59 
   60     /**
   61      * {@inheritdoc}
   62      */
   63     public function createMenuItem(PickerConfig $config)
   64     {
   65         $name = $this->getName();
   66 
   67         return $this->menuFactory->createItem(
   68             $name,
   69             [
   70                 'label' => $GLOBALS['TL_LANG']['MSC'][$name] ?: $name,
   71                 'linkAttributes' => ['class' => $name],
   72                 'current' => $this->isCurrent($config),
   73                 'uri' => $this->generateUrl($config, true),
   74             ]
   75         );
   76     }
   77 
   78     /**
   79      * Sets the security token storage.
   80      *
   81      * @param TokenStorageInterface $tokenStorage
   82      */
   83     public function setTokenStorage(TokenStorageInterface $tokenStorage)
   84     {
   85         $this->tokenStorage = $tokenStorage;
   86     }
   87 
   88     /**
   89      * {@inheritdoc}
   90      */
   91     public function isCurrent(PickerConfig $config)
   92     {
   93         return $config->getCurrent() === $this->getName();
   94     }
   95 
   96     /**
   97      * Returns the back end user object.
   98      *
   99      * @throws \RuntimeException
  100      *
  101      * @return BackendUser
  102      */
  103     protected function getUser()
  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      * @param PickerConfig|null $config
  128      *
  129      * @return array
  130      */
  131     abstract protected function getRouteParameters(PickerConfig $config = null);
  132 
  133     /**
  134      * Generates the URL for the picker.
  135      *
  136      * @param PickerConfig $config
  137      * @param bool         $ignoreValue
  138      *
  139      * @return string
  140      */
  141     private function generateUrl(PickerConfig $config, $ignoreValue)
  142     {
  143         $params = array_merge(
  144             $this->getRouteParameters($ignoreValue ? null : $config),
  145             [
  146                 'popup' => '1',
  147                 'picker' => $config->cloneForCurrent($this->getName())->urlEncode(),
  148             ]
  149         );
  150 
  151         return $this->router->generate('contao_backend', $params);
  152     }
  153 }