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