vendor/ezsystems/ezplatform-kernel/eZ/Bundle/EzPublishCoreBundle/DependencyInjection/Configuration/ChainConfigResolver.php line 12

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration;
  7. use eZ\Publish\Core\MVC\ConfigResolverInterface;
  8. use eZ\Publish\Core\MVC\Exception\ParameterNotFoundException;
  9. class ChainConfigResolver implements ConfigResolverInterface
  10. {
  11.     /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface[] */
  12.     protected $resolvers = [];
  13.     /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface[] */
  14.     protected $sortedResolvers;
  15.     /**
  16.      * Registers $mapper as a valid mapper to be used in the configuration mapping chain.
  17.      * When this mapper will be called in the chain depends on $priority. The highest $priority is, the earliest the router will be called.
  18.      *
  19.      * @param \eZ\Publish\Core\MVC\ConfigResolverInterface $resolver
  20.      * @param int $priority
  21.      */
  22.     public function addResolver(ConfigResolverInterface $resolver$priority 0)
  23.     {
  24.         $priority = (int)$priority;
  25.         if (!isset($this->resolvers[$priority])) {
  26.             $this->resolvers[$priority] = [];
  27.         }
  28.         $this->resolvers[$priority][] = $resolver;
  29.         $this->sortedResolvers = [];
  30.     }
  31.     /**
  32.      * @return \eZ\Publish\Core\MVC\ConfigResolverInterface[]
  33.      */
  34.     public function getAllResolvers()
  35.     {
  36.         if (empty($this->sortedResolvers)) {
  37.             $this->sortedResolvers $this->sortResolvers();
  38.         }
  39.         return $this->sortedResolvers;
  40.     }
  41.     /**
  42.      * Sort the registered mappers by priority.
  43.      * The highest priority number is the highest priority (reverse sorting).
  44.      *
  45.      * @return \eZ\Publish\Core\MVC\ConfigResolverInterface[]
  46.      */
  47.     protected function sortResolvers()
  48.     {
  49.         $sortedResolvers = [];
  50.         krsort($this->resolvers);
  51.         foreach ($this->resolvers as $resolvers) {
  52.             $sortedResolvers array_merge($sortedResolvers$resolvers);
  53.         }
  54.         return $sortedResolvers;
  55.     }
  56.     /**
  57.      * @return mixed
  58.      *
  59.      * @throws \eZ\Publish\Core\MVC\Exception\ParameterNotFoundException
  60.      */
  61.     public function getParameter(string $paramName, ?string $namespace null, ?string $scope null)
  62.     {
  63.         foreach ($this->getAllResolvers() as $resolver) {
  64.             try {
  65.                 return $resolver->getParameter($paramName$namespace$scope);
  66.             } catch (ParameterNotFoundException $e) {
  67.                 // Do nothing, just let the next resolver handle it
  68.             }
  69.         }
  70.         // Finally throw a ParameterNotFoundException since the chain resolver couldn't find any valid resolver for demanded parameter
  71.         throw new ParameterNotFoundException($paramName$namespace, [$scope]);
  72.     }
  73.     public function hasParameter(string $paramName, ?string $namespace null, ?string $scope null): bool
  74.     {
  75.         foreach ($this->getAllResolvers() as $resolver) {
  76.             $hasParameter $resolver->hasParameter($paramName$namespace$scope);
  77.             if ($hasParameter) {
  78.                 return true;
  79.             }
  80.         }
  81.         return false;
  82.     }
  83.     public function setDefaultNamespace(string $defaultNamespace): void
  84.     {
  85.         foreach ($this->getAllResolvers() as $resolver) {
  86.             $resolver->setDefaultNamespace($defaultNamespace);
  87.         }
  88.     }
  89.     /**
  90.      * Not supported.
  91.      *
  92.      * @throws \LogicException
  93.      */
  94.     public function getDefaultNamespace(): string
  95.     {
  96.         throw new \LogicException('getDefaultNamespace() is not supported by the ChainConfigResolver');
  97.     }
  98. }