Completed
Pull Request — 8.x-3.x (#525)
by Philipp
02:22
created

TypePluginManager::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Plugin;
4
5
use Drupal\Core\Cache\Cache;
6
use Drupal\Core\Cache\CacheBackendInterface;
7
use Drupal\Core\Cache\Context\CacheContextsManager;
8
use Drupal\Core\Extension\ModuleHandlerInterface;
9
use Drupal\Core\Plugin\DefaultPluginManager;
10
11
class TypePluginManager extends DefaultPluginManager implements TypePluginManagerInterface {
12
13
  /**
14
   * Static cache of plugin instances.
15
   *
16
   * @var \Drupal\graphql\Plugin\TypePluginInterface[]
17
   */
18
  protected $instances;
19
20
  /**
21
   * TypePluginManager constructor.
22
   *
23
   * @param bool|string $pluginSubdirectory
24
   *   The plugin's subdirectory.
25
   * @param \Traversable $namespaces
26
   *   An object that implements \Traversable which contains the root paths
27
   *   keyed by the corresponding namespace to look for plugin implementations.
28
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
29
   *   The module handler.
30
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
31
   *   The cache backend.
32
   * @param string|null $pluginInterface
33
   *   The interface each plugin should implement.
34
   * @param string $pluginAnnotationName
35
   *   The name of the annotation that contains the plugin definition.
36
   * @param string $pluginType
37
   *   The plugin type.
38
   */
39
  public function __construct(
40
    $pluginSubdirectory,
41
    \Traversable $namespaces,
42
    ModuleHandlerInterface $moduleHandler,
43
    CacheBackendInterface $cacheBackend,
44
    $pluginInterface,
45
    $pluginAnnotationName,
46
    $pluginType,
47
    array $config
48
  ) {
49
    parent::__construct(
50
      $pluginSubdirectory,
51
      $namespaces,
52
      $moduleHandler,
53
      $pluginInterface,
54
      $pluginAnnotationName
55
    );
56
57
    $this->alterInfo("graphql_{$pluginType}");
58
    $this->useCaches(empty($config['development']));
59
    $this->setCacheBackend($cacheBackend, $pluginType, ['graphql', "graphql:{$pluginType}"]);
60
  }
61
62
  /**
63
   * {@inheritdoc}
64
   */
65
  public function getInstance(array $options) {
66
    if (!isset($this->instances[$options['id']])) {
67
      $this->instances[$options['id']] = $this->createInstance($options['id']);
68
    }
69
70
    return $this->instances[$options['id']];
71
  }
72
73
  /**
74
   * {@inheritdoc}
75
   */
76
  public function clearCachedDefinitions() {
77
    parent::clearCachedDefinitions();
78
    $this->instances = [];
79
  }
80
81
  /**
82
   * {@inheritdoc}
83
   */
84
  protected function setCachedDefinitions($definitions) {
85
    $this->definitions = $definitions;
86
    $this->cacheSet($this->cacheKey, $definitions, $this->getCacheMaxAge(), $this->getCacheTags());
87
  }
88
89
  /**
90
   * {@inheritdoc}
91
   */
92
  public function getCacheTags() {
93
    $definitions = $this->getDefinitions();
94
    return array_reduce($definitions, function ($carry, $current) {
95
      if (!empty($current['schema_cache_tags'])) {
96
        return Cache::mergeTags($carry, $current['schema_cache_tags']);
97
      }
98
99
      return $carry;
100
    }, $this->cacheTags);
101
  }
102
103
  /**
104
   * {@inheritdoc}
105
   */
106
  public function getCacheMaxAge() {
107
    $definitions = $this->getDefinitions();
108
    $age = Cache::PERMANENT;
109
    foreach ($definitions as $definition) {
110
      if (!isset($definition['schema_cache_max_age'])) {
111
        continue;
112
      }
113
114
      // Bail out early if the cache max age is 0.
115
      if (($age = Cache::mergeMaxAges($age, $definition['schema_cache_max_age'])) === 0) {
116
        return $age;
117
      }
118
    }
119
120
    return $age;
121
  }
122
}
123