3 namespace Drupal\Core\ImageToolkit;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Plugin\DefaultPluginManager;
11 * Manages image toolkit plugins.
13 * @see \Drupal\Core\ImageToolkit\Annotation\ImageToolkit
14 * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface
15 * @see \Drupal\Core\ImageToolkit\ImageToolkitBase
18 class ImageToolkitManager extends DefaultPluginManager {
23 * @var \Drupal\Core\Config\ConfigFactoryInterface
25 protected $configFactory;
28 * Constructs the ImageToolkitManager object.
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
37 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
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');
43 $this->setCacheBackend($cache_backend, 'image_toolkit_plugins');
44 $this->configFactory = $config_factory;
48 * Gets the default image toolkit ID.
51 * ID of the default toolkit, or FALSE on error.
53 public function getDefaultToolkitId() {
54 $toolkit_id = $this->configFactory->get('system.image')->get('toolkit');
55 $toolkits = $this->getAvailableToolkits();
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.
61 $toolkit_id = key($toolkits);
68 * Gets the default image toolkit.
70 * @return \Drupal\Core\ImageToolkit\ImageToolkitInterface
71 * Object of the default toolkit, or FALSE on error.
73 public function getDefaultToolkit() {
74 if ($toolkit_id = $this->getDefaultToolkitId()) {
75 return $this->createInstance($toolkit_id);
81 * Gets a list of available toolkits.
84 * An array with the toolkit names as keys and the descriptions as values.
86 public function getAvailableToolkits() {
87 // Use plugin system to get list of available toolkits.
88 $toolkits = $this->getDefinitions();
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;