1 | <?php |
||
35 | class AwsS3Provider extends Provider implements ProviderInterface |
||
36 | { |
||
37 | /** |
||
38 | * All the configurations needed by this class with the |
||
39 | * optional configurations default values. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $default = [ |
||
44 | 'url' => null, |
||
45 | 'threshold' => 10, |
||
46 | 'providers' => [ |
||
47 | 'aws' => [ |
||
48 | 's3' => [ |
||
49 | 'version' => null, |
||
50 | 'region' => null, |
||
51 | 'endpoint' => null, |
||
52 | 'buckets' => null, |
||
53 | 'upload_folder' => '', |
||
54 | 'http' => null, |
||
55 | 'acl' => 'public-read', |
||
56 | 'cloudfront' => [ |
||
57 | 'use' => false, |
||
58 | 'cdn_url' => null, |
||
59 | ], |
||
60 | ], |
||
61 | ], |
||
62 | ], |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * Required configurations (must exist in the config file). |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $rules = ['version', 'region', 'key', 'secret', 'buckets', 'url']; |
||
71 | |||
72 | /** |
||
73 | * this array holds the parsed configuration to be used across the class. |
||
74 | * |
||
75 | * @var Array |
||
76 | */ |
||
77 | protected $supplier; |
||
78 | |||
79 | /** |
||
80 | * @var Instance of Aws\S3\S3Client |
||
81 | */ |
||
82 | protected $s3_client; |
||
83 | |||
84 | /** |
||
85 | * @var Instance of Guzzle\Batch\BatchBuilder |
||
86 | */ |
||
87 | protected $batch; |
||
88 | |||
89 | /** |
||
90 | * @var \Publiux\laravelcdn\Contracts\CdnHelperInterface |
||
91 | */ |
||
92 | protected $cdn_helper; |
||
93 | |||
94 | /** |
||
95 | * @var \Publiux\laravelcdn\Validators\Contracts\ConfigurationsInterface |
||
96 | */ |
||
97 | protected $configurations; |
||
98 | |||
99 | /** |
||
100 | * @var \Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface |
||
101 | */ |
||
102 | protected $provider_validator; |
||
103 | |||
104 | /** |
||
105 | * @param \Symfony\Component\Console\Output\ConsoleOutput $console |
||
106 | * @param \Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface $provider_validator |
||
107 | * @param \Publiux\laravelcdn\Contracts\CdnHelperInterface $cdn_helper |
||
108 | */ |
||
109 | public function __construct( |
||
118 | |||
119 | /** |
||
120 | * Read the configuration and prepare an array with the relevant configurations |
||
121 | * for the (AWS S3) provider. and return itself. |
||
122 | * |
||
123 | * @param $configurations |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function init($configurations) |
||
154 | |||
155 | /** |
||
156 | * Upload assets. |
||
157 | * |
||
158 | * @param $assets |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function upload($assets) |
||
216 | |||
217 | /** |
||
218 | * Create an S3 client instance |
||
219 | * (Note: it will read the credentials form the .env file). |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function connect() |
||
242 | |||
243 | /** |
||
244 | * @param $s3_client |
||
245 | */ |
||
246 | public function setS3Client($s3_client) |
||
250 | |||
251 | /** |
||
252 | * @param $assets |
||
253 | * @return mixed |
||
254 | */ |
||
255 | private function getFilesAlreadyOnBucket($assets) |
||
256 | { |
||
257 | $filesOnAWS = new Collection([]); |
||
258 | |||
259 | $files = $this->s3_client->listObjects([ |
||
260 | 'Bucket' => $this->getBucket(), |
||
261 | ]); |
||
262 | |||
263 | if (!$files['Contents']) { |
||
264 | //no files on bucket. lets upload everything found. |
||
265 | return $assets; |
||
266 | } |
||
267 | |||
268 | foreach ($files['Contents'] as $file) { |
||
269 | $a = [ |
||
270 | 'Key' => $file['Key'], |
||
271 | "LastModified" => $file['LastModified']->getTimestamp(), |
||
272 | 'Size' => $file['Size'] |
||
273 | ]; |
||
274 | $filesOnAWS->put($file['Key'], $a); |
||
275 | } |
||
276 | |||
277 | $assets->transform(function ($item, $key) use (&$filesOnAWS) { |
||
278 | $fileOnAWS = $filesOnAWS->get(str_replace('\\', '/', $this->upload_folder.$item->getPathName())); |
||
279 | |||
280 | if (!$fileOnAWS) { |
||
281 | return $item; |
||
282 | } |
||
283 | |||
284 | //select to upload files that are different in size AND last modified time. |
||
285 | if (!($item->getMTime() < (int) $fileOnAWS['LastModified']) || !($item->getSize() === (int) $fileOnAWS['Size'])) { |
||
286 | return $item; |
||
287 | } |
||
288 | }); |
||
289 | |||
290 | $assets = $assets->reject(function ($item) { |
||
291 | return $item === null; |
||
292 | }); |
||
293 | |||
294 | return $assets; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @return array |
||
299 | */ |
||
300 | public function getBucket() |
||
312 | |||
313 | /** |
||
314 | * Empty bucket. |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function emptyBucket() |
||
362 | |||
363 | /** |
||
364 | * This function will be called from the CdnFacade class when |
||
365 | * someone use this {{ Cdn::asset('') }} facade helper. |
||
366 | * |
||
367 | * @param $path |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | public function urlGenerator($path) |
||
386 | |||
387 | /** |
||
388 | * @return string |
||
389 | */ |
||
390 | public function getCloudFront() |
||
398 | |||
399 | /** |
||
400 | * @return string |
||
401 | */ |
||
402 | public function getCloudFrontUrl() |
||
406 | |||
407 | /** |
||
408 | * @return string |
||
409 | */ |
||
410 | public function getUrl() |
||
414 | |||
415 | /** |
||
416 | * @param $attr |
||
417 | * |
||
418 | * @return Mix | null |
||
419 | */ |
||
420 | public function __get($attr) |
||
424 | } |
||
425 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..