You are here

class FlushSingleImage in Flush Single Image Styles 8

Class FlushSingleImage.

Hierarchy

Expanded class hierarchy of FlushSingleImage

1 string reference to 'FlushSingleImage'
flush_single_image.services.yml in ./flush_single_image.services.yml
flush_single_image.services.yml
1 service uses FlushSingleImage
flush_single_image in ./flush_single_image.services.yml
Drupal\flush_single_image\FlushSingleImage

File

src/FlushSingleImage.php, line 11

Namespace

Drupal\flush_single_image
View source
class FlushSingleImage implements FlushSingleImageInterface {

  /**
   * Drupal\Core\Entity\EntityTypeManager definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * Drupal\Core\File\FileSystem definition.
   *
   * @var \Drupal\Core\File\FileSystem
   */
  protected $fileSystem;

  /**
   * Constructs a new FlushSingleImage object.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, FileSystemInterface $file_system) {
    $this->entityTypeManager = $entity_type_manager;
    $this->fileSystem = $file_system;
  }

  /**
   * {inheritdoc}
   */
  public function flush($path) {
    $style_paths = $this
      ->getStylePaths($path);
    foreach ($style_paths as $style_path) {
      $this->fileSystem
        ->unlink($style_path);
    }
    return $style_paths;
  }

  /**
   * {inheritdoc}
   */
  public function getStylePaths($path) {
    $path = $this
      ->buildUri($path);
    $styles = $this->entityTypeManager
      ->getStorage('image_style')
      ->loadMultiple();
    $style_paths = [];
    foreach ($styles as $style) {
      $style_path = $style
        ->buildUri($path);
      if (is_file($style_path) && file_exists($style_path)) {
        $style_paths[] = $style_path;
      }
    }
    return $style_paths;
  }

  /**
   * Build a URI for a given path if it's missing the scheme.
   */
  protected function buildUri($path) {
    if (!$this->fileSystem
      ->uriScheme($path)) {
      $path = file_default_scheme() . '://' . preg_replace('/^\\//', '', $path);
    }
    return $path;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlushSingleImage::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManager definition.
FlushSingleImage::$fileSystem protected property Drupal\Core\File\FileSystem definition.
FlushSingleImage::buildUri protected function Build a URI for a given path if it's missing the scheme.
FlushSingleImage::flush public function {inheritdoc} Overrides FlushSingleImageInterface::flush
FlushSingleImage::getStylePaths public function {inheritdoc} Overrides FlushSingleImageInterface::getStylePaths
FlushSingleImage::__construct public function Constructs a new FlushSingleImage object.