comparison core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 /**
51 * Constructs an ImageToolkitBase object.
52 *
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
62 * A logger instance.
63 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
64 * The config factory.
65 */
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;
71 }
72
73 /**
74 * {@inheritdoc}
75 */
76 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
77
78 /**
79 * {@inheritdoc}
80 */
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.
85 if ($this->source) {
86 throw new \BadMethodCallException(__METHOD__ . '() may only be called once');
87 }
88 $this->source = $source;
89 return $this;
90 }
91
92 /**
93 * {@inheritdoc}
94 */
95 public function getSource() {
96 return $this->source;
97 }
98
99 /**
100 * {@inheritdoc}
101 */
102 public function getRequirements() {
103 return [];
104 }
105
106 /**
107 * Gets a toolkit operation plugin instance.
108 *
109 * @param string $operation
110 * The toolkit operation requested.
111 *
112 * @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
113 * An instance of the requested toolkit operation plugin.
114 */
115 protected function getToolkitOperation($operation) {
116 return $this->operationManager->getToolkitOperation($this, $operation);
117 }
118
119 /**
120 * {@inheritdoc}
121 */
122 public function apply($operation, array $arguments = []) {
123 try {
124 // Get the plugin to use for the operation and apply the operation.
125 return $this->getToolkitOperation($operation)->apply($arguments);
126 }
127 catch (PluginNotFoundException $e) {
128 $this->logger->error("The selected image handling toolkit '@toolkit' can not process operation '@operation'.", ['@toolkit' => $this->getPluginId(), '@operation' => $operation]);
129 return FALSE;
130 }
131 catch (\InvalidArgumentException $e) {
132 $this->logger->warning($e->getMessage(), []);
133 return FALSE;
134 }
135 }
136
137 }