annotate core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\ImageToolkit;
Chris@0 4
Chris@0 5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
Chris@0 6 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 7 use Drupal\Core\Form\FormStateInterface;
Chris@0 8 use Drupal\Core\Plugin\PluginBase;
Chris@0 9 use Psr\Log\LoggerInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Provides a base class for image toolkit plugins.
Chris@0 13 *
Chris@0 14 * @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkit
Chris@0 15 * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
Chris@0 16 * @see \Drupal\Core\ImageToolkit\ImageToolkitManager
Chris@0 17 * @see plugin_api
Chris@0 18 */
Chris@0 19 abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface {
Chris@0 20
Chris@0 21 /**
Chris@0 22 * The config factory.
Chris@0 23 *
Chris@0 24 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@0 25 */
Chris@0 26 protected $configFactory;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Path of the image file.
Chris@0 30 *
Chris@0 31 * @var string
Chris@0 32 */
Chris@0 33 protected $source = '';
Chris@0 34
Chris@0 35 /**
Chris@0 36 * The image toolkit operation manager.
Chris@0 37 *
Chris@0 38 * @var \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface
Chris@0 39 */
Chris@0 40 protected $operationManager;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * A logger instance.
Chris@0 44 *
Chris@0 45 * @var \Psr\Log\LoggerInterface
Chris@0 46 */
Chris@0 47 protected $logger;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * Constructs an ImageToolkitBase object.
Chris@0 51 *
Chris@0 52 * @param array $configuration
Chris@0 53 * A configuration array containing information about the plugin instance.
Chris@0 54 * @param string $plugin_id
Chris@0 55 * The plugin_id for the plugin instance.
Chris@0 56 * @param array $plugin_definition
Chris@0 57 * The plugin implementation definition.
Chris@0 58 * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
Chris@0 59 * The toolkit operation manager.
Chris@0 60 * @param \Psr\Log\LoggerInterface $logger
Chris@0 61 * A logger instance.
Chris@0 62 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 63 * The config factory.
Chris@0 64 */
Chris@0 65 public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
Chris@0 66 parent::__construct($configuration, $plugin_id, $plugin_definition);
Chris@0 67 $this->operationManager = $operation_manager;
Chris@0 68 $this->logger = $logger;
Chris@0 69 $this->configFactory = $config_factory;
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
Chris@0 76
Chris@0 77 /**
Chris@0 78 * {@inheritdoc}
Chris@0 79 */
Chris@0 80 public function setSource($source) {
Chris@0 81 // If a previous image has been loaded, there is no way to know if the
Chris@0 82 // toolkit implementation needs to perform any additional actions like
Chris@0 83 // freeing memory. Therefore, the source image cannot be changed once set.
Chris@0 84 if ($this->source) {
Chris@0 85 throw new \BadMethodCallException(__METHOD__ . '() may only be called once');
Chris@0 86 }
Chris@0 87 $this->source = $source;
Chris@0 88 return $this;
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * {@inheritdoc}
Chris@0 93 */
Chris@0 94 public function getSource() {
Chris@0 95 return $this->source;
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * {@inheritdoc}
Chris@0 100 */
Chris@0 101 public function getRequirements() {
Chris@0 102 return [];
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * Gets a toolkit operation plugin instance.
Chris@0 107 *
Chris@0 108 * @param string $operation
Chris@0 109 * The toolkit operation requested.
Chris@0 110 *
Chris@0 111 * @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
Chris@0 112 * An instance of the requested toolkit operation plugin.
Chris@0 113 */
Chris@0 114 protected function getToolkitOperation($operation) {
Chris@0 115 return $this->operationManager->getToolkitOperation($this, $operation);
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * {@inheritdoc}
Chris@0 120 */
Chris@0 121 public function apply($operation, array $arguments = []) {
Chris@0 122 try {
Chris@0 123 // Get the plugin to use for the operation and apply the operation.
Chris@0 124 return $this->getToolkitOperation($operation)->apply($arguments);
Chris@0 125 }
Chris@0 126 catch (PluginNotFoundException $e) {
Chris@0 127 $this->logger->error("The selected image handling toolkit '@toolkit' can not process operation '@operation'.", ['@toolkit' => $this->getPluginId(), '@operation' => $operation]);
Chris@0 128 return FALSE;
Chris@0 129 }
Chris@0 130 catch (\InvalidArgumentException $e) {
Chris@0 131 $this->logger->warning($e->getMessage(), []);
Chris@0 132 return FALSE;
Chris@0 133 }
Chris@0 134 }
Chris@0 135
Chris@0 136 }