Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\ImageToolkit;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\CacheBackendInterface;
|
Chris@0
|
6 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
7 use Drupal\Core\Plugin\DefaultPluginManager;
|
Chris@0
|
8 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
|
Chris@0
|
9 use Drupal\Component\Plugin\Factory\DefaultFactory;
|
Chris@17
|
10 use Drupal\Component\Render\FormattableMarkup;
|
Chris@0
|
11 use Psr\Log\LoggerInterface;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Manages toolkit operation plugins.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation
|
Chris@0
|
17 * @see \Drupal\Core\ImageToolkit\ImageToolkitOperationBase
|
Chris@0
|
18 * @see \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
|
Chris@0
|
19 * @see plugin_api
|
Chris@0
|
20 */
|
Chris@0
|
21 class ImageToolkitOperationManager extends DefaultPluginManager implements ImageToolkitOperationManagerInterface {
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * A logger instance.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var \Psr\Log\LoggerInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $logger;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * The image toolkit manager.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var \Drupal\Core\ImageToolkit\ImageToolkitManager
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $toolkitManager;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Constructs the ImageToolkitOperationManager object.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @param \Traversable $namespaces
|
Chris@0
|
41 * An object that implements \Traversable which contains the root paths
|
Chris@0
|
42 * keyed by the corresponding namespace to look for plugin implementations.
|
Chris@0
|
43 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
|
Chris@0
|
44 * Cache backend instance to use.
|
Chris@0
|
45 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
46 * The module handler to invoke the alter hook with.
|
Chris@0
|
47 * @param \Psr\Log\LoggerInterface $logger
|
Chris@0
|
48 * A logger instance.
|
Chris@0
|
49 * @param \Drupal\Core\ImageToolkit\ImageToolkitManager $toolkit_manager
|
Chris@0
|
50 * The image toolkit manager.
|
Chris@0
|
51 */
|
Chris@0
|
52 public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, LoggerInterface $logger, ImageToolkitManager $toolkit_manager) {
|
Chris@0
|
53 parent::__construct('Plugin/ImageToolkit/Operation', $namespaces, $module_handler, 'Drupal\Core\ImageToolkit\ImageToolkitOperationInterface', 'Drupal\Core\ImageToolkit\Annotation\ImageToolkitOperation');
|
Chris@0
|
54
|
Chris@0
|
55 $this->alterInfo('image_toolkit_operation');
|
Chris@0
|
56 $this->setCacheBackend($cache_backend, 'image_toolkit_operation_plugins');
|
Chris@0
|
57 $this->logger = $logger;
|
Chris@0
|
58 $this->toolkitManager = $toolkit_manager;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Returns the plugin ID for a given toolkit and operation.
|
Chris@0
|
63 *
|
Chris@0
|
64 * @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit
|
Chris@0
|
65 * The toolkit instance.
|
Chris@0
|
66 * @param string $operation
|
Chris@0
|
67 * The operation (e.g. "crop").
|
Chris@0
|
68 *
|
Chris@0
|
69 * @return string
|
Chris@0
|
70 * The plugin ID.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
|
Chris@0
|
73 * When no plugin is available.
|
Chris@0
|
74 */
|
Chris@0
|
75 protected function getToolkitOperationPluginId(ImageToolkitInterface $toolkit, $operation) {
|
Chris@0
|
76 $toolkit_id = $toolkit->getPluginId();
|
Chris@0
|
77 $definitions = $this->getDefinitions();
|
Chris@0
|
78
|
Chris@0
|
79 $definitions = array_filter($definitions,
|
Chris@0
|
80 function ($definition) use ($toolkit_id, $operation) {
|
Chris@0
|
81 return $definition['toolkit'] == $toolkit_id && $definition['operation'] == $operation;
|
Chris@0
|
82 }
|
Chris@0
|
83 );
|
Chris@0
|
84
|
Chris@0
|
85 if (!$definitions) {
|
Chris@0
|
86 // If this image toolkit plugin is a derivative and returns no operation,
|
Chris@0
|
87 // try once again with its base plugin.
|
Chris@0
|
88 $base_toolkit_id = $toolkit->getBaseId();
|
Chris@0
|
89 if (($toolkit_id != $base_toolkit_id) && !empty($base_toolkit_id)) {
|
Chris@0
|
90 $base_toolkit = $this->toolkitManager->createInstance($base_toolkit_id);
|
Chris@0
|
91 return $this->getToolkitOperationPluginId($base_toolkit, $operation);
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@17
|
94 $message = new FormattableMarkup("No image operation plugin for '@toolkit' toolkit and '@operation' operation.", ['@toolkit' => $toolkit_id, '@operation' => $operation]);
|
Chris@0
|
95 throw new PluginNotFoundException($toolkit_id . '.' . $operation, $message);
|
Chris@0
|
96 }
|
Chris@0
|
97 else {
|
Chris@0
|
98 // Pickup the first plugin found.
|
Chris@0
|
99 // @todo In https://www.drupal.org/node/2110591 we'll return here the UI
|
Chris@0
|
100 // selected plugin or the first found if missed.
|
Chris@0
|
101 $definition = reset($definitions);
|
Chris@0
|
102 return $definition['id'];
|
Chris@0
|
103 }
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * {@inheritdoc}
|
Chris@0
|
108 */
|
Chris@0
|
109 public function createInstance($plugin_id, array $configuration = [], ImageToolkitInterface $toolkit = NULL) {
|
Chris@0
|
110 $plugin_definition = $this->getDefinition($plugin_id);
|
Chris@0
|
111 $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
|
Chris@0
|
112 return new $plugin_class($configuration, $plugin_id, $plugin_definition, $toolkit, $this->logger);
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * {@inheritdoc}
|
Chris@0
|
117 */
|
Chris@0
|
118 public function getToolkitOperation(ImageToolkitInterface $toolkit, $operation) {
|
Chris@0
|
119 $plugin_id = $this->getToolkitOperationPluginId($toolkit, $operation);
|
Chris@0
|
120 return $this->createInstance($plugin_id, [], $toolkit);
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 }
|