Commit 19a98c92 authored by Steven Jones's avatar Steven Jones
Browse files

Re-work the decorator.

parent 941c9ffe
......@@ -165,35 +165,14 @@ class ImageAPIOptimizePipeline extends ConfigEntityBase implements ImageAPIOptim
if (!$image->isValid()) {
return FALSE;
}
/* @var \Drupal\Core\Image\ImageInterface $image */
if (count($this->getProcessors())) {
/*
Copy image to optimiza to a temp location so that:
1. It's always a local image.
2. The filename is only ascii characters.
3. Multiple pipelines can be applied to the same image at the same time.
*/
$temp_image_uri = \Drupal::service('file_system')->tempnam('temporary://', 'image_api_optimize_' . $this->id());
file_unmanaged_copy($image_uri, $temp_image_uri);
// @TODO: Register this file for deletion in a shutdown function.
$image_changed = FALSE;
foreach ($this->getProcessors() as $processor) {
$changed = $processor->applyToImage($temp_image_uri);
$image_changed = $image_changed || $changed;
// The file may have changed on disk after each processor has been
// applied, and PHP has a cache of file size information etc. so clear
// it here so that later calls to filesize() etc. get the correct
// information.
clearstatcache();
}
if ($image_changed) {
// Copy the temporary file back over the original image.
file_unmanaged_copy($temp_image_uri, $image_uri, FILE_EXISTS_REPLACE);
}
foreach ($this->getProcessors() as $processor) {
$processor->applyToImage($image_uri);
// The file may have changed on disk after each processor has been
// applied, and PHP has a cache of file size information etc. so clear
// it here so that later calls to filesize() etc. get the correct
// information.
clearstatcache();
}
}
......
......@@ -15,9 +15,18 @@ use Drupal\entity_decorator\Entity\EntityDecoratorInterface;
final class ImageStyleWithPipeline extends ImageStyleDecoratorBase implements EntityDecoratorInterface {
/**
* @TODO: maybe this should be something other than a 'random' string.
* @inheritDoc
*/
protected $pipeline = '__default__';
public function __construct(\Drupal\Core\Entity\EntityInterface $subject) {
parent::__construct($subject);
if (!isset($this->subject->pipeline)) {
/**
* @TODO: maybe this should be something other than a 'random' string.
*/
$this->subject->pipeline = '__default__';
}
}
/**
* {@inheritdoc}
......@@ -40,22 +49,22 @@ final class ImageStyleWithPipeline extends ImageStyleDecoratorBase implements En
*
*/
public function getPipeline() {
return $this->pipeline;
return $this->subject->pipeline;
}
/**
* @return \Drupal\imageapi_optimize\Entity\ImageAPIOptimizePipeline|null
*/
public function getPipelineEntity() {
if (!empty($this->pipeline)) {
if (!empty($this->subject->pipeline)) {
$storage = $this->entityTypeManager()->getStorage('imageapi_optimize_pipeline');
if ($this->pipeline == '__default__') {
if ($this->subject->pipeline == '__default__') {
if ($default_pipeline_name = \Drupal::config('imageapi_optimize.settings')->get('default_pipeline')) {
$pipelineId = $default_pipeline_name;
}
}
else {
$pipelineId = $this->pipeline;
$pipelineId = $this->subject->pipeline;
}
if (!empty($pipelineId) && ($pipeline = $storage->load($pipelineId))) {
return $pipeline;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment