summaryrefslogtreecommitdiffstats
path: root/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php
blob: 158b6d4ea232da346b4a6543098f19e8307b502d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
  }
}