vendor/symfony/form/ChoiceList/Factory/CachingFactoryDecorator.php line 118

Open in your IDE?
  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 Symfony\Component\Form\ChoiceList\Factory;
  11. use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
  12. use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
  13. use Symfony\Component\Form\ChoiceList\View\ChoiceListView;
  14. use Symfony\Contracts\Service\ResetInterface;
  15. /**
  16.  * Caches the choice lists created by the decorated factory.
  17.  *
  18.  * @author Bernhard Schussek <bschussek@gmail.com>
  19.  */
  20. class CachingFactoryDecorator implements ChoiceListFactoryInterfaceResetInterface
  21. {
  22.     private $decoratedFactory;
  23.     /**
  24.      * @var ChoiceListInterface[]
  25.      */
  26.     private $lists = [];
  27.     /**
  28.      * @var ChoiceListView[]
  29.      */
  30.     private $views = [];
  31.     /**
  32.      * Generates a SHA-256 hash for the given value.
  33.      *
  34.      * Optionally, a namespace string can be passed. Calling this method will
  35.      * the same values, but different namespaces, will return different hashes.
  36.      *
  37.      * @param mixed $value The value to hash
  38.      *
  39.      * @return string The SHA-256 hash
  40.      *
  41.      * @internal
  42.      */
  43.     public static function generateHash($valuestring $namespace ''): string
  44.     {
  45.         if (\is_object($value)) {
  46.             $value spl_object_hash($value);
  47.         } elseif (\is_array($value)) {
  48.             array_walk_recursive($value, function (&$v) {
  49.                 if (\is_object($v)) {
  50.                     $v spl_object_hash($v);
  51.                 }
  52.             });
  53.         }
  54.         return hash('sha256'$namespace.':'.serialize($value));
  55.     }
  56.     public function __construct(ChoiceListFactoryInterface $decoratedFactory)
  57.     {
  58.         $this->decoratedFactory $decoratedFactory;
  59.     }
  60.     /**
  61.      * Returns the decorated factory.
  62.      *
  63.      * @return ChoiceListFactoryInterface The decorated factory
  64.      */
  65.     public function getDecoratedFactory()
  66.     {
  67.         return $this->decoratedFactory;
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function createListFromChoices(iterable $choices$value null)
  73.     {
  74.         if ($choices instanceof \Traversable) {
  75.             $choices iterator_to_array($choices);
  76.         }
  77.         // The value is not validated on purpose. The decorated factory may
  78.         // decide which values to accept and which not.
  79.         $hash self::generateHash([$choices$value], 'fromChoices');
  80.         if (!isset($this->lists[$hash])) {
  81.             $this->lists[$hash] = $this->decoratedFactory->createListFromChoices($choices$value);
  82.         }
  83.         return $this->lists[$hash];
  84.     }
  85.     /**
  86.      * {@inheritdoc}
  87.      */
  88.     public function createListFromLoader(ChoiceLoaderInterface $loader$value null)
  89.     {
  90.         $hash self::generateHash([$loader$value], 'fromLoader');
  91.         if (!isset($this->lists[$hash])) {
  92.             $this->lists[$hash] = $this->decoratedFactory->createListFromLoader($loader$value);
  93.         }
  94.         return $this->lists[$hash];
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function createView(ChoiceListInterface $list$preferredChoices null$label null$index null$groupBy null$attr null)
  100.     {
  101.         // The input is not validated on purpose. This way, the decorated
  102.         // factory may decide which input to accept and which not.
  103.         $hash self::generateHash([$list$preferredChoices$label$index$groupBy$attr]);
  104.         if (!isset($this->views[$hash])) {
  105.             $this->views[$hash] = $this->decoratedFactory->createView(
  106.                 $list,
  107.                 $preferredChoices,
  108.                 $label,
  109.                 $index,
  110.                 $groupBy,
  111.                 $attr
  112.             );
  113.         }
  114.         return $this->views[$hash];
  115.     }
  116.     public function reset()
  117.     {
  118.         $this->lists = [];
  119.         $this->views = [];
  120.     }
  121. }