1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Filesystem; |
15
|
|
|
|
16
|
|
|
use Gaufrette\Adapter as AdapterInterface; |
17
|
|
|
use Gaufrette\Adapter\MetadataSupporter; |
18
|
|
|
use Gaufrette\Filesystem; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @final since sonata-project/media-bundle 3.21.0 |
23
|
|
|
*/ |
24
|
|
|
class Replicate implements AdapterInterface, MetadataSupporter |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var AdapterInterface |
28
|
|
|
*/ |
29
|
|
|
protected $master; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AdapterInterface |
33
|
|
|
*/ |
34
|
|
|
protected $slave; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var LoggerInterface |
38
|
|
|
*/ |
39
|
|
|
protected $logger; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param LoggerInterface $logger |
43
|
|
|
*/ |
44
|
|
|
public function __construct(AdapterInterface $master, AdapterInterface $slave, ?LoggerInterface $logger = null) |
45
|
|
|
{ |
46
|
|
|
$this->master = $master; |
47
|
|
|
$this->slave = $slave; |
48
|
|
|
$this->logger = $logger; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function delete($key) |
52
|
|
|
{ |
53
|
|
|
$ok = true; |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$this->slave->delete($key); |
57
|
|
|
} catch (\Exception $e) { |
58
|
|
|
if ($this->logger) { |
59
|
|
|
$this->logger->critical(sprintf('Unable to delete %s, error: %s', $key, $e->getMessage())); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$ok = false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$this->master->delete($key); |
67
|
|
|
} catch (\Exception $e) { |
68
|
|
|
if ($this->logger) { |
69
|
|
|
$this->logger->critical(sprintf('Unable to delete %s, error: %s', $key, $e->getMessage())); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$ok = false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $ok; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function mtime($key) |
79
|
|
|
{ |
80
|
|
|
return $this->master->mtime($key); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function keys() |
84
|
|
|
{ |
85
|
|
|
return $this->master->keys(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function exists($key) |
89
|
|
|
{ |
90
|
|
|
return $this->master->exists($key); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function write($key, $content, ?array $metadata = null) |
94
|
|
|
{ |
95
|
|
|
$ok = true; |
96
|
|
|
$return = false; |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
$return = $this->master->write($key, $content, $metadata); |
|
|
|
|
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
if ($this->logger) { |
102
|
|
|
$this->logger->critical(sprintf('Unable to write %s, error: %s', $key, $e->getMessage())); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$ok = false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
$return = $this->slave->write($key, $content, $metadata); |
|
|
|
|
110
|
|
|
} catch (\Exception $e) { |
111
|
|
|
if ($this->logger) { |
112
|
|
|
$this->logger->critical(sprintf('Unable to write %s, error: %s', $key, $e->getMessage())); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$ok = false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $ok && $return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function read($key) |
122
|
|
|
{ |
123
|
|
|
return $this->master->read($key); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function rename($key, $new) |
127
|
|
|
{ |
128
|
|
|
$ok = true; |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
$this->master->rename($key, $new); |
132
|
|
|
} catch (\Exception $e) { |
133
|
|
|
if ($this->logger) { |
134
|
|
|
$this->logger->critical(sprintf('Unable to rename %s, error: %s', $key, $e->getMessage())); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$ok = false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
try { |
141
|
|
|
$this->slave->rename($key, $new); |
142
|
|
|
} catch (\Exception $e) { |
143
|
|
|
if ($this->logger) { |
144
|
|
|
$this->logger->critical(sprintf('Unable to rename %s, error: %s', $key, $e->getMessage())); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$ok = false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $ok; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* If one of the adapters can allow inserting metadata. |
155
|
|
|
* |
156
|
|
|
* @return bool true if supports metadata, false if not |
157
|
|
|
*/ |
158
|
|
|
public function supportsMetadata() |
159
|
|
|
{ |
160
|
|
|
return $this->master instanceof MetadataSupporter || $this->slave instanceof MetadataSupporter; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function setMetadata($key, $metadata): void |
164
|
|
|
{ |
165
|
|
|
if ($this->master instanceof MetadataSupporter) { |
166
|
|
|
$this->master->setMetadata($key, $metadata); |
167
|
|
|
} |
168
|
|
|
if ($this->slave instanceof MetadataSupporter) { |
169
|
|
|
$this->slave->setMetadata($key, $metadata); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getMetadata($key) |
174
|
|
|
{ |
175
|
|
|
if ($this->master instanceof MetadataSupporter) { |
176
|
|
|
return $this->master->getMetadata($key); |
177
|
|
|
} elseif ($this->slave instanceof MetadataSupporter) { |
178
|
|
|
return $this->slave->getMetadata($key); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return []; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Gets the class names as an array for both adapters. |
186
|
|
|
* |
187
|
|
|
* @return string[] |
188
|
|
|
*/ |
189
|
|
|
public function getAdapterClassNames() |
190
|
|
|
{ |
191
|
|
|
return [ |
192
|
|
|
\get_class($this->master), |
193
|
|
|
\get_class($this->slave), |
194
|
|
|
]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function createFile($key, Filesystem $filesystem) |
198
|
|
|
{ |
199
|
|
|
return $this->master->createFile($key, $filesystem); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function createFileStream($key, Filesystem $filesystem) |
203
|
|
|
{ |
204
|
|
|
return $this->master->createFileStream($key, $filesystem); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function listDirectory($directory = '') |
208
|
|
|
{ |
209
|
|
|
return $this->master->listDirectory($directory); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function isDirectory($key) |
213
|
|
|
{ |
214
|
|
|
return $this->master->isDirectory($key); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.