PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php

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