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
51 * Constructs an ImageToolkitBase object.
53 * @param array $configuration
54 * A configuration array containing information about the plugin instance.
55 * @param string $plugin_id
56 * The plugin_id for the plugin instance.
57 * @param array $plugin_definition
58 * The plugin implementation definition.
59 * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
60 * The toolkit operation manager.
61 * @param \Psr\Log\LoggerInterface $logger
63 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
66 public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
67 parent::__construct($configuration, $plugin_id, $plugin_definition);
68 $this->operationManager = $operation_manager;
69 $this->logger = $logger;
70 $this->configFactory = $config_factory;
76 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
81 public function setSource($source) {
82 // If a previous image has been loaded, there is no way to know if the
83 // toolkit implementation needs to perform any additional actions like
84 // freeing memory. Therefore, the source image cannot be changed once set.
86 throw new \BadMethodCallException(__METHOD__ . '() may only be called once');
88 $this->source = $source;
95 public function getSource() {
102 public function getRequirements() {
107 * Gets a toolkit operation plugin instance.
109 * @param string $operation
110 * The toolkit operation requested.
112 * @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
113 * An instance of the requested toolkit operation plugin.
115 protected function getToolkitOperation($operation) {
116 return $this->operationManager->getToolkitOperation($this, $operation);
122 public function apply($operation, array $arguments = []) {
124 // Get the plugin to use for the operation and apply the operation.
125 return $this->getToolkitOperation($operation)->apply($arguments);
127 catch (PluginNotFoundException $e) {
128 $this->logger->error("The selected image handling toolkit '@toolkit' can not process operation '@operation'.", ['@toolkit' => $this->getPluginId(), '@operation' => $operation]);
131 catch (\InvalidArgumentException $e) {
132 $this->logger->warning($e->getMessage(), []);