FlushSingleImage.php in Flush Single Image Styles 8
File
src/FlushSingleImage.php
View source
<?php
namespace Drupal\flush_single_image;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
class FlushSingleImage implements FlushSingleImageInterface {
protected $entityTypeManager;
protected $fileSystem;
public function __construct(EntityTypeManagerInterface $entity_type_manager, FileSystemInterface $file_system) {
$this->entityTypeManager = $entity_type_manager;
$this->fileSystem = $file_system;
}
public function flush($path) {
$style_paths = $this
->getStylePaths($path);
foreach ($style_paths as $style_path) {
$this->fileSystem
->unlink($style_path);
}
return $style_paths;
}
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;
}
protected function buildUri($path) {
if (!$this->fileSystem
->uriScheme($path)) {
$path = file_default_scheme() . '://' . preg_replace('/^\\//', '', $path);
}
return $path;
}
}