vendor/sensio/framework-extra-bundle/src/Request/ParamConverter/ParamConverterManager.php line 84

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  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 Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  12. use Symfony\Component\HttpFoundation\Request;
  13. /**
  14.  * Managers converters.
  15.  *
  16.  * @author Fabien Potencier <fabien@symfony.com>
  17.  * @author Henrik Bjornskov <henrik@bjrnskov.dk>
  18.  */
  19. class ParamConverterManager
  20. {
  21.     /**
  22.      * @var array
  23.      */
  24.     private $converters = [];
  25.     /**
  26.      * @var array
  27.      */
  28.     private $namedConverters = [];
  29.     /**
  30.      * Applies all converters to the passed configurations and stops when a
  31.      * converter is applied it will move on to the next configuration and so on.
  32.      *
  33.      * @param array|object $configurations
  34.      */
  35.     public function apply(Request $request$configurations)
  36.     {
  37.         if (\is_object($configurations)) {
  38.             $configurations = [$configurations];
  39.         }
  40.         foreach ($configurations as $configuration) {
  41.             $this->applyConverter($request$configuration);
  42.         }
  43.     }
  44.     /**
  45.      * Applies converter on request based on the given configuration.
  46.      */
  47.     private function applyConverter(Request $requestParamConverter $configuration)
  48.     {
  49.         $value $request->attributes->get($configuration->getName());
  50.         $className $configuration->getClass();
  51.         // If the value is already an instance of the class we are trying to convert it into
  52.         // we should continue as no conversion is required
  53.         if (\is_object($value) && $value instanceof $className) {
  54.             return;
  55.         }
  56.         if ($converterName $configuration->getConverter()) {
  57.             if (!isset($this->namedConverters[$converterName])) {
  58.                 throw new \RuntimeException(sprintf("No converter named '%s' found for conversion of parameter '%s'."$converterName$configuration->getName()));
  59.             }
  60.             $converter $this->namedConverters[$converterName];
  61.             if (!$converter->supports($configuration)) {
  62.                 throw new \RuntimeException(sprintf("Converter '%s' does not support conversion of parameter '%s'."$converterName$configuration->getName()));
  63.             }
  64.             $converter->apply($request$configuration);
  65.             return;
  66.         }
  67.         foreach ($this->all() as $converter) {
  68.             if ($converter->supports($configuration)) {
  69.                 if ($converter->apply($request$configuration)) {
  70.                     return;
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     /**
  76.      * Adds a parameter converter.
  77.      *
  78.      * Converters match either explicitly via $name or by iteration over all
  79.      * converters with a $priority. If you pass a $priority = null then the
  80.      * added converter will not be part of the iteration chain and can only
  81.      * be invoked explicitly.
  82.      *
  83.      * @param int    $priority the priority (between -10 and 10)
  84.      * @param string $name     name of the converter
  85.      */
  86.     public function add(ParamConverterInterface $converter$priority 0$name null)
  87.     {
  88.         if (null !== $priority) {
  89.             if (!isset($this->converters[$priority])) {
  90.                 $this->converters[$priority] = [];
  91.             }
  92.             $this->converters[$priority][] = $converter;
  93.         }
  94.         if (null !== $name) {
  95.             $this->namedConverters[$name] = $converter;
  96.         }
  97.     }
  98.     /**
  99.      * Returns all registered param converters.
  100.      *
  101.      * @return array An array of param converters
  102.      */
  103.     public function all()
  104.     {
  105.         krsort($this->converters);
  106.         $converters = [];
  107.         foreach ($this->converters as $all) {
  108.             $converters array_merge($converters$all);
  109.         }
  110.         return $converters;
  111.     }
  112. }