Skip to content
Snippets Groups Projects
Commit 4c54dce3 authored by Steven Jones's avatar Steven Jones Committed by Steven Jones
Browse files

Issue #2990670 by Steven Jones: Enable optimizations from separate modules

parent 3b643e98
Branches 8.x-1.x
No related merge requests found
......@@ -30,3 +30,48 @@ function imageapi_optimize_update_8001() {
->save(TRUE);
}
}
/**
* Re-enable sub modules.
*/
function imageapi_optimize_update_8002() {
$modules = [];
$pipelines = \Drupal\imageapi_optimize\Entity\ImageAPIOptimizePipeline::loadMultiple();
/* @var \Drupal\imageapi_optimize\Entity\ImageAPIOptimizePipeline $pipeline */
foreach ($pipelines as $pipeline) {
$processors = $pipeline->getProcessorsCollection();
foreach ($processors->getInstanceIds() as $id) {
try {
$processors->get($id);
} catch (\Drupal\imageapi_optimize\Exception\PluginNotFoundException $e) {
switch ($e->getPluginId()) {
case 'advdef':
case 'advpng':
case 'jfifremove':
case 'jpegoptim':
case 'jpegtran':
case 'optipng':
case 'pngcrush':
case 'pngout':
case 'pngquant':
$modules[] = 'imageapi_optimize_binaries';
break;
case 'resmushit':
$modules[] = 'imageapi_optimize_resmushit';
break;
case 'tinypng':
$modules[] = 'imageapi_optimize_tinypng';
break;
}
}
}
}
// Now we might have a list of modules to enable.
if (!empty($modules)) {
\Drupal::service('module_installer')->install(array_unique($modules));
}
}
......@@ -229,12 +229,19 @@ class ImageAPIOptimizePipeline extends ConfigEntityBase implements ImageAPIOptim
*/
public function getProcessors() {
if (!$this->processorsCollection) {
$this->processorsCollection = new ImageAPIOptimizeProcessorPluginCollection($this->getImageAPIOptimizeProcessorPluginManager(), $this->processors);
$this->processorsCollection = $this->getProcessorsCollection();
$this->processorsCollection->sort();
}
return $this->processorsCollection;
}
/**
* {@inheritdoc}
*/
public function getProcessorsCollection() {
return new ImageAPIOptimizeProcessorPluginCollection($this->getImageAPIOptimizeProcessorPluginManager(), $this->processors);
}
/**
* {@inheritdoc}
*/
......
<?php
namespace Drupal\imageapi_optimize\Exception;
use Drupal\Component\Plugin\Exception\PluginNotFoundException as CorePluginNotFoundException;
/**
* Plugin not found exception so we can retrieve the plugin ID
*
* @TOOD: Get this change into Drupal core.
*
* @see \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
class PluginNotFoundException extends CorePluginNotFoundException {
protected $pluginId;
/**
* @inheritDoc
*/
public function __construct($plugin_id, $message = '', $code = 0, \Exception $previous = NULL) {
parent::__construct($plugin_id, $message, $code, $previous);
$this->pluginId = $plugin_id;
}
/**
* @return mixed
*/
public function getPluginId() {
return $this->pluginId;
}
}
\ No newline at end of file
......@@ -56,11 +56,21 @@ interface ImageAPIOptimizePipelineInterface extends ConfigEntityInterface {
/**
* Returns the image optimize processors for this pipeline.
*
* The processors should be sorted, and will have been instantiated.
*
* @return \Drupal\imageapi_optimize\ImageAPIOptimizeProcessorPluginCollection|\Drupal\imageapi_optimize\ImageAPIOptimizeProcessorInterface[]
* The image optimize processor plugin collection.
*/
public function getProcessors();
/**
* Returns an image optimize processors collection.
*
* @return \Drupal\imageapi_optimize\ImageAPIOptimizeProcessorPluginCollection|\Drupal\imageapi_optimize\ImageAPIOptimizeProcessorInterface[]
* The image optimize processor plugin collection.
*/
public function getProcessorsCollection();
/**
* Saves an image optimize processor for this pipeline.
*
......
......@@ -5,6 +5,7 @@ namespace Drupal\imageapi_optimize;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\imageapi_optimize\Exception\PluginNotFoundException;
/**
* Manages image optimize processor plugins.
......@@ -37,4 +38,34 @@ class ImageAPIOptimizeProcessorManager extends DefaultPluginManager {
$this->setCacheBackend($cache_backend, 'imageapi_optimize_processor_plugins');
}
/**
* Gets a specific plugin definition.
*
* @param array $definitions
* An array of the available plugin definitions.
* @param string $plugin_id
* A plugin id.
* @param bool $exception_on_invalid
* If TRUE, an invalid plugin ID will cause an exception to be thrown; if
* FALSE, NULL will be returned.
*
* @return array|null
* A plugin definition, or NULL if the plugin ID is invalid and
* $exception_on_invalid is TRUE.
*
* @throws \Drupal\imageapi_optimize\Exception\PluginNotFoundException
* Thrown if $plugin_id is invalid and $exception_on_invalid is TRUE.
*/
protected function doGetDefinition(array $definitions, $plugin_id, $exception_on_invalid) {
// Avoid using a ternary that would create a copy of the array.
if (isset($definitions[$plugin_id])) {
return $definitions[$plugin_id];
}
elseif (!$exception_on_invalid) {
return NULL;
}
throw new PluginNotFoundException($plugin_id, sprintf('The "%s" plugin does not exist.', $plugin_id));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment