PageRenderTime 22ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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