|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Imagine\Data; |
|
13
|
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Binary\BinaryInterface; |
|
15
|
|
|
use Liip\ImagineBundle\Binary\Loader\LoaderInterface; |
|
16
|
|
|
use Liip\ImagineBundle\Binary\MimeTypeGuesserInterface; |
|
17
|
|
|
use Liip\ImagineBundle\Exception\InvalidArgumentException; |
|
18
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration; |
|
19
|
|
|
use Liip\ImagineBundle\Model\Binary; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface as DeprecatedExtensionGuesserInterface; |
|
21
|
|
|
use Symfony\Component\Mime\MimeTypesInterface; |
|
22
|
|
|
|
|
23
|
|
|
class DataManager |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var MimeTypeGuesserInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $mimeTypeGuesser; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var DeprecatedExtensionGuesserInterface|MimeTypesInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $extensionGuesser; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var FilterConfiguration |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $filterConfig; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string|null |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $defaultLoader; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string|null |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $globalDefaultImage; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var LoaderInterface[] |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $loaders = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param MimeTypeGuesserInterface $mimeTypeGuesser |
|
57
|
|
|
* @param DeprecatedExtensionGuesserInterface|MimeTypesInterface $extensionGuesser |
|
58
|
|
|
* @param FilterConfiguration $filterConfig |
|
59
|
|
|
* @param string $defaultLoader |
|
60
|
|
|
* @param string $globalDefaultImage |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct( |
|
63
|
|
|
MimeTypeGuesserInterface $mimeTypeGuesser, |
|
64
|
|
|
$extensionGuesser, |
|
65
|
|
|
FilterConfiguration $filterConfig, |
|
66
|
|
|
$defaultLoader = null, |
|
67
|
|
|
$globalDefaultImage = null |
|
68
|
|
|
) { |
|
69
|
|
|
if (!$extensionGuesser instanceof MimeTypesInterface && !$extensionGuesser instanceof DeprecatedExtensionGuesserInterface) { |
|
70
|
|
|
throw new InvalidArgumentException('$extensionGuesser must be an instance of Symfony\Component\Mime\MimeTypesInterface or Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (interface_exists(MimeTypesInterface::class) && $extensionGuesser instanceof DeprecatedExtensionGuesserInterface) { |
|
74
|
|
|
@trigger_error(sprintf('Passing a %s to "%s()" is deprecated since Symfony 4.3, pass a "%s" instead.', DeprecatedExtensionGuesserInterface::class, __METHOD__, MimeTypesInterface::class), E_USER_DEPRECATED); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->mimeTypeGuesser = $mimeTypeGuesser; |
|
78
|
|
|
$this->filterConfig = $filterConfig; |
|
79
|
|
|
$this->defaultLoader = $defaultLoader; |
|
80
|
|
|
$this->extensionGuesser = $extensionGuesser; |
|
81
|
|
|
$this->globalDefaultImage = $globalDefaultImage; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Adds a loader to retrieve images for the given filter. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $filter |
|
88
|
|
|
* @param LoaderInterface $loader |
|
89
|
|
|
*/ |
|
90
|
|
|
public function addLoader($filter, LoaderInterface $loader) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->loaders[$filter] = $loader; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns a loader previously attached to the given filter. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $filter |
|
99
|
|
|
* |
|
100
|
|
|
* @throws \InvalidArgumentException |
|
101
|
|
|
* |
|
102
|
|
|
* @return LoaderInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getLoader($filter) |
|
105
|
|
|
{ |
|
106
|
|
|
$config = $this->filterConfig->get($filter); |
|
107
|
|
|
|
|
108
|
|
|
$loaderName = empty($config['data_loader']) ? $this->defaultLoader : $config['data_loader']; |
|
109
|
|
|
|
|
110
|
|
|
if (!isset($this->loaders[$loaderName])) { |
|
111
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
112
|
|
|
'Could not find data loader "%s" for "%s" filter type', |
|
113
|
|
|
$loaderName, |
|
114
|
|
|
$filter |
|
115
|
|
|
)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->loaders[$loaderName]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Retrieves an image with the given filter applied. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $filter |
|
125
|
|
|
* @param string $path |
|
126
|
|
|
* |
|
127
|
|
|
* @throws \LogicException |
|
128
|
|
|
* |
|
129
|
|
|
* @return BinaryInterface |
|
130
|
|
|
*/ |
|
131
|
|
|
public function find($filter, $path) |
|
132
|
|
|
{ |
|
133
|
|
|
$loader = $this->getLoader($filter); |
|
134
|
|
|
|
|
135
|
|
|
$binary = $loader->find($path); |
|
136
|
|
|
if (!$binary instanceof BinaryInterface) { |
|
137
|
|
|
$mimeType = $this->mimeTypeGuesser->guess($binary); |
|
138
|
|
|
|
|
139
|
|
|
$extension = $this->getExtension($mimeType); |
|
140
|
|
|
$binary = new Binary( |
|
141
|
|
|
$binary, |
|
142
|
|
|
$mimeType, |
|
143
|
|
|
$extension |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if (null === $binary->getMimeType()) { |
|
148
|
|
|
throw new \LogicException(sprintf('The mime type of image %s was not guessed.', $path)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if (0 !== mb_strpos($binary->getMimeType(), 'image/')) { |
|
152
|
|
|
throw new \LogicException(sprintf('The mime type of image %s must be image/xxx got %s.', $path, $binary->getMimeType())); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $binary; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get default image url with the given filter applied. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $filter |
|
162
|
|
|
* |
|
163
|
|
|
* @return string|null |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getDefaultImageUrl($filter) |
|
166
|
|
|
{ |
|
167
|
|
|
$config = $this->filterConfig->get($filter); |
|
168
|
|
|
|
|
169
|
|
|
$defaultImage = null; |
|
170
|
|
|
if (false === empty($config['default_image'])) { |
|
171
|
|
|
$defaultImage = $config['default_image']; |
|
172
|
|
|
} elseif (!empty($this->globalDefaultImage)) { |
|
173
|
|
|
$defaultImage = $this->globalDefaultImage; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $defaultImage; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
private function getExtension(?string $mimeType): ?string |
|
180
|
|
|
{ |
|
181
|
|
|
if ($this->extensionGuesser instanceof DeprecatedExtensionGuesserInterface) { |
|
182
|
|
|
return $this->extensionGuesser->guess($mimeType); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (null === $mimeType) { |
|
186
|
|
|
return null; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: