-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStorageService.php
213 lines (187 loc) · 5.75 KB
/
StorageService.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php declare(strict_types=1);
/**
* Copyright (c) Florian Krämer (https://florian-kraemer.net)
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
* @author Florian Krämer
* @link https://github.com/Phauthentic
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace PhpCollective\Infrastructure\Storage;
use League\Flysystem\Config;
use League\Flysystem\FilesystemAdapter;
use PhpCollective\Infrastructure\Storage\Factories\Exception\FactoryNotFoundException;
use RuntimeException;
/**
* StorageFactory - Manages and instantiates storage engine adapters.
*
* @author Florian Krämer
* @copyright 2012 - 2020 Florian Krämer
* @license MIT
*/
class StorageService implements StorageServiceInterface
{
/**
* @var array
*/
protected array $adapterConfig = [];
/**
* @var \PhpCollective\Infrastructure\Storage\AdapterCollectionInterface
*/
protected AdapterCollectionInterface $adapterCollection;
/**
* @var \PhpCollective\Infrastructure\Storage\StorageAdapterFactoryInterface
*/
protected StorageAdapterFactoryInterface $adapterFactory;
/**
* Constructor
*
* @param \PhpCollective\Infrastructure\Storage\StorageAdapterFactoryInterface $adapterFactory Adapter Factory
* @param \PhpCollective\Infrastructure\Storage\AdapterCollectionInterface|null $factoryCollection Factory Collection
*/
public function __construct(
StorageAdapterFactoryInterface $adapterFactory,
?AdapterCollectionInterface $factoryCollection = null
) {
$this->adapterFactory = $adapterFactory;
$this->adapterCollection = $factoryCollection ?? new AdapterCollection();
}
/**
* Adapter Factory
*
* @return \PhpCollective\Infrastructure\Storage\StorageAdapterFactoryInterface
*/
public function adapterFactory(): StorageAdapterFactoryInterface
{
return $this->adapterFactory;
}
/**
* @inheritDoc
*/
public function adapters(): AdapterCollectionInterface
{
return $this->adapterCollection;
}
/**
* @inheritDoc
*
* @throws \PhpCollective\Infrastructure\Storage\Factories\Exception\FactoryNotFoundException
*/
public function adapter(string $name): FilesystemAdapter
{
if ($this->adapterCollection->has($name)) {
return $this->adapterCollection->get($name);
}
if (!isset($this->adapterConfig[$name])) {
throw FactoryNotFoundException::withName($name);
}
$options = $this->adapterConfig[$name];
return $this->loadAdapter($name, $options['class'], $options['options']);
}
/**
* Loads an adapter instance using the factory
*
* @param string $name Name
* @param string $adapter Adapter
* @param array $options
*
* @return \League\Flysystem\FilesystemAdapter
*/
public function loadAdapter(string $name, string $adapter, array $options): FilesystemAdapter
{
$adapter = $this->adapterFactory->buildStorageAdapter(
$adapter,
$options,
);
$this->adapterCollection->add($name, $adapter);
return $adapter;
}
/**
* Adds an adapter config
*
* @param string $name
* @param string $class
* @param array $options
*
* @return void
*/
public function addAdapterConfig(string $name, string $class, array $options)
{
$this->adapterConfig[$name] = [
'class' => $class,
'options' => $options,
];
}
/**
* Sets the adapter configuration to lazy load them later
*
* @param array $config Config
*
* @throws \RuntimeException
*
* @return void
*/
public function setAdapterConfigFromArray(array $config): void
{
foreach ($config as $name => $options) {
if (!isset($options['class'])) {
throw new RuntimeException('Adapter class or name is missing');
}
if (!isset($options['options']) || !is_array($options['options'])) {
throw new RuntimeException('Adapter options must be an array');
}
$this->adapterConfig[$name] = $options;
}
}
/**
* @param \League\Flysystem\Config|null $config Config
*
* @return \League\Flysystem\Config
*/
protected function makeConfigIfNeeded(?Config $config)
{
if ($config === null) {
$config = new Config();
}
return $config;
}
/**
* @inheritDoc
*/
public function storeResource(string $adapter, string $path, $resource, ?Config $config = null): void
{
$config = $this->makeConfigIfNeeded($config);
$this->adapter($adapter)->writeStream($path, $resource, $config);
}
/**
* @inheritDoc
*/
public function storeFile(string $adapter, string $path, string $file, ?Config $config = null): void
{
$config = $this->makeConfigIfNeeded($config);
$this->adapter($adapter)->write($path, (string)file_get_contents($file), $config);
}
/**
* @param string $adapter Adapter
* @param string $path Path
*
* @return bool
*/
public function fileExists(string $adapter, string $path): bool
{
return $this->adapter($adapter)->fileExists($path);
}
/**
* @param string $adapter Name
* @param string $path File to delete
*
* @return void
*/
public function removeFile(string $adapter, string $path): void
{
$this->adapter($adapter)->delete($path);
}
}