vendor/shopware/core/Framework/Adapter/Filesystem/FilesystemFactory.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Filesystem;
  3. use League\Flysystem\AdapterInterface;
  4. use League\Flysystem\Filesystem as LeagueFilesystem;
  5. use League\Flysystem\FilesystemInterface;
  6. use League\Flysystem\PluginInterface;
  7. use Shopware\Core\Framework\Adapter\Filesystem\Adapter\AdapterFactoryInterface;
  8. use Shopware\Core\Framework\Adapter\Filesystem\Exception\AdapterFactoryNotFoundException;
  9. use Shopware\Core\Framework\Adapter\Filesystem\Exception\DuplicateFilesystemFactoryException;
  10. use Shopware\Core\Framework\Log\Package;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. #[Package('core')]
  13. class FilesystemFactory
  14. {
  15.     /**
  16.      * @var AdapterFactoryInterface[]
  17.      */
  18.     private $adapterFactories;
  19.     /**
  20.      * @var PluginInterface[]
  21.      */
  22.     private $filesystemPlugins;
  23.     /**
  24.      * @internal
  25.      *
  26.      * @param AdapterFactoryInterface[]|iterable $adapterFactories
  27.      * @param PluginInterface[]|iterable         $filesystemPlugins
  28.      *
  29.      * @throws DuplicateFilesystemFactoryException
  30.      */
  31.     public function __construct(iterable $adapterFactoriesiterable $filesystemPlugins)
  32.     {
  33.         $this->checkDuplicates($adapterFactories);
  34.         $this->adapterFactories $adapterFactories;
  35.         $this->filesystemPlugins $filesystemPlugins;
  36.     }
  37.     public function factory(array $config): FilesystemInterface
  38.     {
  39.         $config $this->resolveFilesystemConfig($config);
  40.         $factory $this->findAdapterFactory($config['type']);
  41.         if (isset($config['config']['options']['visibility'])) {
  42.             $config['visibility'] = $config['config']['options']['visibility'];
  43.         }
  44.         $filesystem = new LeagueFilesystem(
  45.             $factory->create($config['config']),
  46.             ['visibility' => $config['visibility']]
  47.         );
  48.         foreach ($this->filesystemPlugins as $plugin) {
  49.             $plugin = clone $plugin;
  50.             $plugin->setFilesystem($filesystem);
  51.             $filesystem->addPlugin($plugin);
  52.         }
  53.         return $filesystem;
  54.     }
  55.     /**
  56.      * @throws AdapterFactoryNotFoundException
  57.      */
  58.     private function findAdapterFactory(string $type): AdapterFactoryInterface
  59.     {
  60.         foreach ($this->adapterFactories as $factory) {
  61.             if ($factory->getType() === $type) {
  62.                 return $factory;
  63.             }
  64.         }
  65.         throw new AdapterFactoryNotFoundException($type);
  66.     }
  67.     /**
  68.      * @param AdapterFactoryInterface[]|iterable $adapterFactories
  69.      *
  70.      * @throws DuplicateFilesystemFactoryException
  71.      */
  72.     private function checkDuplicates(iterable $adapterFactories): void
  73.     {
  74.         $dupes = [];
  75.         foreach ($adapterFactories as $adapter) {
  76.             $type mb_strtolower($adapter->getType());
  77.             if (\array_key_exists($type$dupes)) {
  78.                 throw new DuplicateFilesystemFactoryException($type);
  79.             }
  80.             $dupes[$type] = 1;
  81.         }
  82.     }
  83.     private function resolveFilesystemConfig(array $config): array
  84.     {
  85.         $options = new OptionsResolver();
  86.         $options->setRequired(['type']);
  87.         $options->setDefined(['config''visibility''disable_asserts''url']);
  88.         $options->setDefault('config', []);
  89.         $options->setDefault('visibility'AdapterInterface::VISIBILITY_PUBLIC);
  90.         $options->setDefault('disable_asserts'false);
  91.         $options->setAllowedTypes('type''string');
  92.         $options->setAllowedTypes('config''array');
  93.         $options->setAllowedTypes('disable_asserts''bool');
  94.         $options->setAllowedValues('visibility', [AdapterInterface::VISIBILITY_PUBLICAdapterInterface::VISIBILITY_PRIVATE]);
  95.         return $options->resolve($config);
  96.     }
  97. }