These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Twig\Extension; |
||
15 | |||
16 | use Sonata\Doctrine\Model\ManagerInterface; |
||
17 | use Sonata\MediaBundle\Model\MediaInterface; |
||
18 | use Sonata\MediaBundle\Provider\Pool; |
||
19 | use Sonata\MediaBundle\Twig\TokenParser\MediaTokenParser; |
||
20 | use Sonata\MediaBundle\Twig\TokenParser\PathTokenParser; |
||
21 | use Sonata\MediaBundle\Twig\TokenParser\ThumbnailTokenParser; |
||
22 | use Twig\Environment; |
||
23 | use Twig\Extension\AbstractExtension; |
||
24 | use Twig\Extension\InitRuntimeInterface; |
||
25 | |||
26 | /** |
||
27 | * @final since sonata-project/media-bundle 3.21.0 |
||
28 | */ |
||
29 | class MediaExtension extends AbstractExtension implements InitRuntimeInterface |
||
0 ignored issues
–
show
|
|||
30 | { |
||
31 | /** |
||
32 | * @var Pool |
||
33 | */ |
||
34 | protected $mediaService; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $resources = []; |
||
40 | |||
41 | /** |
||
42 | * @var ManagerInterface |
||
43 | */ |
||
44 | protected $mediaManager; |
||
45 | |||
46 | /** |
||
47 | * @var Environment |
||
48 | */ |
||
49 | protected $environment; |
||
50 | |||
51 | public function __construct(Pool $mediaService, ManagerInterface $mediaManager) |
||
52 | { |
||
53 | $this->mediaService = $mediaService; |
||
54 | $this->mediaManager = $mediaManager; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function getTokenParsers() |
||
61 | { |
||
62 | return [ |
||
63 | new MediaTokenParser(static::class), |
||
64 | new ThumbnailTokenParser(static::class), |
||
65 | new PathTokenParser(static::class), |
||
66 | ]; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function initRuntime(Environment $environment): void |
||
73 | { |
||
74 | $this->environment = $environment; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param MediaInterface $media |
||
79 | * @param string $format |
||
80 | * @param array $options |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function media($media, $format, $options = []) |
||
85 | { |
||
86 | $media = $this->getMedia($media); |
||
87 | |||
88 | if (null === $media) { |
||
89 | return ''; |
||
90 | } |
||
91 | |||
92 | $provider = $this |
||
93 | ->getMediaService() |
||
94 | ->getProvider($media->getProviderName()); |
||
95 | |||
96 | $format = $provider->getFormatName($media, $format); |
||
97 | |||
98 | $options = $provider->getHelperProperties($media, $format, $options); |
||
99 | |||
100 | return $this->render($provider->getTemplate('helper_view'), [ |
||
101 | 'media' => $media, |
||
102 | 'format' => $format, |
||
103 | 'options' => $options, |
||
104 | ]); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns the thumbnail for the provided media. |
||
109 | * |
||
110 | * @param MediaInterface $media |
||
111 | * @param string $format |
||
112 | * @param array $options |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | public function thumbnail($media, $format, $options = []) |
||
117 | { |
||
118 | $media = $this->getMedia($media); |
||
119 | |||
120 | if (null === $media) { |
||
121 | return ''; |
||
122 | } |
||
123 | |||
124 | $provider = $this->getMediaService() |
||
125 | ->getProvider($media->getProviderName()); |
||
126 | |||
127 | $format = $provider->getFormatName($media, $format); |
||
128 | $format_definition = $provider->getFormat($format); |
||
129 | |||
130 | // build option |
||
131 | $defaultOptions = [ |
||
132 | 'title' => $media->getName(), |
||
133 | 'alt' => $media->getName(), |
||
134 | ]; |
||
135 | |||
136 | if ($format_definition['width']) { |
||
137 | $defaultOptions['width'] = $format_definition['width']; |
||
138 | } |
||
139 | if ($format_definition['height']) { |
||
140 | $defaultOptions['height'] = $format_definition['height']; |
||
141 | } |
||
142 | |||
143 | $options = array_merge($defaultOptions, $options); |
||
144 | |||
145 | $options['src'] = $provider->generatePublicUrl($media, $format); |
||
146 | |||
147 | return $this->render($provider->getTemplate('helper_thumbnail'), [ |
||
148 | 'media' => $media, |
||
149 | 'options' => $options, |
||
150 | ]); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param string $template |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | public function render($template, array $parameters = []) |
||
159 | { |
||
160 | if (!isset($this->resources[$template])) { |
||
161 | $this->resources[$template] = $this->environment->loadTemplate($template); |
||
162 | } |
||
163 | |||
164 | return $this->resources[$template]->render($parameters); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param MediaInterface $media |
||
169 | * @param string $format |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function path($media, $format) |
||
174 | { |
||
175 | $media = $this->getMedia($media); |
||
176 | |||
177 | if (!$media) { |
||
178 | return ''; |
||
179 | } |
||
180 | |||
181 | $provider = $this->getMediaService() |
||
182 | ->getProvider($media->getProviderName()); |
||
183 | |||
184 | $format = $provider->getFormatName($media, $format); |
||
185 | |||
186 | return $provider->generatePublicUrl($media, $format); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @return Pool |
||
191 | */ |
||
192 | public function getMediaService() |
||
193 | { |
||
194 | return $this->mediaService; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param mixed $media |
||
199 | */ |
||
200 | private function getMedia($media): ?MediaInterface |
||
201 | { |
||
202 | if (!$media instanceof MediaInterface && \strlen((string) $media) > 0) { |
||
203 | $media = $this->mediaManager->findOneBy([ |
||
204 | 'id' => $media, |
||
205 | ]); |
||
206 | } |
||
207 | |||
208 | if (!$media instanceof MediaInterface) { |
||
209 | return null; |
||
210 | } |
||
211 | |||
212 | if (MediaInterface::STATUS_OK !== $media->getProviderStatus()) { |
||
213 | return null; |
||
214 | } |
||
215 | |||
216 | return $media; |
||
217 | } |
||
218 | } |
||
219 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.