Completed
Pull Request — 8.x-3.x (#525)
by Sebastian
06:05
created

SchemaPluginManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 7
dl 0
loc 21
rs 9.3142
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\Extension\ModuleHandlerInterface;
8
use Drupal\Core\Plugin\DefaultPluginManager;
9
10
class SchemaPluginManager extends DefaultPluginManager {
11
12
  /**
13
   * SchemaPluginManager constructor.
14
   *
15
   * @param bool|string $pluginSubdirectory
16
   *   The plugin's subdirectory.
17
   * @param \Traversable $namespaces
18
   *   An object that implements \Traversable which contains the root paths
19
   *   keyed by the corresponding namespace to look for plugin implementations.
20
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
21
   *   The module handler.
22
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
23
   *   The cache backend.
24
   * @param string|null $pluginInterface
25
   *   The interface each plugin should implement.
26
   * @param string $pluginAnnotationName
27
   *   The name of the annotation that contains the plugin definition.
28
   */
29
  public function __construct(
30
    $pluginSubdirectory,
31
    \Traversable $namespaces,
32
    ModuleHandlerInterface $moduleHandler,
33
    CacheBackendInterface $cacheBackend,
34
    $pluginInterface,
35
    $pluginAnnotationName,
36
    array $config
37
  ) {
38
    parent::__construct(
39
      $pluginSubdirectory,
40
      $namespaces,
41
      $moduleHandler,
42
      $pluginInterface,
43
      $pluginAnnotationName
44
    );
45
46
    $this->alterInfo('graphql_schema');
47
    $this->useCaches(empty($config['development']));
48
    $this->setCacheBackend($cacheBackend, 'schemas', ['graphql', 'graphql:schemas']);
49
  }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54
  protected function setCachedDefinitions($definitions) {
55
    $this->definitions = $definitions;
56
    $this->cacheSet($this->cacheKey, $definitions, $this->getCacheMaxAge(), $this->getCacheTags());
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function getCacheTags() {
63
    $definitions = $this->getDefinitions();
64
    return array_reduce($definitions, function ($carry, $current) {
65
      if (!empty($current['schema_cache_tags'])) {
66
        return Cache::mergeTags($carry, $current['schema_cache_tags']);
67
      }
68
69
      return $carry;
70
    }, $this->cacheTags);
71
  }
72
73
  /**
74
   * {@inheritdoc}
75
   */
76
  public function getCacheMaxAge() {
77
    $definitions = $this->getDefinitions();
78
    $age = Cache::PERMANENT;
79
    foreach ($definitions as $definition) {
80
      if (!isset($definition['schema_cache_max_age'])) {
81
        continue;
82
      }
83
84
      // Bail out early if the cache max age is 0.
85
      if (($age = Cache::mergeMaxAges($age, $definition['schema_cache_max_age'])) === 0) {
86
        return $age;
87
      }
88
    }
89
90
    return $age;
91
  }
92
}
93