ce0e283c8efcd6d6c0a0555ddd9ac8a639a68e8e
[yaffs-website] / ImageToolkitBase.php
1 <?php
2
3 namespace Drupal\Core\ImageToolkit;
4
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;
10
11 /**
12  * Provides a base class for image toolkit plugins.
13  *
14  * @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkit
15  * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
16  * @see \Drupal\Core\ImageToolkit\ImageToolkitManager
17  * @see plugin_api
18  */
19 abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface {
20
21   /**
22    * The config factory.
23    *
24    * @var \Drupal\Core\Config\ConfigFactoryInterface
25    */
26   protected $configFactory;
27
28   /**
29    * Path of the image file.
30    *
31    * @var string
32    */
33   protected $source = '';
34
35   /**
36    * The image toolkit operation manager.
37    *
38    * @var \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface
39    */
40   protected $operationManager;
41
42   /**
43    * A logger instance.
44    *
45    * @var \Psr\Log\LoggerInterface
46    */
47   protected $logger;
48
49   /**
50    * Constructs an ImageToolkitBase object.
51    *
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
61    *   A logger instance.
62    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
63    *   The config factory.
64    */
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;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
76
77   /**
78    * {@inheritdoc}
79    */
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.
84     if ($this->source) {
85       throw new \BadMethodCallException(__METHOD__ . '() may only be called once');
86     }
87     $this->source = $source;
88     return $this;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getSource() {
95     return $this->source;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function getRequirements() {
102     return [];
103   }
104
105   /**
106    * Gets a toolkit operation plugin instance.
107    *
108    * @param string $operation
109    *   The toolkit operation requested.
110    *
111    * @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
112    *   An instance of the requested toolkit operation plugin.
113    */
114   protected function getToolkitOperation($operation) {
115     return $this->operationManager->getToolkitOperation($this, $operation);
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function apply($operation, array $arguments = []) {
122     try {
123       // Get the plugin to use for the operation and apply the operation.
124       return $this->getToolkitOperation($operation)->apply($arguments);
125     }
126     catch (PluginNotFoundException $e) {
127       $this->logger->error("The selected image handling toolkit '@toolkit' can not process operation '@operation'.", ['@toolkit' => $this->getPluginId(), '@operation' => $operation]);
128       return FALSE;
129     }
130     catch (\InvalidArgumentException $e) {
131       $this->logger->warning($e->getMessage(), []);
132       return FALSE;
133     }
134   }
135
136 }