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\Metadata; |
15
|
|
|
|
16
|
|
|
use Gaufrette\Adapter\AmazonS3; |
17
|
|
|
use Gaufrette\Adapter\AwsS3; |
18
|
|
|
use Sonata\MediaBundle\Filesystem\Replicate; |
19
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @final since sonata-project/media-bundle 3.21.0 |
24
|
|
|
*/ |
25
|
|
|
class ProxyMetadataBuilder implements MetadataBuilderInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ContainerInterface |
29
|
|
|
*/ |
30
|
|
|
private $container; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* NEXT_MAJOR: remove the second parameter $map. |
34
|
|
|
* |
35
|
|
|
* @param array $map |
36
|
|
|
*/ |
37
|
|
|
public function __construct(ContainerInterface $container, ?array $map = null) |
38
|
|
|
{ |
39
|
|
|
$this->container = $container; |
40
|
|
|
|
41
|
|
|
if (null !== $map) { |
42
|
|
|
@trigger_error( |
|
|
|
|
43
|
|
|
'The "map" parameter is deprecated since sonata-project/media-bundle 2.4 and will be removed in 4.0.', |
44
|
|
|
E_USER_DEPRECATED |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function get(MediaInterface $media, $filename) |
50
|
|
|
{ |
51
|
|
|
//get adapter for current media |
52
|
|
|
if (!$this->container->has($media->getProviderName())) { |
53
|
|
|
return []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($meta = $this->getAmazonBuilder($media, $filename)) { |
|
|
|
|
57
|
|
|
return $meta; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$this->container->has('sonata.media.metadata.noop')) { |
61
|
|
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->container->get('sonata.media.metadata.noop')->get($media, $filename); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $filename |
69
|
|
|
* |
70
|
|
|
* @return array|bool |
71
|
|
|
*/ |
72
|
|
|
protected function getAmazonBuilder(MediaInterface $media, $filename) |
73
|
|
|
{ |
74
|
|
|
$adapter = $this->container->get($media->getProviderName())->getFilesystem()->getAdapter(); |
75
|
|
|
|
76
|
|
|
//handle special Replicate adapter |
77
|
|
|
if ($adapter instanceof Replicate) { |
78
|
|
|
$adapterClassNames = $adapter->getAdapterClassNames(); |
79
|
|
|
} else { |
80
|
|
|
$adapterClassNames = [\get_class($adapter)]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
//for amazon s3 |
84
|
|
|
if ((!\in_array(AmazonS3::class, $adapterClassNames, true) && !\in_array(AwsS3::class, $adapterClassNames, true)) || !$this->container->has('sonata.media.metadata.amazon')) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->container->get('sonata.media.metadata.amazon')->get($media, $filename); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: