|
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\Exception\LogicException; |
|
19
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration; |
|
20
|
|
|
use Liip\ImagineBundle\Model\Binary; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface as DeprecatedExtensionGuesserInterface; |
|
22
|
|
|
use Symfony\Component\Mime\MimeTypesInterface; |
|
23
|
|
|
|
|
24
|
|
|
class DataManager |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var MimeTypeGuesserInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $mimeTypeGuesser; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var DeprecatedExtensionGuesserInterface|MimeTypesInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $extensionGuesser; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var FilterConfiguration |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $filterConfig; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string|null |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $defaultLoader; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string|null |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $globalDefaultImage; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var LoaderInterface[] |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $loaders = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param DeprecatedExtensionGuesserInterface|MimeTypesInterface $extensionGuesser |
|
58
|
|
|
* @param string $defaultLoader |
|
59
|
|
|
* @param string $globalDefaultImage |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct( |
|
62
|
|
|
MimeTypeGuesserInterface $mimeTypeGuesser, |
|
63
|
|
|
$extensionGuesser, |
|
64
|
|
|
FilterConfiguration $filterConfig, |
|
65
|
|
|
$defaultLoader = null, |
|
66
|
|
|
$globalDefaultImage = null |
|
67
|
|
|
) { |
|
68
|
|
|
if (!$extensionGuesser instanceof MimeTypesInterface && !$extensionGuesser instanceof DeprecatedExtensionGuesserInterface) { |
|
|
|
|
|
|
69
|
|
|
throw new InvalidArgumentException('$extensionGuesser must be an instance of Symfony\Component\Mime\MimeTypesInterface or Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (interface_exists(MimeTypesInterface::class) && $extensionGuesser instanceof DeprecatedExtensionGuesserInterface) { |
|
|
|
|
|
|
73
|
|
|
@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); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->mimeTypeGuesser = $mimeTypeGuesser; |
|
77
|
|
|
$this->filterConfig = $filterConfig; |
|
78
|
|
|
$this->defaultLoader = $defaultLoader; |
|
79
|
|
|
$this->extensionGuesser = $extensionGuesser; |
|
80
|
|
|
$this->globalDefaultImage = $globalDefaultImage; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Adds a loader to retrieve images for the given filter. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $filter |
|
87
|
|
|
*/ |
|
88
|
|
|
public function addLoader($filter, LoaderInterface $loader) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->loaders[$filter] = $loader; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns a loader previously attached to the given filter. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $filter |
|
97
|
|
|
* |
|
98
|
|
|
* @throws \InvalidArgumentException |
|
99
|
|
|
* |
|
100
|
|
|
* @return LoaderInterface |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getLoader($filter) |
|
103
|
|
|
{ |
|
104
|
|
|
$config = $this->filterConfig->get($filter); |
|
105
|
|
|
|
|
106
|
|
|
$loaderName = empty($config['data_loader']) ? $this->defaultLoader : $config['data_loader']; |
|
107
|
|
|
|
|
108
|
|
|
if (!isset($this->loaders[$loaderName])) { |
|
109
|
|
|
throw new \InvalidArgumentException(sprintf('Could not find data loader "%s" for "%s" filter type', $loaderName, $filter)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this->loaders[$loaderName]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Retrieves an image with the given filter applied. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $filter |
|
119
|
|
|
* @param string $path |
|
120
|
|
|
* |
|
121
|
|
|
* @throws LogicException |
|
122
|
|
|
* |
|
123
|
|
|
* @return BinaryInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
public function find($filter, $path) |
|
126
|
|
|
{ |
|
127
|
|
|
$loader = $this->getLoader($filter); |
|
128
|
|
|
|
|
129
|
|
|
$binary = $loader->find($path); |
|
130
|
|
|
if (!$binary instanceof BinaryInterface) { |
|
131
|
|
|
$mimeType = $this->mimeTypeGuesser->guess($binary); |
|
132
|
|
|
|
|
133
|
|
|
$extension = $this->getExtension($mimeType); |
|
134
|
|
|
$binary = new Binary( |
|
135
|
|
|
$binary, |
|
136
|
|
|
$mimeType, |
|
137
|
|
|
$extension |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if (null === $binary->getMimeType()) { |
|
142
|
|
|
throw new LogicException(sprintf('The mime type of image %s was not guessed.', $path)); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if (0 !== mb_strpos($binary->getMimeType(), 'image/') && 'application/pdf' !== $binary->getMimeType()) { |
|
146
|
|
|
throw new LogicException(sprintf('The mime type of file %s must be image/xxx or application/pdf, got %s.', $path, $binary->getMimeType())); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $binary; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get default image url with the given filter applied. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $filter |
|
156
|
|
|
* |
|
157
|
|
|
* @return string|null |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getDefaultImageUrl($filter) |
|
160
|
|
|
{ |
|
161
|
|
|
$config = $this->filterConfig->get($filter); |
|
162
|
|
|
|
|
163
|
|
|
$defaultImage = null; |
|
164
|
|
|
if (false === empty($config['default_image'])) { |
|
165
|
|
|
$defaultImage = $config['default_image']; |
|
166
|
|
|
} elseif (!empty($this->globalDefaultImage)) { |
|
167
|
|
|
$defaultImage = $this->globalDefaultImage; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $defaultImage; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private function getExtension(?string $mimeType): ?string |
|
174
|
|
|
{ |
|
175
|
|
|
if ($this->extensionGuesser instanceof DeprecatedExtensionGuesserInterface) { |
|
|
|
|
|
|
176
|
|
|
return $this->extensionGuesser->guess($mimeType); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (null === $mimeType) { |
|
180
|
|
|
return null; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $this->extensionGuesser->getExtensions($mimeType)[0] ?? null; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.