1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Florian Krämer (https://florian-kraemer.net) |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net) |
10
|
|
|
* @author Florian Krämer |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phauthentic\Infrastructure\Storage; |
18
|
|
|
|
19
|
|
|
use InvalidArgumentException; |
20
|
|
|
use League\Flysystem\AdapterInterface; |
21
|
|
|
use League\Flysystem\Config; |
22
|
|
|
use Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface; |
23
|
|
|
use Phauthentic\Infrastructure\Storage\Processor\Exception\VariantDoesNotExistException; |
24
|
|
|
use Phauthentic\Infrastructure\Storage\Processor\Exception\VariantException; |
25
|
|
|
use Phauthentic\Infrastructure\Storage\UrlBuilder\UrlBuilderInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* File Storage |
29
|
|
|
*/ |
30
|
|
|
class FileStorage implements FileStorageInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected array $callbacks = [ |
36
|
|
|
'beforeSave' => [], |
37
|
|
|
'afterSave' => [], |
38
|
|
|
'beforeRemove' => [], |
39
|
|
|
'afterRemove' => [], |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface|null |
44
|
|
|
*/ |
45
|
|
|
protected ?PathBuilderInterface $pathBuilder; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Phauthentic\Infrastructure\Storage\UrlBuilder\UrlBuilderInterface|null |
49
|
|
|
*/ |
50
|
|
|
protected ?UrlBuilderInterface $urlBuilder; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Phauthentic\Infrastructure\Storage\StorageServiceInterface |
54
|
|
|
*/ |
55
|
|
|
protected StorageServiceInterface $storageService; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructor |
59
|
|
|
* |
60
|
|
|
* @param \Phauthentic\Infrastructure\Storage\StorageServiceInterface $storageService Storage Service |
61
|
|
|
* @param \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface|null $pathBuilder Path Builder |
62
|
|
|
* @param \Phauthentic\Infrastructure\Storage\UrlBuilder\UrlBuilderInterface|null $urlBuilder Path Builder |
63
|
|
|
*/ |
64
|
1 |
|
public function __construct( |
65
|
|
|
StorageServiceInterface $storageService, |
66
|
|
|
?PathBuilderInterface $pathBuilder = null, |
67
|
|
|
?UrlBuilderInterface $urlBuilder = null |
68
|
|
|
) { |
69
|
1 |
|
$this->storageService = $storageService; |
70
|
1 |
|
$this->pathBuilder = $pathBuilder; |
71
|
1 |
|
$this->urlBuilder = $urlBuilder; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $type Type |
76
|
|
|
* @param \Phauthentic\Infrastructure\Storage\FileInterface $file File |
77
|
|
|
* @return \Phauthentic\Infrastructure\Storage\FileInterface |
78
|
|
|
*/ |
79
|
1 |
|
public function runCallbacks(string $type, FileInterface $file): FileInterface |
80
|
|
|
{ |
81
|
1 |
|
if (!isset($this->callbacks[$type])) { |
82
|
|
|
throw new InvalidArgumentException(sprintf( |
83
|
|
|
'Type %s is invalid', |
84
|
|
|
$type |
85
|
|
|
)); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
foreach ($this->callbacks[$type] as $callback) { |
89
|
|
|
$file = $callback($file); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $file; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
1 |
|
public function store(FileInterface $file): FileInterface |
99
|
|
|
{ |
100
|
1 |
|
$config = new Config(); |
101
|
|
|
|
102
|
1 |
|
if ($this->pathBuilder !== null) { |
103
|
1 |
|
$file = $file->buildPath($this->pathBuilder); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
if ($this->urlBuilder !== null) { |
107
|
|
|
$file = $file->buildUrl($this->urlBuilder); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$file = $this->runCallbacks('beforeSave', $file); |
111
|
|
|
|
112
|
1 |
|
$storage = $this->getStorage($file->storage()); |
113
|
1 |
|
$storage->writeStream($file->path(), $file->resource(), $config); |
114
|
|
|
|
115
|
1 |
|
return $this->runCallbacks('afterSave', $file); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
1 |
|
public function remove(FileInterface $file): FileInterface |
122
|
|
|
{ |
123
|
1 |
|
$file = $this->runCallbacks('beforeRemove', $file); |
124
|
|
|
|
125
|
|
|
// Delete all variants of the file |
126
|
1 |
|
foreach ($file->variants() as $variant) { |
127
|
|
|
if (!empty($variant['path'])) { |
128
|
|
|
$this->getStorage($file->storage())->delete($variant['path']); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Delete the file |
133
|
1 |
|
$this->getStorage($file->storage())->delete($file->path()); |
134
|
|
|
|
135
|
1 |
|
return $this->runCallbacks('afterRemove', $file); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritDoc |
140
|
|
|
*/ |
141
|
|
|
public function removeVariant(FileInterface $file, string $name): FileInterface |
142
|
|
|
{ |
143
|
|
|
if (!$file->hasVariant($name)) { |
144
|
|
|
throw VariantDoesNotExistException::withName($name); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$variant = $file->variant($name); |
148
|
|
|
if (empty($variant['path'])) { |
149
|
|
|
throw new VariantException(sprintf( |
150
|
|
|
'Variant `%s` is missing a path', |
151
|
|
|
$name |
152
|
|
|
)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->getStorage($file->storage())->delete($variant['path']); |
156
|
|
|
|
157
|
|
|
$variants = $file->variants(); |
158
|
|
|
unset($variants[$name]); |
159
|
|
|
|
160
|
|
|
return $file->withVariants($variants, false); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Gets the storage abstraction to use |
165
|
|
|
* |
166
|
|
|
* @param string $storage Storage name to use |
167
|
|
|
* @return \League\Flysystem\AdapterInterface |
168
|
|
|
*/ |
169
|
1 |
|
public function getStorage(string $storage): AdapterInterface |
170
|
|
|
{ |
171
|
1 |
|
return $this->storageService->adapter($storage); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|