3 namespace Drupal\Core\ImageToolkit;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Plugin\PluginBase;
9 use Psr\Log\LoggerInterface;
12 * Provides a base class for image toolkit plugins.
14 * @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkit
15 * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
16 * @see \Drupal\Core\ImageToolkit\ImageToolkitManager
19 abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface {
24 * @var \Drupal\Core\Config\ConfigFactoryInterface
26 protected $configFactory;
29 * Path of the image file.
33 protected $source = '';
36 * The image toolkit operation manager.
38 * @var \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface
40 protected $operationManager;
45 * @var \Psr\Log\LoggerInterface
50 * Constructs an ImageToolkitBase object.
52 * @param array $configuration
53 * A configuration array containing information about the plugin instance.
54 * @param string $plugin_id
55 * The plugin_id for the plugin instance.
56 * @param array $plugin_definition
57 * The plugin implementation definition.
58 * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
59 * The toolkit operation manager.
60 * @param \Psr\Log\LoggerInterface $logger
62 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
65 public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
66 parent::__construct($configuration, $plugin_id, $plugin_definition);
67 $this->operationManager = $operation_manager;
68 $this->logger = $logger;
69 $this->configFactory = $config_factory;
75 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
80 public function setSource($source) {
81 // If a previous image has been loaded, there is no way to know if the
82 // toolkit implementation needs to perform any additional actions like
83 // freeing memory. Therefore, the source image cannot be changed once set.
85 throw new \BadMethodCallException(__METHOD__ . '() may only be called once');
87 $this->source = $source;
94 public function getSource() {
101 public function getRequirements() {
106 * Gets a toolkit operation plugin instance.
108 * @param string $operation
109 * The toolkit operation requested.
111 * @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
112 * An instance of the requested toolkit operation plugin.
114 protected function getToolkitOperation($operation) {
115 return $this->operationManager->getToolkitOperation($this, $operation);
121 public function apply($operation, array $arguments = []) {
123 // Get the plugin to use for the operation and apply the operation.
124 return $this->getToolkitOperation($operation)->apply($arguments);
126 catch (PluginNotFoundException $e) {
127 $this->logger->error("The selected image handling toolkit '@toolkit' can not process operation '@operation'.", ['@toolkit' => $this->getPluginId(), '@operation' => $operation]);
130 catch (\InvalidArgumentException $e) {
131 $this->logger->warning($e->getMessage(), []);