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

/web/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 191 lines | 46 code | 14 blank | 131 comment | 6 complexity | 352fa6b31d81c5a56c4c3cec56407cc8 MD5 | raw file
  1. <?php
  2. namespace Drupal\Core\ImageToolkit;
  3. use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
  4. use Drupal\Core\Plugin\PluginBase;
  5. use Psr\Log\LoggerInterface;
  6. /**
  7. * Provides a base class for image toolkit operation plugins.
  8. *
  9. * @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation
  10. * @see \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
  11. * @see \Drupal\Core\ImageToolkit\ImageToolkitOperationManager
  12. * @see plugin_api
  13. */
  14. abstract class ImageToolkitOperationBase extends PluginBase implements ImageToolkitOperationInterface {
  15. /**
  16. * The image toolkit.
  17. *
  18. * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface
  19. */
  20. protected $toolkit;
  21. /**
  22. * A logger instance.
  23. *
  24. * @var \Psr\Log\LoggerInterface
  25. */
  26. protected $logger;
  27. /**
  28. * Constructs an image toolkit operation plugin.
  29. *
  30. * @param array $configuration
  31. * A configuration array containing information about the plugin instance.
  32. * @param string $plugin_id
  33. * The plugin_id for the plugin instance.
  34. * @param array $plugin_definition
  35. * The plugin implementation definition.
  36. * @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit
  37. * The image toolkit.
  38. * @param \Psr\Log\LoggerInterface $logger
  39. * A logger instance.
  40. */
  41. public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitInterface $toolkit, LoggerInterface $logger) {
  42. parent::__construct($configuration, $plugin_id, $plugin_definition);
  43. $this->toolkit = $toolkit;
  44. $this->logger = $logger;
  45. }
  46. /**
  47. * Returns the image toolkit instance for this operation.
  48. *
  49. * Image toolkit implementers should provide a toolkit operation base class
  50. * that overrides this method to correctly document the return type of this
  51. * getter. This provides better DX (code checking and code completion) for
  52. * image toolkit operation developers.
  53. *
  54. * @return \Drupal\Core\ImageToolkit\ImageToolkitInterface
  55. */
  56. protected function getToolkit() {
  57. return $this->toolkit;
  58. }
  59. /**
  60. * Returns the definition of the operation arguments.
  61. *
  62. * Image toolkit operation implementers must implement this method to
  63. * "document" their operation, thus also if no arguments are expected.
  64. *
  65. * @return array
  66. * An array whose keys are the names of the arguments (e.g. "width",
  67. * "degrees") and each value is an associative array having the following
  68. * keys:
  69. * - description: A string with the argument description. This is used only
  70. * internally for documentation purposes, so it does not need to be
  71. * translatable.
  72. * - required: (optional) A boolean indicating if this argument should be
  73. * provided or not. Defaults to TRUE.
  74. * - default: (optional) When the argument is set to "required" = FALSE,
  75. * this must be set to a default value. Ignored for "required" = TRUE
  76. * arguments.
  77. */
  78. abstract protected function arguments();
  79. /**
  80. * Checks if required arguments are passed in and adds defaults for non passed
  81. * in optional arguments.
  82. *
  83. * Image toolkit operation implementers should not normally need to override
  84. * this method as they should place their own validation in validateArguments.
  85. *
  86. * @param array $arguments
  87. * An associative array of arguments to be used by the toolkit operation.
  88. *
  89. * @return array
  90. * The prepared arguments array.
  91. *
  92. * @throws \InvalidArgumentException.
  93. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException.
  94. */
  95. protected function prepareArguments(array $arguments) {
  96. foreach ($this->arguments() as $id => $argument) {
  97. $argument += ['required' => TRUE];
  98. // Check if the argument is required and, if so, has been provided.
  99. if ($argument['required']) {
  100. if (!array_key_exists($id, $arguments)) {
  101. // If the argument is required throw an exception.
  102. throw new \InvalidArgumentException("Argument '$id' expected by plugin '{$this->getPluginId()}' but not passed");
  103. }
  104. }
  105. else {
  106. // Optional arguments require a 'default' value.
  107. // We check this even if the argument is provided by the caller, as we
  108. // want to fail fast here, i.e. at development time.
  109. if (!array_key_exists('default', $argument)) {
  110. // The plugin did not define a default, so throw a plugin exception,
  111. // not an invalid argument exception.
  112. throw new InvalidPluginDefinitionException("Default for argument '$id' expected by plugin '{$this->getPluginId()}' but not defined");
  113. }
  114. // Use the default value if the argument is not passed in.
  115. if (!array_key_exists($id, $arguments)) {
  116. $arguments[$id] = $argument['default'];
  117. }
  118. }
  119. }
  120. return $arguments;
  121. }
  122. /**
  123. * Validates the arguments.
  124. *
  125. * Image toolkit operation implementers should place any argument validation
  126. * in this method, throwing an InvalidArgumentException when an error is
  127. * encountered.
  128. *
  129. * Validation typically includes things like:
  130. * - Checking that width and height are not negative.
  131. * - Checking that a color value is indeed a color.
  132. *
  133. * But validation may also include correcting the arguments, e.g:
  134. * - Casting arguments to the correct type.
  135. * - Rounding pixel values to an integer.
  136. *
  137. * This base implementation just returns the array of arguments and thus does
  138. * not need to be called by overriding methods.
  139. *
  140. * @param array $arguments
  141. * An associative array of arguments to be used by the toolkit operation.
  142. *
  143. * @return array
  144. * The validated and corrected arguments array.
  145. *
  146. * @throws \InvalidArgumentException
  147. * If one or more of the arguments are not valid.
  148. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  149. * If the plugin does not define a default for an optional argument.
  150. */
  151. protected function validateArguments(array $arguments) {
  152. return $arguments;
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. final public function apply(array $arguments) {
  158. $arguments = $this->prepareArguments($arguments);
  159. $arguments = $this->validateArguments($arguments);
  160. return $this->execute($arguments);
  161. }
  162. /**
  163. * Performs the actual manipulation on the image.
  164. *
  165. * Image toolkit operation implementers must implement this method. This
  166. * method is responsible for actually performing the operation on the image.
  167. * When this method gets called, the implementer may assume all arguments,
  168. * also the optional ones, to be available, validated and corrected.
  169. *
  170. * @param array $arguments
  171. * An associative array of arguments to be used by the toolkit operation.
  172. *
  173. * @return bool
  174. * TRUE if the operation was performed successfully, FALSE otherwise.
  175. */
  176. abstract protected function execute(array $arguments);
  177. }