View source
<?php
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;
class ImageToolkitOperationManager extends DefaultPluginManager implements ImageToolkitOperationManagerInterface {
protected $logger;
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;
}
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 {
$definition = reset($definitions);
return $definition['id'];
}
}
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);
}
public function getToolkitOperation(ImageToolkitInterface $toolkit, $operation) {
$plugin_id = $this
->getToolkitOperationPluginId($toolkit
->getPluginId(), $operation);
return $this
->createInstance($plugin_id, array(), $toolkit);
}
}