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