Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\image\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\Cache;
|
Chris@0
|
6 use Drupal\Core\Config\Entity\ConfigEntityBase;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
|
Chris@0
|
9 use Drupal\Core\Routing\RequestHelper;
|
Chris@0
|
10 use Drupal\Core\Site\Settings;
|
Chris@0
|
11 use Drupal\Core\Url;
|
Chris@0
|
12 use Drupal\image\ImageEffectPluginCollection;
|
Chris@0
|
13 use Drupal\image\ImageEffectInterface;
|
Chris@0
|
14 use Drupal\image\ImageStyleInterface;
|
Chris@0
|
15 use Drupal\Component\Utility\Crypt;
|
Chris@0
|
16 use Drupal\Component\Utility\UrlHelper;
|
Chris@0
|
17 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
18 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
Chris@0
|
20 use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Defines an image style configuration entity.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @ConfigEntityType(
|
Chris@0
|
26 * id = "image_style",
|
Chris@0
|
27 * label = @Translation("Image style"),
|
Chris@0
|
28 * handlers = {
|
Chris@0
|
29 * "form" = {
|
Chris@0
|
30 * "add" = "Drupal\image\Form\ImageStyleAddForm",
|
Chris@0
|
31 * "edit" = "Drupal\image\Form\ImageStyleEditForm",
|
Chris@0
|
32 * "delete" = "Drupal\image\Form\ImageStyleDeleteForm",
|
Chris@0
|
33 * "flush" = "Drupal\image\Form\ImageStyleFlushForm"
|
Chris@0
|
34 * },
|
Chris@0
|
35 * "list_builder" = "Drupal\image\ImageStyleListBuilder",
|
Chris@0
|
36 * "storage" = "Drupal\image\ImageStyleStorage",
|
Chris@0
|
37 * },
|
Chris@0
|
38 * admin_permission = "administer image styles",
|
Chris@0
|
39 * config_prefix = "style",
|
Chris@0
|
40 * entity_keys = {
|
Chris@0
|
41 * "id" = "name",
|
Chris@0
|
42 * "label" = "label"
|
Chris@0
|
43 * },
|
Chris@0
|
44 * links = {
|
Chris@0
|
45 * "flush-form" = "/admin/config/media/image-styles/manage/{image_style}/flush",
|
Chris@0
|
46 * "edit-form" = "/admin/config/media/image-styles/manage/{image_style}",
|
Chris@0
|
47 * "delete-form" = "/admin/config/media/image-styles/manage/{image_style}/delete",
|
Chris@0
|
48 * "collection" = "/admin/config/media/image-styles",
|
Chris@0
|
49 * },
|
Chris@0
|
50 * config_export = {
|
Chris@0
|
51 * "name",
|
Chris@0
|
52 * "label",
|
Chris@0
|
53 * "effects",
|
Chris@0
|
54 * }
|
Chris@0
|
55 * )
|
Chris@0
|
56 */
|
Chris@0
|
57 class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, EntityWithPluginCollectionInterface {
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * The name of the image style.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @var string
|
Chris@0
|
63 */
|
Chris@0
|
64 protected $name;
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * The image style label.
|
Chris@0
|
68 *
|
Chris@0
|
69 * @var string
|
Chris@0
|
70 */
|
Chris@0
|
71 protected $label;
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * The array of image effects for this image style.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @var array
|
Chris@0
|
77 */
|
Chris@0
|
78 protected $effects = [];
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Holds the collection of image effects that are used by this image style.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @var \Drupal\image\ImageEffectPluginCollection
|
Chris@0
|
84 */
|
Chris@0
|
85 protected $effectsCollection;
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * {@inheritdoc}
|
Chris@0
|
89 */
|
Chris@0
|
90 public function id() {
|
Chris@0
|
91 return $this->name;
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * {@inheritdoc}
|
Chris@0
|
96 */
|
Chris@0
|
97 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@0
|
98 parent::postSave($storage, $update);
|
Chris@0
|
99
|
Chris@0
|
100 if ($update) {
|
Chris@0
|
101 if (!empty($this->original) && $this->id() !== $this->original->id()) {
|
Chris@0
|
102 // The old image style name needs flushing after a rename.
|
Chris@0
|
103 $this->original->flush();
|
Chris@0
|
104 // Update field settings if necessary.
|
Chris@0
|
105 if (!$this->isSyncing()) {
|
Chris@0
|
106 static::replaceImageStyle($this);
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109 else {
|
Chris@0
|
110 // Flush image style when updating without changing the name.
|
Chris@0
|
111 $this->flush();
|
Chris@0
|
112 }
|
Chris@0
|
113 }
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * {@inheritdoc}
|
Chris@0
|
118 */
|
Chris@0
|
119 public static function postDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@0
|
120 parent::postDelete($storage, $entities);
|
Chris@0
|
121
|
Chris@0
|
122 /** @var \Drupal\image\ImageStyleInterface[] $entities */
|
Chris@0
|
123 foreach ($entities as $style) {
|
Chris@0
|
124 // Flush cached media for the deleted style.
|
Chris@0
|
125 $style->flush();
|
Chris@0
|
126 // Clear the replacement ID, if one has been previously stored.
|
Chris@0
|
127 /** @var \Drupal\image\ImageStyleStorageInterface $storage */
|
Chris@0
|
128 $storage->clearReplacementId($style->id());
|
Chris@0
|
129 }
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * Update field settings if the image style name is changed.
|
Chris@0
|
134 *
|
Chris@0
|
135 * @param \Drupal\image\ImageStyleInterface $style
|
Chris@0
|
136 * The image style.
|
Chris@0
|
137 */
|
Chris@0
|
138 protected static function replaceImageStyle(ImageStyleInterface $style) {
|
Chris@0
|
139 if ($style->id() != $style->getOriginalId()) {
|
Chris@0
|
140 // Loop through all entity displays looking for formatters / widgets using
|
Chris@0
|
141 // the image style.
|
Chris@0
|
142 foreach (EntityViewDisplay::loadMultiple() as $display) {
|
Chris@0
|
143 foreach ($display->getComponents() as $name => $options) {
|
Chris@0
|
144 if (isset($options['type']) && $options['type'] == 'image' && $options['settings']['image_style'] == $style->getOriginalId()) {
|
Chris@0
|
145 $options['settings']['image_style'] = $style->id();
|
Chris@0
|
146 $display->setComponent($name, $options)
|
Chris@0
|
147 ->save();
|
Chris@0
|
148 }
|
Chris@0
|
149 }
|
Chris@0
|
150 }
|
Chris@0
|
151 foreach (EntityViewDisplay::loadMultiple() as $display) {
|
Chris@0
|
152 foreach ($display->getComponents() as $name => $options) {
|
Chris@0
|
153 if (isset($options['type']) && $options['type'] == 'image_image' && $options['settings']['preview_image_style'] == $style->getOriginalId()) {
|
Chris@0
|
154 $options['settings']['preview_image_style'] = $style->id();
|
Chris@0
|
155 $display->setComponent($name, $options)
|
Chris@0
|
156 ->save();
|
Chris@0
|
157 }
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 /**
|
Chris@0
|
164 * {@inheritdoc}
|
Chris@0
|
165 */
|
Chris@0
|
166 public function buildUri($uri) {
|
Chris@0
|
167 $source_scheme = $scheme = $this->fileUriScheme($uri);
|
Chris@0
|
168 $default_scheme = $this->fileDefaultScheme();
|
Chris@0
|
169
|
Chris@0
|
170 if ($source_scheme) {
|
Chris@0
|
171 $path = $this->fileUriTarget($uri);
|
Chris@0
|
172 // The scheme of derivative image files only needs to be computed for
|
Chris@0
|
173 // source files not stored in the default scheme.
|
Chris@0
|
174 if ($source_scheme != $default_scheme) {
|
Chris@0
|
175 $class = $this->getStreamWrapperManager()->getClass($source_scheme);
|
Chris@0
|
176 $is_writable = $class::getType() & StreamWrapperInterface::WRITE;
|
Chris@0
|
177
|
Chris@0
|
178 // Compute the derivative URI scheme. Derivatives created from writable
|
Chris@0
|
179 // source stream wrappers will inherit the scheme. Derivatives created
|
Chris@0
|
180 // from read-only stream wrappers will fall-back to the default scheme.
|
Chris@0
|
181 $scheme = $is_writable ? $source_scheme : $default_scheme;
|
Chris@0
|
182 }
|
Chris@0
|
183 }
|
Chris@0
|
184 else {
|
Chris@0
|
185 $path = $uri;
|
Chris@0
|
186 $source_scheme = $scheme = $default_scheme;
|
Chris@0
|
187 }
|
Chris@0
|
188 return "$scheme://styles/{$this->id()}/$source_scheme/{$this->addExtension($path)}";
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * {@inheritdoc}
|
Chris@0
|
193 */
|
Chris@0
|
194 public function buildUrl($path, $clean_urls = NULL) {
|
Chris@0
|
195 $uri = $this->buildUri($path);
|
Chris@0
|
196 // The token query is added even if the
|
Chris@0
|
197 // 'image.settings:allow_insecure_derivatives' configuration is TRUE, so
|
Chris@0
|
198 // that the emitted links remain valid if it is changed back to the default
|
Chris@0
|
199 // FALSE. However, sites which need to prevent the token query from being
|
Chris@0
|
200 // emitted at all can additionally set the
|
Chris@0
|
201 // 'image.settings:suppress_itok_output' configuration to TRUE to achieve
|
Chris@0
|
202 // that (if both are set, the security token will neither be emitted in the
|
Chris@0
|
203 // image derivative URL nor checked for in
|
Chris@0
|
204 // \Drupal\image\ImageStyleInterface::deliver()).
|
Chris@0
|
205 $token_query = [];
|
Chris@0
|
206 if (!\Drupal::config('image.settings')->get('suppress_itok_output')) {
|
Chris@0
|
207 // The passed $path variable can be either a relative path or a full URI.
|
Chris@0
|
208 $original_uri = file_uri_scheme($path) ? file_stream_wrapper_uri_normalize($path) : file_build_uri($path);
|
Chris@0
|
209 $token_query = [IMAGE_DERIVATIVE_TOKEN => $this->getPathToken($original_uri)];
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 if ($clean_urls === NULL) {
|
Chris@0
|
213 // Assume clean URLs unless the request tells us otherwise.
|
Chris@0
|
214 $clean_urls = TRUE;
|
Chris@0
|
215 try {
|
Chris@0
|
216 $request = \Drupal::request();
|
Chris@0
|
217 $clean_urls = RequestHelper::isCleanUrl($request);
|
Chris@0
|
218 }
|
Chris@0
|
219 catch (ServiceNotFoundException $e) {
|
Chris@0
|
220 }
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 // If not using clean URLs, the image derivative callback is only available
|
Chris@0
|
224 // with the script path. If the file does not exist, use Url::fromUri() to
|
Chris@0
|
225 // ensure that it is included. Once the file exists it's fine to fall back
|
Chris@0
|
226 // to the actual file path, this avoids bootstrapping PHP once the files are
|
Chris@0
|
227 // built.
|
Chris@0
|
228 if ($clean_urls === FALSE && file_uri_scheme($uri) == 'public' && !file_exists($uri)) {
|
Chris@0
|
229 $directory_path = $this->getStreamWrapperManager()->getViaUri($uri)->getDirectoryPath();
|
Chris@0
|
230 return Url::fromUri('base:' . $directory_path . '/' . file_uri_target($uri), ['absolute' => TRUE, 'query' => $token_query])->toString();
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 $file_url = file_create_url($uri);
|
Chris@0
|
234 // Append the query string with the token, if necessary.
|
Chris@0
|
235 if ($token_query) {
|
Chris@0
|
236 $file_url .= (strpos($file_url, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($token_query);
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 return $file_url;
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 /**
|
Chris@0
|
243 * {@inheritdoc}
|
Chris@0
|
244 */
|
Chris@0
|
245 public function flush($path = NULL) {
|
Chris@0
|
246 // A specific image path has been provided. Flush only that derivative.
|
Chris@0
|
247 if (isset($path)) {
|
Chris@0
|
248 $derivative_uri = $this->buildUri($path);
|
Chris@0
|
249 if (file_exists($derivative_uri)) {
|
Chris@0
|
250 file_unmanaged_delete($derivative_uri);
|
Chris@0
|
251 }
|
Chris@0
|
252 return $this;
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@0
|
255 // Delete the style directory in each registered wrapper.
|
Chris@0
|
256 $wrappers = $this->getStreamWrapperManager()->getWrappers(StreamWrapperInterface::WRITE_VISIBLE);
|
Chris@0
|
257 foreach ($wrappers as $wrapper => $wrapper_data) {
|
Chris@0
|
258 if (file_exists($directory = $wrapper . '://styles/' . $this->id())) {
|
Chris@0
|
259 file_unmanaged_delete_recursive($directory);
|
Chris@0
|
260 }
|
Chris@0
|
261 }
|
Chris@0
|
262
|
Chris@0
|
263 // Let other modules update as necessary on flush.
|
Chris@0
|
264 $module_handler = \Drupal::moduleHandler();
|
Chris@0
|
265 $module_handler->invokeAll('image_style_flush', [$this]);
|
Chris@0
|
266
|
Chris@0
|
267 // Clear caches so that formatters may be added for this style.
|
Chris@0
|
268 drupal_theme_rebuild();
|
Chris@0
|
269
|
Chris@0
|
270 Cache::invalidateTags($this->getCacheTagsToInvalidate());
|
Chris@0
|
271
|
Chris@0
|
272 return $this;
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 /**
|
Chris@0
|
276 * {@inheritdoc}
|
Chris@0
|
277 */
|
Chris@0
|
278 public function createDerivative($original_uri, $derivative_uri) {
|
Chris@0
|
279 // If the source file doesn't exist, return FALSE without creating folders.
|
Chris@0
|
280 $image = $this->getImageFactory()->get($original_uri);
|
Chris@0
|
281 if (!$image->isValid()) {
|
Chris@0
|
282 return FALSE;
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 // Get the folder for the final location of this style.
|
Chris@0
|
286 $directory = drupal_dirname($derivative_uri);
|
Chris@0
|
287
|
Chris@0
|
288 // Build the destination folder tree if it doesn't already exist.
|
Chris@0
|
289 if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
|
Chris@0
|
290 \Drupal::logger('image')->error('Failed to create style directory: %directory', ['%directory' => $directory]);
|
Chris@0
|
291 return FALSE;
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 foreach ($this->getEffects() as $effect) {
|
Chris@0
|
295 $effect->applyEffect($image);
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 if (!$image->save($derivative_uri)) {
|
Chris@0
|
299 if (file_exists($derivative_uri)) {
|
Chris@0
|
300 \Drupal::logger('image')->error('Cached image file %destination already exists. There may be an issue with your rewrite configuration.', ['%destination' => $derivative_uri]);
|
Chris@0
|
301 }
|
Chris@0
|
302 return FALSE;
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 return TRUE;
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 /**
|
Chris@0
|
309 * {@inheritdoc}
|
Chris@0
|
310 */
|
Chris@0
|
311 public function transformDimensions(array &$dimensions, $uri) {
|
Chris@0
|
312 foreach ($this->getEffects() as $effect) {
|
Chris@0
|
313 $effect->transformDimensions($dimensions, $uri);
|
Chris@0
|
314 }
|
Chris@0
|
315 }
|
Chris@0
|
316
|
Chris@0
|
317 /**
|
Chris@0
|
318 * {@inheritdoc}
|
Chris@0
|
319 */
|
Chris@0
|
320 public function getDerivativeExtension($extension) {
|
Chris@0
|
321 foreach ($this->getEffects() as $effect) {
|
Chris@0
|
322 $extension = $effect->getDerivativeExtension($extension);
|
Chris@0
|
323 }
|
Chris@0
|
324 return $extension;
|
Chris@0
|
325 }
|
Chris@0
|
326
|
Chris@0
|
327 /**
|
Chris@0
|
328 * {@inheritdoc}
|
Chris@0
|
329 */
|
Chris@0
|
330 public function getPathToken($uri) {
|
Chris@0
|
331 // Return the first 8 characters.
|
Chris@0
|
332 return substr(Crypt::hmacBase64($this->id() . ':' . $this->addExtension($uri), $this->getPrivateKey() . $this->getHashSalt()), 0, 8);
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@0
|
335 /**
|
Chris@0
|
336 * {@inheritdoc}
|
Chris@0
|
337 */
|
Chris@0
|
338 public function deleteImageEffect(ImageEffectInterface $effect) {
|
Chris@0
|
339 $this->getEffects()->removeInstanceId($effect->getUuid());
|
Chris@0
|
340 $this->save();
|
Chris@0
|
341 return $this;
|
Chris@0
|
342 }
|
Chris@0
|
343
|
Chris@0
|
344 /**
|
Chris@0
|
345 * {@inheritdoc}
|
Chris@0
|
346 */
|
Chris@0
|
347 public function supportsUri($uri) {
|
Chris@0
|
348 // Only support the URI if its extension is supported by the current image
|
Chris@0
|
349 // toolkit.
|
Chris@0
|
350 return in_array(
|
Chris@0
|
351 Unicode::strtolower(pathinfo($uri, PATHINFO_EXTENSION)),
|
Chris@0
|
352 $this->getImageFactory()->getSupportedExtensions()
|
Chris@0
|
353 );
|
Chris@0
|
354 }
|
Chris@0
|
355
|
Chris@0
|
356 /**
|
Chris@0
|
357 * {@inheritdoc}
|
Chris@0
|
358 */
|
Chris@0
|
359 public function getEffect($effect) {
|
Chris@0
|
360 return $this->getEffects()->get($effect);
|
Chris@0
|
361 }
|
Chris@0
|
362
|
Chris@0
|
363 /**
|
Chris@0
|
364 * {@inheritdoc}
|
Chris@0
|
365 */
|
Chris@0
|
366 public function getEffects() {
|
Chris@0
|
367 if (!$this->effectsCollection) {
|
Chris@0
|
368 $this->effectsCollection = new ImageEffectPluginCollection($this->getImageEffectPluginManager(), $this->effects);
|
Chris@0
|
369 $this->effectsCollection->sort();
|
Chris@0
|
370 }
|
Chris@0
|
371 return $this->effectsCollection;
|
Chris@0
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 /**
|
Chris@0
|
375 * {@inheritdoc}
|
Chris@0
|
376 */
|
Chris@0
|
377 public function getPluginCollections() {
|
Chris@0
|
378 return ['effects' => $this->getEffects()];
|
Chris@0
|
379 }
|
Chris@0
|
380
|
Chris@0
|
381 /**
|
Chris@0
|
382 * {@inheritdoc}
|
Chris@0
|
383 */
|
Chris@0
|
384 public function addImageEffect(array $configuration) {
|
Chris@0
|
385 $configuration['uuid'] = $this->uuidGenerator()->generate();
|
Chris@0
|
386 $this->getEffects()->addInstanceId($configuration['uuid'], $configuration);
|
Chris@0
|
387 return $configuration['uuid'];
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 /**
|
Chris@0
|
391 * {@inheritdoc}
|
Chris@0
|
392 */
|
Chris@0
|
393 public function getReplacementID() {
|
Chris@0
|
394 /** @var \Drupal\image\ImageStyleStorageInterface $storage */
|
Chris@0
|
395 $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId());
|
Chris@0
|
396 return $storage->getReplacementId($this->id());
|
Chris@0
|
397 }
|
Chris@0
|
398
|
Chris@0
|
399 /**
|
Chris@0
|
400 * {@inheritdoc}
|
Chris@0
|
401 */
|
Chris@0
|
402 public function getName() {
|
Chris@0
|
403 return $this->get('name');
|
Chris@0
|
404 }
|
Chris@0
|
405
|
Chris@0
|
406 /**
|
Chris@0
|
407 * {@inheritdoc}
|
Chris@0
|
408 */
|
Chris@0
|
409 public function setName($name) {
|
Chris@0
|
410 $this->set('name', $name);
|
Chris@0
|
411 return $this;
|
Chris@0
|
412 }
|
Chris@0
|
413
|
Chris@0
|
414 /**
|
Chris@0
|
415 * Returns the image effect plugin manager.
|
Chris@0
|
416 *
|
Chris@0
|
417 * @return \Drupal\Component\Plugin\PluginManagerInterface
|
Chris@0
|
418 * The image effect plugin manager.
|
Chris@0
|
419 */
|
Chris@0
|
420 protected function getImageEffectPluginManager() {
|
Chris@0
|
421 return \Drupal::service('plugin.manager.image.effect');
|
Chris@0
|
422 }
|
Chris@0
|
423
|
Chris@0
|
424 /**
|
Chris@0
|
425 * Returns the image factory.
|
Chris@0
|
426 *
|
Chris@0
|
427 * @return \Drupal\Core\Image\ImageFactory
|
Chris@0
|
428 * The image factory.
|
Chris@0
|
429 */
|
Chris@0
|
430 protected function getImageFactory() {
|
Chris@0
|
431 return \Drupal::service('image.factory');
|
Chris@0
|
432 }
|
Chris@0
|
433
|
Chris@0
|
434 /**
|
Chris@0
|
435 * Gets the Drupal private key.
|
Chris@0
|
436 *
|
Chris@0
|
437 * @return string
|
Chris@0
|
438 * The Drupal private key.
|
Chris@0
|
439 */
|
Chris@0
|
440 protected function getPrivateKey() {
|
Chris@0
|
441 return \Drupal::service('private_key')->get();
|
Chris@0
|
442 }
|
Chris@0
|
443
|
Chris@0
|
444 /**
|
Chris@0
|
445 * Gets a salt useful for hardening against SQL injection.
|
Chris@0
|
446 *
|
Chris@0
|
447 * @return string
|
Chris@0
|
448 * A salt based on information in settings.php, not in the database.
|
Chris@0
|
449 *
|
Chris@0
|
450 * @throws \RuntimeException
|
Chris@0
|
451 */
|
Chris@0
|
452 protected function getHashSalt() {
|
Chris@0
|
453 return Settings::getHashSalt();
|
Chris@0
|
454 }
|
Chris@0
|
455
|
Chris@0
|
456 /**
|
Chris@0
|
457 * Adds an extension to a path.
|
Chris@0
|
458 *
|
Chris@0
|
459 * If this image style changes the extension of the derivative, this method
|
Chris@0
|
460 * adds the new extension to the given path. This way we avoid filename
|
Chris@0
|
461 * clashes while still allowing us to find the source image.
|
Chris@0
|
462 *
|
Chris@0
|
463 * @param string $path
|
Chris@0
|
464 * The path to add the extension to.
|
Chris@0
|
465 *
|
Chris@0
|
466 * @return string
|
Chris@0
|
467 * The given path if this image style doesn't change its extension, or the
|
Chris@0
|
468 * path with the added extension if it does.
|
Chris@0
|
469 */
|
Chris@0
|
470 protected function addExtension($path) {
|
Chris@0
|
471 $original_extension = pathinfo($path, PATHINFO_EXTENSION);
|
Chris@0
|
472 $extension = $this->getDerivativeExtension($original_extension);
|
Chris@0
|
473 if ($original_extension !== $extension) {
|
Chris@0
|
474 $path .= '.' . $extension;
|
Chris@0
|
475 }
|
Chris@0
|
476 return $path;
|
Chris@0
|
477 }
|
Chris@0
|
478
|
Chris@0
|
479 /**
|
Chris@0
|
480 * Provides a wrapper for file_uri_scheme() to allow unit testing.
|
Chris@0
|
481 *
|
Chris@0
|
482 * Returns the scheme of a URI (e.g. a stream).
|
Chris@0
|
483 *
|
Chris@0
|
484 * @param string $uri
|
Chris@0
|
485 * A stream, referenced as "scheme://target" or "data:target".
|
Chris@0
|
486 *
|
Chris@0
|
487 * @see file_uri_target()
|
Chris@0
|
488 *
|
Chris@0
|
489 * @todo: Remove when https://www.drupal.org/node/2050759 is in.
|
Chris@0
|
490 *
|
Chris@0
|
491 * @return string
|
Chris@0
|
492 * A string containing the name of the scheme, or FALSE if none. For
|
Chris@0
|
493 * example, the URI "public://example.txt" would return "public".
|
Chris@0
|
494 */
|
Chris@0
|
495 protected function fileUriScheme($uri) {
|
Chris@0
|
496 return file_uri_scheme($uri);
|
Chris@0
|
497 }
|
Chris@0
|
498
|
Chris@0
|
499 /**
|
Chris@0
|
500 * Provides a wrapper for file_uri_target() to allow unit testing.
|
Chris@0
|
501 *
|
Chris@0
|
502 * Returns the part of a URI after the schema.
|
Chris@0
|
503 *
|
Chris@0
|
504 * @param string $uri
|
Chris@0
|
505 * A stream, referenced as "scheme://target" or "data:target".
|
Chris@0
|
506 *
|
Chris@0
|
507 * @see file_uri_scheme()
|
Chris@0
|
508 *
|
Chris@0
|
509 * @todo: Convert file_uri_target() into a proper injectable service.
|
Chris@0
|
510 *
|
Chris@0
|
511 * @return string|bool
|
Chris@0
|
512 * A string containing the target (path), or FALSE if none.
|
Chris@0
|
513 * For example, the URI "public://sample/test.txt" would return
|
Chris@0
|
514 * "sample/test.txt".
|
Chris@0
|
515 */
|
Chris@0
|
516 protected function fileUriTarget($uri) {
|
Chris@0
|
517 return file_uri_target($uri);
|
Chris@0
|
518 }
|
Chris@0
|
519
|
Chris@0
|
520 /**
|
Chris@0
|
521 * Provides a wrapper for file_default_scheme() to allow unit testing.
|
Chris@0
|
522 *
|
Chris@0
|
523 * Gets the default file stream implementation.
|
Chris@0
|
524 *
|
Chris@0
|
525 * @todo: Convert file_default_scheme() into a proper injectable service.
|
Chris@0
|
526 *
|
Chris@0
|
527 * @return string
|
Chris@0
|
528 * 'public', 'private' or any other file scheme defined as the default.
|
Chris@0
|
529 */
|
Chris@0
|
530 protected function fileDefaultScheme() {
|
Chris@0
|
531 return file_default_scheme();
|
Chris@0
|
532 }
|
Chris@0
|
533
|
Chris@0
|
534 /**
|
Chris@0
|
535 * Gets the stream wrapper manager service.
|
Chris@0
|
536 *
|
Chris@0
|
537 * @return \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
|
Chris@0
|
538 * The stream wrapper manager service
|
Chris@0
|
539 *
|
Chris@0
|
540 * @todo Properly inject this service in Drupal 9.0.x.
|
Chris@0
|
541 */
|
Chris@0
|
542 protected function getStreamWrapperManager() {
|
Chris@0
|
543 return \Drupal::service('stream_wrapper_manager');
|
Chris@0
|
544 }
|
Chris@0
|
545
|
Chris@0
|
546 }
|