annotate core/modules/image/src/Entity/ImageStyle.php @ 19:fa3358dc1485 tip

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