Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\ImageToolkit\Annotation\ImageToolkit.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\ImageToolkit\Annotation;
|
||||
|
||||
use Drupal\Component\Annotation\Plugin;
|
||||
|
||||
/**
|
||||
* Defines a Plugin annotation object for the image toolkit plugin.
|
||||
*
|
||||
* An image toolkit provides common image file manipulations like scaling,
|
||||
* cropping, and rotating.
|
||||
*
|
||||
* Plugin namespace: Plugin\ImageToolkit
|
||||
*
|
||||
* For a working example, see
|
||||
* \Drupal\system\Plugin\ImageToolkit\GDToolkit
|
||||
*
|
||||
* @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitBase
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitManager
|
||||
* @see plugin_api
|
||||
*
|
||||
* @Annotation
|
||||
*/
|
||||
class ImageToolkit extends Plugin {
|
||||
|
||||
/**
|
||||
* The plugin ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* The title of the image toolkit.
|
||||
*
|
||||
* The string should be wrapped in a @Translation().
|
||||
*
|
||||
* @ingroup plugin_translatable
|
||||
*
|
||||
* @var \Drupal\Core\Annotation\Translation
|
||||
*/
|
||||
public $title;
|
||||
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\ImageToolkit\Annotation;
|
||||
|
||||
use Drupal\Component\Annotation\Plugin;
|
||||
|
||||
/**
|
||||
* Defines a Plugin annotation object for the image toolkit operation plugin.
|
||||
*
|
||||
* An image toolkit operation plugin provides a self-contained image
|
||||
* manipulation routine, for a specific image toolkit. Examples of image
|
||||
* toolkit operations are scaling, cropping, rotating, etc.
|
||||
*
|
||||
* Plugin namespace: Plugin\ImageToolkit\Operation
|
||||
*
|
||||
* For a working example, see
|
||||
* \Drupal\system\Plugin\ImageToolkit\Operation\gd\Crop
|
||||
*
|
||||
* @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkit
|
||||
* @see \Drupal\image\Annotation\ImageEffect
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitOperationBase
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitOperationManager
|
||||
* @see plugin_api
|
||||
*
|
||||
* @Annotation
|
||||
*/
|
||||
class ImageToolkitOperation extends Plugin {
|
||||
|
||||
/**
|
||||
* The plugin ID.
|
||||
*
|
||||
* There are no strict requirements as to the string to be used to identify
|
||||
* the plugin, since discovery of the appropriate operation plugin to be
|
||||
* used to apply an operation is based on the values of the 'toolkit' and
|
||||
* the 'operation' annotation values.
|
||||
*
|
||||
* However, it is recommended that the following patterns be used:
|
||||
* - '{toolkit}_{operation}' for the first implementation of an operation
|
||||
* by a toolkit.
|
||||
* - '{module}_{toolkit}_{operation}' for overrides of existing
|
||||
* implementations supplied by an alternative module, and for new
|
||||
* module-supplied operations.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* The id of the image toolkit plugin for which the operation is implemented.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $toolkit;
|
||||
|
||||
/**
|
||||
* The machine name of the image toolkit operation implemented (e.g. "crop").
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $operation;
|
||||
|
||||
/**
|
||||
* The human-readable name of the image toolkit operation.
|
||||
*
|
||||
* The string should be wrapped in a @Translation().
|
||||
*
|
||||
* @ingroup plugin_translatable
|
||||
*
|
||||
* @var \Drupal\Core\Annotation\Translation
|
||||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* The description of the image toolkit operation.
|
||||
*
|
||||
* The string should be wrapped in a @Translation().
|
||||
*
|
||||
* @ingroup plugin_translatable
|
||||
*
|
||||
* @var \Drupal\Core\Annotation\Translation
|
||||
*/
|
||||
public $description;
|
||||
|
||||
}
|
139
core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
Normal file
139
core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
Normal file
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\ImageToolkit\ImageToolkitBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\ImageToolkit;
|
||||
|
||||
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Image\ImageInterface;
|
||||
use Drupal\Core\Plugin\PluginBase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Provides a base class for image toolkit plugins.
|
||||
*
|
||||
* @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkit
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface {
|
||||
|
||||
/**
|
||||
* The config factory.
|
||||
*
|
||||
* @var \Drupal\Core\Config\ConfigFactoryInterface
|
||||
*/
|
||||
protected $configFactory;
|
||||
|
||||
/**
|
||||
* Image object this toolkit instance is tied to.
|
||||
*
|
||||
* @var \Drupal\Core\Image\ImageInterface
|
||||
*/
|
||||
protected $image;
|
||||
|
||||
/**
|
||||
* The image toolkit operation manager.
|
||||
*
|
||||
* @var \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface
|
||||
*/
|
||||
protected $operationManager;
|
||||
|
||||
/**
|
||||
* A logger instance.
|
||||
*
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs an ImageToolkitBase object.
|
||||
*
|
||||
* @param array $configuration
|
||||
* A configuration array containing information about the plugin instance.
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the plugin instance.
|
||||
* @param array $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
|
||||
* The toolkit operation manager.
|
||||
* @param \Psr\Log\LoggerInterface $logger
|
||||
* A logger instance.
|
||||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
||||
* The config factory.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->operationManager = $operation_manager;
|
||||
$this->logger = $logger;
|
||||
$this->configFactory = $config_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { }
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setImage(ImageInterface $image) {
|
||||
if ($this->image) {
|
||||
throw new \BadMethodCallException(__METHOD__ . '() may only be called once');
|
||||
}
|
||||
$this->image = $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getImage() {
|
||||
return $this->image;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRequirements() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a toolkit operation plugin instance.
|
||||
*
|
||||
* @param string $operation
|
||||
* The toolkit operation requested.
|
||||
*
|
||||
* @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
|
||||
* An instance of the requested toolkit operation plugin.
|
||||
*/
|
||||
protected function getToolkitOperation($operation) {
|
||||
return $this->operationManager->getToolkitOperation($this, $operation);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply($operation, array $arguments = array()) {
|
||||
try {
|
||||
// Get the plugin to use for the operation and apply the operation.
|
||||
return $this->getToolkitOperation($operation)->apply($arguments);
|
||||
}
|
||||
catch (PluginNotFoundException $e) {
|
||||
$this->logger->error("The selected image handling toolkit '@toolkit' can not process operation '@operation'.", array('@toolkit' => $this->getPluginId(), '@operation' => $operation));
|
||||
return FALSE;
|
||||
}
|
||||
catch (\InvalidArgumentException $e) {
|
||||
$this->logger->warning($e->getMessage(), array());
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
176
core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
Normal file
176
core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
Normal file
|
@ -0,0 +1,176 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\ImageToolkit\ImageToolkitInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\ImageToolkit;
|
||||
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Plugin\PluginFormInterface;
|
||||
use Drupal\Core\Image\ImageInterface;
|
||||
use Drupal\Component\Plugin\PluginInspectionInterface;
|
||||
|
||||
/**
|
||||
* @defgroup image Image toolkits
|
||||
* @{
|
||||
* Functions for image file manipulations.
|
||||
*
|
||||
* Drupal's image toolkits provide an abstraction layer for common image file
|
||||
* manipulations like scaling, cropping, and rotating. The abstraction frees
|
||||
* module authors from the need to support multiple image libraries, and it
|
||||
* allows site administrators to choose the library that's best for them.
|
||||
*
|
||||
* PHP includes the GD library by default so a GD toolkit is installed with
|
||||
* Drupal. Other toolkits like ImageMagick are available from contrib modules.
|
||||
* GD works well for small images, but using it with larger files may cause PHP
|
||||
* to run out of memory. In contrast the ImageMagick library does not suffer
|
||||
* from this problem, but it requires the ISP to have installed additional
|
||||
* software.
|
||||
*
|
||||
* Image toolkits are discovered using the Plugin system using
|
||||
* \Drupal\Core\ImageToolkit\ImageToolkitManager. The toolkit must then be
|
||||
* enabled using the admin/config/media/image-toolkit form.
|
||||
*
|
||||
* Only one toolkit may be selected at a time. If a module author wishes to call
|
||||
* a specific toolkit they can check that it is installed by calling
|
||||
* \Drupal\Core\ImageToolkit\ImageToolkitManager::getAvailableToolkits(), and
|
||||
* then calling its functions directly.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Defines an interface for image toolkits.
|
||||
*
|
||||
* An image toolkit provides common image file manipulations like scaling,
|
||||
* cropping, and rotating.
|
||||
*
|
||||
* @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkit
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitBase
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
interface ImageToolkitInterface extends ContainerFactoryPluginInterface, PluginInspectionInterface, PluginFormInterface {
|
||||
|
||||
/**
|
||||
* Sets the image object that this toolkit instance is tied to.
|
||||
*
|
||||
* @param \Drupal\Core\Image\ImageInterface $image
|
||||
* The image that this toolkit instance will be tied to.
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* When called twice.
|
||||
*/
|
||||
public function setImage(ImageInterface $image);
|
||||
|
||||
/**
|
||||
* Gets the image object that this toolkit instance is tied to.
|
||||
*
|
||||
* @return \Drupal\Core\Image\ImageInterface
|
||||
* The image object that this toolkit instance is tied to.
|
||||
*/
|
||||
public function getImage();
|
||||
|
||||
/**
|
||||
* Checks if the image is valid.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the image toolkit is currently handling a valid image, FALSE
|
||||
* otherwise.
|
||||
*/
|
||||
public function isValid();
|
||||
|
||||
/**
|
||||
* Writes an image resource to a destination file.
|
||||
*
|
||||
* @param string $destination
|
||||
* A string file URI or path where the image should be saved.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function save($destination);
|
||||
|
||||
/**
|
||||
* Determines if a file contains a valid image.
|
||||
*
|
||||
* Drupal supports GIF, JPG and PNG file formats when used with the GD
|
||||
* toolkit, and may support others, depending on which toolkits are
|
||||
* installed.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the file could be found and is an image, FALSE otherwise.
|
||||
*/
|
||||
public function parseFile();
|
||||
|
||||
/**
|
||||
* Returns the height of the image.
|
||||
*
|
||||
* @return int|null
|
||||
* The height of the image, or NULL if the image is invalid.
|
||||
*/
|
||||
public function getHeight();
|
||||
|
||||
/**
|
||||
* Returns the width of the image.
|
||||
*
|
||||
* @return int|null
|
||||
* The width of the image, or NULL if the image is invalid.
|
||||
*/
|
||||
public function getWidth();
|
||||
|
||||
/**
|
||||
* Returns the MIME type of the image file.
|
||||
*
|
||||
* @return string
|
||||
* The MIME type of the image file, or an empty string if the image is
|
||||
* invalid.
|
||||
*/
|
||||
public function getMimeType();
|
||||
|
||||
/**
|
||||
* Gets toolkit requirements in a format suitable for hook_requirements().
|
||||
*
|
||||
* @return array
|
||||
* An associative requirements array as is returned by hook_requirements().
|
||||
* If the toolkit claims no requirements to the system, returns an empty
|
||||
* array. The array can have arbitrary keys and they do not have to be
|
||||
* prefixed by e.g. the module name or toolkit ID, as the system will make
|
||||
* the keys globally unique.
|
||||
*
|
||||
* @see hook_requirements()
|
||||
*/
|
||||
public function getRequirements();
|
||||
|
||||
/**
|
||||
* Verifies that the Image Toolkit is set up correctly.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the toolkit is available on this machine, FALSE otherwise.
|
||||
*/
|
||||
public static function isAvailable();
|
||||
|
||||
/**
|
||||
* Returns a list of image file extensions supported by the toolkit.
|
||||
*
|
||||
* @return array
|
||||
* An array of supported image file extensions (e.g. png/jpeg/gif).
|
||||
*/
|
||||
public static function getSupportedExtensions();
|
||||
|
||||
/**
|
||||
* Applies a toolkit operation to an image.
|
||||
*
|
||||
* @param string $operation
|
||||
* The toolkit operation to be processed.
|
||||
* @param array $arguments
|
||||
* An associative array of arguments to be passed to the toolkit
|
||||
* operation, e.g. array('width' => 50, 'height' => 100,
|
||||
* 'upscale' => TRUE).
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the operation was performed successfully, FALSE otherwise.
|
||||
*/
|
||||
public function apply($operation, array $arguments = array());
|
||||
|
||||
}
|
106
core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php
Normal file
106
core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php
Normal file
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\ImageToolkit\ImageToolkitManager.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\ImageToolkit;
|
||||
|
||||
use Drupal\Core\Cache\CacheBackendInterface;
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Plugin\DefaultPluginManager;
|
||||
|
||||
/**
|
||||
* Manages image toolkit plugins.
|
||||
*
|
||||
* @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkit
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitBase
|
||||
* @see plugin_api
|
||||
*/
|
||||
class ImageToolkitManager extends DefaultPluginManager {
|
||||
|
||||
/**
|
||||
* The config factory.
|
||||
*
|
||||
* @var \Drupal\Core\Config\ConfigFactoryInterface
|
||||
*/
|
||||
protected $configFactory;
|
||||
|
||||
/**
|
||||
* Constructs the ImageToolkitManager object.
|
||||
*
|
||||
* @param \Traversable $namespaces
|
||||
* An object that implements \Traversable which contains the root paths
|
||||
* keyed by the corresponding namespace to look for plugin implementations.
|
||||
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
|
||||
* Cache backend instance to use.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler.
|
||||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
||||
* The config factory.
|
||||
*/
|
||||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
|
||||
parent::__construct('Plugin/ImageToolkit', $namespaces, $module_handler, 'Drupal\Core\ImageToolkit\ImageToolkitInterface', 'Drupal\Core\ImageToolkit\Annotation\ImageToolkit');
|
||||
|
||||
$this->setCacheBackend($cache_backend, 'image_toolkit_plugins');
|
||||
$this->configFactory = $config_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default image toolkit ID.
|
||||
*
|
||||
* @return string|bool
|
||||
* ID of the default toolkit, or FALSE on error.
|
||||
*/
|
||||
public function getDefaultToolkitId() {
|
||||
$toolkit_id = $this->configFactory->get('system.image')->get('toolkit');
|
||||
$toolkits = $this->getAvailableToolkits();
|
||||
|
||||
if (!isset($toolkits[$toolkit_id]) || !class_exists($toolkits[$toolkit_id]['class'])) {
|
||||
// The selected toolkit isn't available so return the first one found. If
|
||||
// none are available this will return FALSE.
|
||||
reset($toolkits);
|
||||
$toolkit_id = key($toolkits);
|
||||
}
|
||||
|
||||
return $toolkit_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default image toolkit.
|
||||
*
|
||||
* @return \Drupal\Core\ImageToolkit\ImageToolkitInterface
|
||||
* Object of the default toolkit, or FALSE on error.
|
||||
*/
|
||||
public function getDefaultToolkit() {
|
||||
if ($toolkit_id = $this->getDefaultToolkitId()) {
|
||||
return $this->createInstance($toolkit_id);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of available toolkits.
|
||||
*
|
||||
* @return array
|
||||
* An array with the toolkit names as keys and the descriptions as values.
|
||||
*/
|
||||
public function getAvailableToolkits() {
|
||||
// Use plugin system to get list of available toolkits.
|
||||
$toolkits = $this->getDefinitions();
|
||||
|
||||
$output = array();
|
||||
foreach ($toolkits as $id => $definition) {
|
||||
// Only allow modules that aren't marked as unavailable.
|
||||
if (call_user_func($definition['class'] . '::isAvailable')) {
|
||||
$output[$id] = $definition;
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
}
|
197
core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php
Normal file
197
core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php
Normal file
|
@ -0,0 +1,197 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\ImageToolkit\ImageToolkitOperationBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\ImageToolkit;
|
||||
|
||||
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Plugin\PluginBase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Provides a base class for image toolkit operation plugins.
|
||||
*
|
||||
* @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitOperationManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
abstract class ImageToolkitOperationBase extends PluginBase implements ImageToolkitOperationInterface {
|
||||
|
||||
/**
|
||||
* The image toolkit.
|
||||
*
|
||||
* @var \Drupal\Core\ImageToolkit\ImageToolkitInterface
|
||||
*/
|
||||
protected $toolkit;
|
||||
|
||||
/**
|
||||
* A logger instance.
|
||||
*
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* Constructs an image toolkit operation plugin.
|
||||
*
|
||||
* @param array $configuration
|
||||
* A configuration array containing information about the plugin instance.
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the plugin instance.
|
||||
* @param array $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit
|
||||
* The image toolkit.
|
||||
* @param \Psr\Log\LoggerInterface $logger
|
||||
* A logger instance.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitInterface $toolkit, LoggerInterface $logger) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->toolkit = $toolkit;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the image toolkit instance for this operation.
|
||||
*
|
||||
* Image toolkit implementers should provide a toolkit operation base class
|
||||
* that overrides this method to correctly document the return type of this
|
||||
* getter. This provides better DX (code checking and code completion) for
|
||||
* image toolkit operation developers.
|
||||
*
|
||||
* @return \Drupal\Core\ImageToolkit\ImageToolkitInterface
|
||||
*/
|
||||
protected function getToolkit() {
|
||||
return $this->toolkit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the definition of the operation arguments.
|
||||
*
|
||||
* Image toolkit operation implementers must implement this method to
|
||||
* "document" their operation, thus also if no arguments are expected.
|
||||
*
|
||||
* @return array
|
||||
* An array whose keys are the names of the arguments (e.g. "width",
|
||||
* "degrees") and each value is an associative array having the following
|
||||
* keys:
|
||||
* - description: A string with the argument description. This is used only
|
||||
* internally for documentation purposes, so it does not need to be
|
||||
* translatable.
|
||||
* - required: (optional) A boolean indicating if this argument should be
|
||||
* provided or not. Defaults to TRUE.
|
||||
* - default: (optional) When the argument is set to "required" = FALSE,
|
||||
* this must be set to a default value. Ignored for "required" = TRUE
|
||||
* arguments.
|
||||
*/
|
||||
abstract protected function arguments();
|
||||
|
||||
/**
|
||||
* Checks if required arguments are passed in and adds defaults for non passed
|
||||
* in optional arguments.
|
||||
*
|
||||
* Image toolkit operation implementers should not normally need to override
|
||||
* this method as they should place their own validation in validateArguments.
|
||||
*
|
||||
* @param array $arguments
|
||||
* An associative array of arguments to be used by the toolkit operation.
|
||||
*
|
||||
* @return array
|
||||
* The prepared arguments array.
|
||||
*
|
||||
* @throws \InvalidArgumentException.
|
||||
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException.
|
||||
*/
|
||||
protected function prepareArguments(array $arguments) {
|
||||
foreach ($this->arguments() as $id => $argument) {
|
||||
$argument += array('required' => TRUE);
|
||||
// Check if the argument is required and, if so, has been provided.
|
||||
if ($argument['required']) {
|
||||
if (!array_key_exists($id, $arguments)) {
|
||||
// If the argument is required throw an exception.
|
||||
throw new \InvalidArgumentException(SafeMarkup::format("Argument '@argument' expected by plugin '@plugin' but not passed", array('@argument' => $id, '@plugin' => $this->getPluginId())));
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Optional arguments require a 'default' value.
|
||||
// We check this even if the argument is provided by the caller, as we
|
||||
// want to fail fast here, i.e. at development time.
|
||||
if (!array_key_exists('default', $argument)) {
|
||||
// The plugin did not define a default, so throw a plugin exception,
|
||||
// not an invalid argument exception.
|
||||
throw new InvalidPluginDefinitionException(SafeMarkup::format("Default for argument '@argument' expected by plugin '@plugin' but not defined", array('@argument' => $id, '@plugin' => $this->getPluginId())));
|
||||
}
|
||||
|
||||
// Use the default value if the argument is not passed in.
|
||||
if (!array_key_exists($id, $arguments)) {
|
||||
$arguments[$id] = $argument['default'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the arguments.
|
||||
*
|
||||
* Image toolkit operation implementers should place any argument validation
|
||||
* in this method, throwing an InvalidArgumentException when an error is
|
||||
* encountered.
|
||||
*
|
||||
* Validation typically includes things like:
|
||||
* - Checking that width and height are not negative.
|
||||
* - Checking that a color value is indeed a color.
|
||||
*
|
||||
* But validation may also include correcting the arguments, e.g:
|
||||
* - Casting arguments to the correct type.
|
||||
* - Rounding pixel values to an integer.
|
||||
*
|
||||
* This base implementation just returns the array of arguments and thus does
|
||||
* not need to be called by overriding methods.
|
||||
*
|
||||
* @param array $arguments
|
||||
* An associative array of arguments to be used by the toolkit operation.
|
||||
*
|
||||
* @return array
|
||||
* The validated and corrected arguments array.
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* If one or more of the arguments are not valid.
|
||||
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
|
||||
* If the plugin does not define a default for an optional argument.
|
||||
*/
|
||||
protected function validateArguments(array $arguments) {
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public final function apply(array $arguments) {
|
||||
$arguments = $this->prepareArguments($arguments);
|
||||
$arguments = $this->validateArguments($arguments);
|
||||
return $this->execute($arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the actual manipulation on the image.
|
||||
*
|
||||
* Image toolkit operation implementers must implement this method. This
|
||||
* method is responsible for actually performing the operation on the image.
|
||||
* When this method gets called, the implementer may assume all arguments,
|
||||
* also the optional ones, to be available, validated and corrected.
|
||||
*
|
||||
* @param array $arguments
|
||||
* An associative array of arguments to be used by the toolkit operation.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the operation was performed successfully, FALSE otherwise.
|
||||
*/
|
||||
abstract protected function execute(array $arguments);
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\ImageToolkit;
|
||||
|
||||
use Drupal\Component\Plugin\PluginInspectionInterface;
|
||||
|
||||
/**
|
||||
* Defines an interface for image toolkit operations.
|
||||
*
|
||||
* An image toolkit operation plugin provides a self-contained image
|
||||
* manipulation routine, for a specific image toolkit. Examples of image
|
||||
* toolkit operations are scaling, cropping, rotating, etc.
|
||||
*
|
||||
* @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitOperationBase
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitOperationManager
|
||||
* @see plugin_api
|
||||
*/
|
||||
interface ImageToolkitOperationInterface extends PluginInspectionInterface {
|
||||
|
||||
/**
|
||||
* Applies a toolkit specific operation to an image.
|
||||
*
|
||||
* @param array $arguments
|
||||
* An associative array of data to be used by the toolkit operation.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the operation was performed successfully, FALSE otherwise.
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* If one or more of the arguments are not valid.
|
||||
*/
|
||||
public function apply(array $arguments);
|
||||
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\ImageToolkit\ImageToolkitOperationManager.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\ImageToolkit;
|
||||
|
||||
use Drupal\Core\Cache\CacheBackendInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Plugin\DefaultPluginManager;
|
||||
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
|
||||
use Drupal\Component\Plugin\Factory\DefaultFactory;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Manages toolkit operation plugins.
|
||||
*
|
||||
* @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitOperationBase
|
||||
* @see \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
|
||||
* @see plugin_api
|
||||
*/
|
||||
class ImageToolkitOperationManager extends DefaultPluginManager implements ImageToolkitOperationManagerInterface {
|
||||
|
||||
/**
|
||||
* A logger instance.
|
||||
*
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* Constructs the ImageToolkitOperationManager object.
|
||||
*
|
||||
* @param \Traversable $namespaces
|
||||
* An object that implements \Traversable which contains the root paths
|
||||
* keyed by the corresponding namespace to look for plugin implementations.
|
||||
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
|
||||
* Cache backend instance to use.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler to invoke the alter hook with.
|
||||
* @param \Psr\Log\LoggerInterface $logger
|
||||
* A logger instance.
|
||||
*/
|
||||
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, LoggerInterface $logger) {
|
||||
parent::__construct('Plugin/ImageToolkit/Operation', $namespaces, $module_handler, 'Drupal\Core\ImageToolkit\ImageToolkitOperationInterface', 'Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation');
|
||||
|
||||
$this->alterInfo('image_toolkit_operation');
|
||||
$this->setCacheBackend($cache_backend, 'image_toolkit_operation_plugins');
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the plugin ID for a given toolkit and operation.
|
||||
*
|
||||
* @param string $toolkit_id
|
||||
* The toolkit plugin ID.
|
||||
* @param string $operation
|
||||
* The operation (e.g. "crop").
|
||||
*
|
||||
* @return string
|
||||
* The plugin ID.
|
||||
*
|
||||
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
|
||||
* When no plugin is available.
|
||||
*/
|
||||
protected function getToolkitOperationPluginId($toolkit_id, $operation) {
|
||||
$definitions = $this->getDefinitions();
|
||||
|
||||
$definitions = array_filter($definitions,
|
||||
function ($definition) use ($toolkit_id, $operation) {
|
||||
return $definition['toolkit'] == $toolkit_id && $definition['operation'] == $operation;
|
||||
}
|
||||
);
|
||||
|
||||
if (!$definitions) {
|
||||
$message = SafeMarkup::format("No image operation plugin for '@toolkit' toolkit and '@operation' operation.", array('@toolkit' => $toolkit_id, '@operation' => $operation));
|
||||
throw new PluginNotFoundException($toolkit_id . '.' . $operation, $message);
|
||||
}
|
||||
else {
|
||||
// Pickup the first plugin found.
|
||||
// @todo In https://www.drupal.org/node/2110591 we'll return here the UI
|
||||
// selected plugin or the first found if missed.
|
||||
$definition = reset($definitions);
|
||||
return $definition['id'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createInstance($plugin_id, array $configuration = array(), ImageToolkitInterface $toolkit = NULL) {
|
||||
$plugin_definition = $this->getDefinition($plugin_id);
|
||||
$plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
|
||||
return new $plugin_class($configuration, $plugin_id, $plugin_definition, $toolkit, $this->logger);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getToolkitOperation(ImageToolkitInterface $toolkit, $operation) {
|
||||
$plugin_id = $this->getToolkitOperationPluginId($toolkit->getPluginId(), $operation);
|
||||
return $this->createInstance($plugin_id, array(), $toolkit);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\ImageToolkit;
|
||||
|
||||
/**
|
||||
* Defines an interface for image toolkit operation managers.
|
||||
*/
|
||||
interface ImageToolkitOperationManagerInterface {
|
||||
|
||||
/**
|
||||
* Returns a toolkit operation plugin instance.
|
||||
*
|
||||
* @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit
|
||||
* The toolkit instance.
|
||||
* @param string $operation
|
||||
* The operation (e.g. "crop").
|
||||
*
|
||||
* @return \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
|
||||
* An instance of the requested toolkit operation plugin.
|
||||
*
|
||||
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
|
||||
* When no plugin is available.
|
||||
*/
|
||||
public function getToolkitOperation(ImageToolkitInterface $toolkit, $operation);
|
||||
|
||||
}
|
Reference in a new issue