comparison core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\ImageToolkit;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Plugin\DefaultPluginManager;
9
10 /**
11 * Manages image toolkit plugins.
12 *
13 * @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkit
14 * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
15 * @see \Drupal\Core\ImageToolkit\ImageToolkitBase
16 * @see plugin_api
17 */
18 class ImageToolkitManager extends DefaultPluginManager {
19
20 /**
21 * The config factory.
22 *
23 * @var \Drupal\Core\Config\ConfigFactoryInterface
24 */
25 protected $configFactory;
26
27 /**
28 * Constructs the ImageToolkitManager object.
29 *
30 * @param \Traversable $namespaces
31 * An object that implements \Traversable which contains the root paths
32 * keyed by the corresponding namespace to look for plugin implementations.
33 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
34 * Cache backend instance to use.
35 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
36 * The module handler.
37 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
38 * The config factory.
39 */
40 public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
41 parent::__construct('Plugin/ImageToolkit', $namespaces, $module_handler, 'Drupal\Core\ImageToolkit\ImageToolkitInterface', 'Drupal\Core\ImageToolkit\Annotation\ImageToolkit');
42
43 $this->setCacheBackend($cache_backend, 'image_toolkit_plugins');
44 $this->configFactory = $config_factory;
45 }
46
47 /**
48 * Gets the default image toolkit ID.
49 *
50 * @return string|bool
51 * ID of the default toolkit, or FALSE on error.
52 */
53 public function getDefaultToolkitId() {
54 $toolkit_id = $this->configFactory->get('system.image')->get('toolkit');
55 $toolkits = $this->getAvailableToolkits();
56
57 if (!isset($toolkits[$toolkit_id]) || !class_exists($toolkits[$toolkit_id]['class'])) {
58 // The selected toolkit isn't available so return the first one found. If
59 // none are available this will return FALSE.
60 reset($toolkits);
61 $toolkit_id = key($toolkits);
62 }
63
64 return $toolkit_id;
65 }
66
67 /**
68 * Gets the default image toolkit.
69 *
70 * @return \Drupal\Core\ImageToolkit\ImageToolkitInterface
71 * Object of the default toolkit, or FALSE on error.
72 */
73 public function getDefaultToolkit() {
74 if ($toolkit_id = $this->getDefaultToolkitId()) {
75 return $this->createInstance($toolkit_id);
76 }
77 return FALSE;
78 }
79
80 /**
81 * Gets a list of available toolkits.
82 *
83 * @return array
84 * An array with the toolkit names as keys and the descriptions as values.
85 */
86 public function getAvailableToolkits() {
87 // Use plugin system to get list of available toolkits.
88 $toolkits = $this->getDefinitions();
89
90 $output = [];
91 foreach ($toolkits as $id => $definition) {
92 // Only allow modules that aren't marked as unavailable.
93 if (call_user_func($definition['class'] . '::isAvailable')) {
94 $output[$id] = $definition;
95 }
96 }
97
98 return $output;
99 }
100
101 }