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