-
Notifications
You must be signed in to change notification settings - Fork 3
/
FlySystemManager.php
161 lines (135 loc) · 4.33 KB
/
FlySystemManager.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace WShafer\PSR11FlySystem;
use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\MountManager;
use Psr\Container\ContainerInterface;
use WShafer\PSR11FlySystem\Config\FileSystemConfig;
use WShafer\PSR11FlySystem\Config\MainConfig;
use WShafer\PSR11FlySystem\Exception\UnknownFileSystemException;
use WShafer\PSR11FlySystem\Exception\UnknownPluginException;
class FlySystemManager implements ContainerInterface
{
/** @var MainConfig */
protected $config;
/** @var ContainerInterface */
protected $adaptorManager;
/** @var ContainerInterface */
protected $cacheManager;
/** @var Filesystem[]|MountManager[] */
protected $systems = [];
/** @var ContainerInterface */
protected $container;
/**
* Manager constructor.
* @param MainConfig $config
* @param ContainerInterface $adaptorManager
* @param ContainerInterface $cacheManager
* @param ContainerInterface $container
*/
public function __construct(
MainConfig $config,
ContainerInterface $adaptorManager,
ContainerInterface $cacheManager,
ContainerInterface $container
) {
$this->config = $config;
$this->adaptorManager = $adaptorManager;
$this->cacheManager = $cacheManager;
$this->container = $container;
}
/**
* @param string $id
*
* @return Filesystem|MountManager
*/
public function get($id)
{
if (key_exists($id, $this->systems)) {
return $this->systems[$id];
}
$fileSystemConfig = $this->config->getFileSystemConfig($id);
if (!$fileSystemConfig) {
throw new UnknownFileSystemException(
'Unable to locate file system '.$id.'. Please check your configuration.'
);
}
if (!$fileSystemConfig->isManager()) {
return $this->getFileSystem($id, $fileSystemConfig);
}
$fileSystems = [];
foreach ($fileSystemConfig->getFileSystems() as $name => $managerSystemConfig) {
$fileSystems[$name] = $this->getFileSystem($name, $managerSystemConfig);
}
$manager = new MountManager($fileSystems);
if ($fileSystemConfig->getPlugins()) {
$this->setPlugins($manager, $fileSystemConfig->getPlugins());
}
$this->systems[$id] = $manager;
return $this->systems[$id];
}
/**
* @param $id
* @param FileSystemConfig $fileSystemConfig
* @return Filesystem
*/
protected function getFileSystem($id, FileSystemConfig $fileSystemConfig)
{
$adaptor = $this->adaptorManager->get($fileSystemConfig->getAdaptor());
$cache = $this->cacheManager->get($fileSystemConfig->getCache());
$cachedAdaptor = new CachedAdapter($adaptor, $cache);
$fileSystem = new Filesystem($cachedAdaptor);
if ($fileSystemConfig->getPlugins()) {
$this->setPlugins($fileSystem, $fileSystemConfig->getPlugins());
}
$this->systems[$id] = $fileSystem;
return $fileSystem;
}
/**
* @param FilesystemInterface|MountManager $filesystem
* @param array $plugins
*/
protected function setPlugins($filesystem, array $plugins = [])
{
foreach ($plugins as $plugin) {
$this->addPlugin($filesystem, $plugin);
}
}
/**
* @param FilesystemInterface|MountManager $filesystem
* @param string $pluginName
*/
protected function addPlugin($filesystem, string $pluginName)
{
if (!$this->container->has($pluginName)) {
throw new UnknownPluginException(
'Unable to locate plugin service '.$pluginName.' in the container. Please check your config.'
);
}
$filesystem->addPlugin($this->container->get($pluginName));
}
public function has($id)
{
return $this->config->hasFileSystemConfig($id);
}
/**
* Get the Adaptor manager
*
* @return ContainerInterface
*/
public function getAdaptorManager()
{
return $this->adaptorManager;
}
/**
* Get the Cache Manager
*
* @return mixed
*/
public function getCacheManager()
{
return $this->cacheManager;
}
}