annotate core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
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 /**
Chris@0 51 * Constructs an ImageToolkitBase object.
Chris@0 52 *
Chris@0 53 * @param array $configuration
Chris@0 54 * A configuration array containing information about the plugin instance.
Chris@0 55 * @param string $plugin_id
Chris@0 56 * The plugin_id for the plugin instance.
Chris@0 57 * @param array $plugin_definition
Chris@0 58 * The plugin implementation definition.
Chris@0 59 * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
Chris@0 60 * The toolkit operation manager.
Chris@0 61 * @param \Psr\Log\LoggerInterface $logger
Chris@0 62 * A logger instance.
Chris@0 63 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 64 * The config factory.
Chris@0 65 */
Chris@0 66 public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
Chris@0 67 parent::__construct($configuration, $plugin_id, $plugin_definition);
Chris@0 68 $this->operationManager = $operation_manager;
Chris@0 69 $this->logger = $logger;
Chris@0 70 $this->configFactory = $config_factory;
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * {@inheritdoc}
Chris@0 75 */
Chris@0 76 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {}
Chris@0 77
Chris@0 78 /**
Chris@0 79 * {@inheritdoc}
Chris@0 80 */
Chris@0 81 public function setSource($source) {
Chris@0 82 // If a previous image has been loaded, there is no way to know if the
Chris@0 83 // toolkit implementation needs to perform any additional actions like
Chris@0 84 // freeing memory. Therefore, the source image cannot be changed once set.
Chris@0 85 if ($this->source) {
Chris@0 86 throw new \BadMethodCallException(__METHOD__ . '() may only be called once');
Chris@0 87 }
Chris@0 88 $this->source = $source;
Chris@0 89 return $this;
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * {@inheritdoc}
Chris@0 94 */
Chris@0 95 public function getSource() {
Chris@0 96 return $this->source;
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * {@inheritdoc}
Chris@0 101 */
Chris@0 102 public function getRequirements() {
Chris@0 103 return [];
Chris@0 104 }
Chris@0 105
Chris@0 106 /**
Chris@0 107 * Gets a toolkit operation plugin instance.
Chris@0 108 *
Chris@0 109 * @param string $operation
Chris@0 110 * The toolkit operation requested.
Chris@0 111 *
Chris@0 112 * @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
Chris@0 113 * An instance of the requested toolkit operation plugin.
Chris@0 114 */
Chris@0 115 protected function getToolkitOperation($operation) {
Chris@0 116 return $this->operationManager->getToolkitOperation($this, $operation);
Chris@0 117 }
Chris@0 118
Chris@0 119 /**
Chris@0 120 * {@inheritdoc}
Chris@0 121 */
Chris@0 122 public function apply($operation, array $arguments = []) {
Chris@0 123 try {
Chris@0 124 // Get the plugin to use for the operation and apply the operation.
Chris@0 125 return $this->getToolkitOperation($operation)->apply($arguments);
Chris@0 126 }
Chris@0 127 catch (PluginNotFoundException $e) {
Chris@0 128 $this->logger->error("The selected image handling toolkit '@toolkit' can not process operation '@operation'.", ['@toolkit' => $this->getPluginId(), '@operation' => $operation]);
Chris@0 129 return FALSE;
Chris@0 130 }
Chris@0 131 catch (\InvalidArgumentException $e) {
Chris@0 132 $this->logger->warning($e->getMessage(), []);
Chris@0 133 return FALSE;
Chris@0 134 }
Chris@0 135 }
Chris@0 136
Chris@0 137 }