Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / system / tests / modules / image_test / src / Plugin / ImageToolkit / TestToolkit.php
1 <?php
2
3 namespace Drupal\image_test\Plugin\ImageToolkit;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\ImageToolkit\ImageToolkitBase;
9 use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface;
10 use Drupal\Core\State\StateInterface;
11 use Psr\Log\LoggerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a Test toolkit for image manipulation within Drupal.
16  *
17  * @ImageToolkit(
18  *   id = "test",
19  *   title = @Translation("A dummy toolkit that works")
20  * )
21  */
22 class TestToolkit extends ImageToolkitBase {
23
24   /**
25    * The state service.
26    *
27    * @var \Drupal\Core\State\StateInterface
28    */
29   protected $state;
30
31   /**
32    * Image type represented by a PHP IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG).
33    *
34    * @var int
35    */
36   protected $type;
37
38   /**
39    * The width of the image.
40    *
41    * @var int
42    */
43   protected $width;
44
45   /**
46    * The height of the image.
47    *
48    * @var int
49    */
50   protected $height;
51
52   /**
53    * Constructs a TestToolkit object.
54    *
55    * @param array $configuration
56    *   A configuration array containing information about the plugin instance.
57    * @param string $plugin_id
58    *   The plugin_id for the plugin instance.
59    * @param array $plugin_definition
60    *   The plugin implementation definition.
61    * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
62    *   The toolkit operation manager.
63    * @param \Psr\Log\LoggerInterface $logger
64    *   A logger instance.
65    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
66    *   The config factory.
67    * @param \Drupal\Core\State\StateInterface $state
68    *   The state key value store.
69    */
70   public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory, StateInterface $state) {
71     parent::__construct($configuration, $plugin_id, $plugin_definition, $operation_manager, $logger, $config_factory);
72     $this->state = $state;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
79     return new static(
80       $configuration,
81       $plugin_id,
82       $plugin_definition,
83       $container->get('image.toolkit.operation.manager'),
84       $container->get('logger.channel.image'),
85       $container->get('config.factory'),
86       $container->get('state')
87     );
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
94     $this->logCall('settings', func_get_args());
95     $form['test_parameter'] = [
96       '#type' => 'number',
97       '#title' => $this->t('Test toolkit parameter'),
98       '#description' => $this->t('A toolkit parameter for testing purposes.'),
99       '#min' => 0,
100       '#max' => 100,
101       '#default_value' => $this->configFactory->getEditable('system.image.test_toolkit')->get('test_parameter', FALSE),
102     ];
103     return $form;
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
110     if ($form_state->getValue(['test', 'test_parameter']) == 0) {
111       $form_state->setErrorByName('test][test_parameter', $this->t('Test parameter should be different from 0.'));
112     }
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
119     $this->configFactory->getEditable('system.image.test_toolkit')
120       ->set('test_parameter', $form_state->getValue(['test', 'test_parameter']))
121       ->save();
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function isValid() {
128     return isset($this->type);
129   }
130
131   /**
132    * {@inheritdoc}
133    */
134   public function parseFile() {
135     $this->logCall('parseFile', func_get_args());
136     $data = @getimagesize($this->getSource());
137     if ($data && in_array($data[2], static::supportedTypes())) {
138       $this->setType($data[2]);
139       $this->width = $data[0];
140       $this->height = $data[1];
141       return TRUE;
142     }
143     return FALSE;
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function save($destination) {
150     $this->logCall('save', func_get_args());
151     // Return false so that image_save() doesn't try to chmod the destination
152     // file that we didn't bother to create.
153     return FALSE;
154   }
155
156   /**
157    * Stores the values passed to a toolkit call.
158    *
159    * @param string $op
160    *   One of the toolkit methods 'parseFile', 'save', 'settings', or 'apply'.
161    * @param array $args
162    *   Values passed to hook.
163    *
164    * @see \Drupal\Tests\system\Functional\Image\ToolkitTestBase::imageTestReset()
165    * @see \Drupal\Tests\system\Functional\Image\ToolkitTestBase::imageTestGetAllCalls()
166    */
167   protected function logCall($op, $args) {
168     $results = $this->state->get('image_test.results') ?: [];
169     $results[$op][] = $args;
170     // A call to apply is also logged under its operation name whereby the
171     // array of arguments are logged as separate arguments, this because at the
172     // ImageInterface level we still have methods named after the operations.
173     if ($op === 'apply') {
174       $operation = array_shift($args);
175       $results[$operation][] = array_values(reset($args));
176     }
177     $this->state->set('image_test.results', $results);
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   public function getWidth() {
184     return $this->width;
185   }
186
187   /**
188    * {@inheritdoc}
189    */
190   public function getHeight() {
191     return $this->height;
192   }
193
194   /**
195    * Returns the type of the image.
196    *
197    * @return int
198    *   The image type represented by a PHP IMAGETYPE_* constant (e.g.
199    *   IMAGETYPE_JPEG).
200    */
201   public function getType() {
202     return $this->type;
203   }
204
205   /**
206    * Sets the PHP type of the image.
207    *
208    * @param int $type
209    *   The image type represented by a PHP IMAGETYPE_* constant (e.g.
210    *   IMAGETYPE_JPEG).
211    *
212    * @return $this
213    */
214   public function setType($type) {
215     if (in_array($type, static::supportedTypes())) {
216       $this->type = $type;
217     }
218     return $this;
219   }
220
221   /**
222    * {@inheritdoc}
223    */
224   public function getMimeType() {
225     return $this->getType() ? image_type_to_mime_type($this->getType()) : '';
226   }
227
228   /**
229    * {@inheritdoc}
230    */
231   public static function isAvailable() {
232     return TRUE;
233   }
234
235   /**
236    * {@inheritdoc}
237    */
238   public static function getSupportedExtensions() {
239     $extensions = [];
240     foreach (static::supportedTypes() as $image_type) {
241       $extensions[] = Unicode::strtolower(image_type_to_extension($image_type, FALSE));
242     }
243     return $extensions;
244   }
245
246   /**
247    * Returns a list of image types supported by the toolkit.
248    *
249    * @return array
250    *   An array of available image types. An image type is represented by a PHP
251    *   IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
252    */
253   protected static function supportedTypes() {
254     return [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF];
255   }
256
257   /**
258    * {@inheritdoc}
259    */
260   public function apply($operation, array $arguments = []) {
261     $this->logCall('apply', func_get_args());
262     return TRUE;
263   }
264
265 }