1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Gedmo\Uploadable\Mapping\Driver\Fluent as UploadableDriver; |
6
|
|
|
use Gedmo\Uploadable\Mapping\Validator; |
7
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
8
|
|
|
use LaravelDoctrine\Fluent\Builders\Builder; |
9
|
|
|
use LaravelDoctrine\Fluent\Builders\Delay; |
10
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
11
|
|
|
|
12
|
|
|
class Uploadable implements Buildable, Delay |
13
|
|
|
{ |
14
|
|
|
const MACRO_METHOD = 'uploadable'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
private $allowOverwrite = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
private $appendNumber = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $path = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $pathMethod = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $callback = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $filenameGenerator = Validator::FILENAME_GENERATOR_NONE; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $maxSize = 0; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private $allowedTypes = ''; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $disallowedTypes = ''; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var ExtensibleClassMetadata |
63
|
|
|
*/ |
64
|
|
|
private $classMetadata; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
68
|
|
|
*/ |
69
|
95 |
|
public function __construct(ExtensibleClassMetadata $classMetadata) |
70
|
|
|
{ |
71
|
95 |
|
$this->classMetadata = $classMetadata; |
72
|
95 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Enable the Uploadable extension. |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public static function enable() |
80
|
|
|
{ |
81
|
74 |
|
Builder::macro(self::MACRO_METHOD, function (Builder $builder) { |
82
|
74 |
|
return new static($builder->getClassMetadata()); |
|
|
|
|
83
|
74 |
|
}); |
84
|
|
|
|
85
|
74 |
|
UploadableFile::enable(); |
86
|
74 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* If this option is true, it will overwrite a file if it already exists. If you set "false", |
90
|
|
|
* an exception will be thrown. |
91
|
|
|
* |
92
|
|
|
* Default: false |
93
|
|
|
* |
94
|
|
|
* @return Uploadable |
95
|
|
|
*/ |
96
|
1 |
|
public function allowOverwrite() |
97
|
|
|
{ |
98
|
1 |
|
$this->allowOverwrite = true; |
99
|
|
|
|
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* If the file already exists and "allowOverwrite" is false, append a number to the filename. |
105
|
|
|
* |
106
|
|
|
* Example: if you're uploading a file named "test.txt", if the file already exists and this option |
107
|
|
|
* is true, the extension will modify the name of the uploaded file to "test-1.txt", where "1" could |
108
|
|
|
* be any number. The extension will check if the file exists until it finds a filename with a number |
109
|
|
|
* as its postfix that is not used. If you use a filename generator and this option is true, it will |
110
|
|
|
* append a number to the filename anyway if a file with the same name already exists. |
111
|
|
|
* |
112
|
|
|
* Default value: false |
113
|
|
|
* |
114
|
|
|
* @return Uploadable |
115
|
|
|
*/ |
116
|
1 |
|
public function appendNumber() |
117
|
|
|
{ |
118
|
1 |
|
$this->appendNumber = true; |
119
|
|
|
|
120
|
1 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* The path where the files represented by this entity will be moved. Path can be set in other ways: |
125
|
|
|
* From the listener or from a method. |
126
|
|
|
* |
127
|
|
|
* Default: "". |
128
|
|
|
* |
129
|
|
|
* @param string $path |
130
|
|
|
* |
131
|
|
|
* @return Uploadable |
132
|
|
|
*/ |
133
|
1 |
|
public function path($path) |
134
|
|
|
{ |
135
|
1 |
|
$this->path = $path; |
136
|
|
|
|
137
|
1 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Similar to "path", this represents the name of a method on the entity that will return the path |
142
|
|
|
* where the files represented by this entity will be moved. This is useful in several cases. For |
143
|
|
|
* example, you can set specific paths for specific entities, or you can get the path from other |
144
|
|
|
* sources (like a framework configuration) instead of hard-coding it in the entity. |
145
|
|
|
* |
146
|
|
|
* As first argument this method takes default path, so you can return a path relative to the default. |
147
|
|
|
* |
148
|
|
|
* Default: "". |
149
|
|
|
* |
150
|
|
|
* @param string $methodName |
151
|
|
|
* |
152
|
|
|
* @return Uploadable |
153
|
|
|
*/ |
154
|
2 |
|
public function pathMethod($methodName) |
155
|
|
|
{ |
156
|
2 |
|
$this->pathMethod = $methodName; |
157
|
|
|
|
158
|
2 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Allows you to set a method name. If set, the method will be called after the file is moved. |
163
|
|
|
* |
164
|
|
|
* This method will receive an array with information about the uploaded file, which includes the |
165
|
|
|
* following keys: |
166
|
|
|
* |
167
|
|
|
* <ul> |
168
|
|
|
* <li>fileName: The filename.</li> |
169
|
|
|
* <li>fileExtension: The extension of the file (including the dot). Example: .jpg</li> |
170
|
|
|
* <li>fileWithoutExt: The filename without the extension.</li> |
171
|
|
|
* <li>filePath: The file path. Example: /my/path/filename.jpg</li> |
172
|
|
|
* <li>fileMimeType: The mime-type of the file. Example: text/plain.</li> |
173
|
|
|
* <li>fileSize: Size of the file in bytes. Example: 140000.</li> |
174
|
|
|
* </ul> |
175
|
|
|
* |
176
|
|
|
* Default value: "". |
177
|
|
|
* |
178
|
|
|
* @param string $callback |
179
|
|
|
* |
180
|
|
|
* @return Uploadable |
181
|
|
|
*/ |
182
|
2 |
|
public function callback($callback) |
183
|
|
|
{ |
184
|
2 |
|
$this->callback = $callback; |
185
|
|
|
|
186
|
2 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Normalizes the filename, leaving only alphanumeric characters in the filename, and replacing |
191
|
|
|
* anything else with a "-". |
192
|
|
|
* |
193
|
|
|
* @return Uploadable |
194
|
|
|
*/ |
195
|
1 |
|
public function alphanumericFilename() |
196
|
|
|
{ |
197
|
1 |
|
$this->filenameGenerator = Validator::FILENAME_GENERATOR_ALPHANUMERIC; |
198
|
|
|
|
199
|
1 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Generates a sha1 filename for the file. |
204
|
|
|
* |
205
|
|
|
* @return Uploadable |
206
|
|
|
*/ |
207
|
1 |
|
public function sha1Filename() |
208
|
|
|
{ |
209
|
1 |
|
$this->filenameGenerator = Validator::FILENAME_GENERATOR_SHA1; |
210
|
|
|
|
211
|
1 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set a custom FilenameGenerator class. This class must implement |
216
|
|
|
* `Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorInterface`. |
217
|
|
|
* |
218
|
|
|
* @param string $className |
219
|
|
|
* |
220
|
|
|
* @return Uploadable |
221
|
|
|
*/ |
222
|
1 |
|
public function customFilename($className) |
223
|
|
|
{ |
224
|
1 |
|
$this->filenameGenerator = $className; |
225
|
|
|
|
226
|
1 |
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set a maximum size for the file, in bytes. If file size exceeds the value set in this |
231
|
|
|
* configuration, `UploadableMaxSizeException` will be thrown. |
232
|
|
|
* |
233
|
|
|
* Default value: 0, meaning that no size validation will occur. |
234
|
|
|
* |
235
|
|
|
* @param float $bytes |
236
|
|
|
* |
237
|
|
|
* @return Uploadable |
238
|
|
|
*/ |
239
|
2 |
|
public function maxSize($bytes) |
240
|
|
|
{ |
241
|
2 |
|
$this->maxSize = $bytes; |
|
|
|
|
242
|
|
|
|
243
|
2 |
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Allow only specific types. |
248
|
|
|
* |
249
|
|
|
* @param array|string ...$type can be an array or multiple string parameters |
250
|
|
|
* |
251
|
|
|
* @return Uploadable |
252
|
|
|
*/ |
253
|
4 |
|
public function allow($type) |
254
|
|
|
{ |
255
|
4 |
|
if (!is_array($type)) { |
256
|
3 |
|
$type = func_get_args(); |
257
|
3 |
|
} |
258
|
4 |
|
$this->allowedTypes = implode(',', $type); |
259
|
|
|
|
260
|
4 |
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Disallow specific types |
265
|
|
|
* |
266
|
|
|
* @param array|string ...$type can be an array or multiple string parameters |
267
|
|
|
* |
268
|
|
|
* @return Uploadable |
269
|
|
|
*/ |
270
|
4 |
|
public function disallow($type) |
271
|
|
|
{ |
272
|
4 |
|
if (!is_array($type)) { |
273
|
3 |
|
$type = func_get_args(); |
274
|
3 |
|
} |
275
|
4 |
|
$this->disallowedTypes = implode(',', $type); |
276
|
|
|
|
277
|
4 |
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Execute the build process |
282
|
|
|
*/ |
283
|
94 |
|
public function build() |
284
|
|
|
{ |
285
|
94 |
|
$config = $this->getConfiguration(); |
286
|
|
|
|
287
|
94 |
|
Validator::validateConfiguration($this->classMetadata, $config); |
288
|
|
|
|
289
|
17 |
|
$this->classMetadata->addExtension(UploadableDriver::EXTENSION_NAME, $config); |
290
|
17 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Build the configuration, based on defaults, current extension configuration and accumulated parameters. |
294
|
|
|
* |
295
|
|
|
* @return array |
296
|
|
|
*/ |
297
|
94 |
|
private function getConfiguration() |
298
|
|
|
{ |
299
|
94 |
|
return array_merge( |
300
|
|
|
[ |
301
|
94 |
|
'fileMimeTypeField' => false, |
302
|
94 |
|
'fileNameField' => false, |
303
|
94 |
|
'filePathField' => false, |
304
|
94 |
|
'fileSizeField' => false, |
305
|
94 |
|
], |
306
|
94 |
|
$this->classMetadata->getExtension(UploadableDriver::EXTENSION_NAME), |
307
|
|
|
[ |
308
|
94 |
|
'uploadable' => true, |
309
|
94 |
|
'allowOverwrite' => $this->allowOverwrite, |
310
|
94 |
|
'appendNumber' => $this->appendNumber, |
311
|
94 |
|
'path' => $this->path, |
312
|
94 |
|
'pathMethod' => $this->pathMethod, |
313
|
94 |
|
'callback' => $this->callback, |
314
|
94 |
|
'filenameGenerator' => $this->filenameGenerator, |
315
|
94 |
|
'maxSize' => (double)$this->maxSize, |
316
|
94 |
|
'allowedTypes' => $this->allowedTypes, |
317
|
94 |
|
'disallowedTypes' => $this->disallowedTypes, |
318
|
|
|
] |
319
|
94 |
|
); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.