summaryrefslogtreecommitdiffstats
path: root/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php')
-rw-r--r--core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php78
1 files changed, 78 insertions, 0 deletions
diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php
new file mode 100644
index 0000000..158b6d4
--- /dev/null
+++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php
@@ -0,0 +1,78 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\system\Plugin\ImageToolkitManager.
+ */
+
+namespace Drupal\system\Plugin;
+
+use Drupal\Component\Plugin\Factory\DefaultFactory;
+use Drupal\Component\Plugin\PluginManagerBase;
+use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
+
+/**
+ * Manages toolkit plugins.
+ */
+class ImageToolkitManager extends PluginManagerBase {
+
+ /**
+ * Constructs the ImageToolkitManager object.
+ */
+ public function __construct(array $namespaces) {
+ $this->discovery = new AnnotatedClassDiscovery('system', 'imagetoolkit', $namespaces);
+ $this->factory = new DefaultFactory($this->discovery);
+ }
+
+ /**
+ * Gets the default image toolkit.
+ *
+ * @param string $toolkit_id
+ * (optional) String specifying toolkit to load. NULL will load the default
+ * toolkit.
+ *
+ * @return \Drupal\system\Plugin\ImageToolkitInterface
+ * Object of the default toolkit, or FALSE on error.
+ */
+ public function getDefaultToolkit() {
+ $toolkit_id = config('system.image')->get('toolkit');
+ $toolkits = $this->getAvailableToolkits();
+
+ if (!isset($toolkits[$toolkit_id]) || !class_exists($toolkits[$toolkit_id]['class'])) {
+ // The selected toolkit isn't available so return the first one found. If
+ // none are available this will return FALSE.
+ reset($toolkits);
+ $toolkit_id = key($toolkits);
+ }
+
+ if ($toolkit_id) {
+ $toolkit = $this->createInstance($toolkit_id);
+ }
+ else {
+ $toolkit = FALSE;
+ }
+
+ return $toolkit;
+ }
+
+ /**
+ * Gets a list of available toolkits.
+ *
+ * @return array
+ * An array with the toolkit names as keys and the descriptions as values.
+ */
+ public function getAvailableToolkits() {
+ // Use plugin system to get list of available toolkits.
+ $toolkits = $this->getDefinitions();
+
+ $output = array();
+ foreach ($toolkits as $id => $definition) {
+ // Only allow modules that aren't marked as unavailable.
+ if (call_user_func($definition['class'] . '::isAvailable')) {
+ $output[$id] = $definition;
+ }
+ }
+
+ return $output;
+ }
+}