Code Duplication    Length = 109-109 lines in 2 locations

src/Plugin/FieldPluginManager.php 1 location

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

src/Plugin/MutationPluginManager.php 1 location

@@ 11-119 (lines=109) @@
8
use Drupal\Core\Extension\ModuleHandlerInterface;
9
use Drupal\Core\Plugin\DefaultPluginManager;
10
11
class MutationPluginManager extends DefaultPluginManager {
12
13
  /**
14
   * Static cache of plugin instances.
15
   *
16
   * @var \Drupal\graphql\Plugin\MutationPluginInterface[]
17
   */
18
  protected $instances;
19
20
  /**
21
   * FieldPluginManager 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
   */
37
  public function __construct(
38
    $pluginSubdirectory,
39
    \Traversable $namespaces,
40
    ModuleHandlerInterface $moduleHandler,
41
    CacheBackendInterface $cacheBackend,
42
    $pluginInterface,
43
    $pluginAnnotationName,
44
    array $config
45
  ) {
46
    parent::__construct(
47
      $pluginSubdirectory,
48
      $namespaces,
49
      $moduleHandler,
50
      $pluginInterface,
51
      $pluginAnnotationName
52
    );
53
54
    $this->alterInfo('graphql_mutations');
55
    $this->useCaches(empty($config['development']));
56
    $this->setCacheBackend($cacheBackend, 'mutations', ['graphql', 'graphql:mutations']);
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function getInstance(array $options) {
63
    if (!isset($this->instances[$options['id']])) {
64
      $this->instances[$options['id']] = $this->createInstance($options['id']);
65
    }
66
67
    return $this->instances[$options['id']];
68
  }
69
70
  /**
71
   * {@inheritdoc}
72
   */
73
  public function clearCachedDefinitions() {
74
    parent::clearCachedDefinitions();
75
    $this->instances = [];
76
  }
77
78
  /**
79
   * {@inheritdoc}
80
   */
81
  protected function setCachedDefinitions($definitions) {
82
    $this->definitions = $definitions;
83
    $this->cacheSet($this->cacheKey, $definitions, $this->getCacheMaxAge(), $this->getCacheTags());
84
  }
85
86
  /**
87
   * {@inheritdoc}
88
   */
89
  public function getCacheTags() {
90
    $definitions = $this->getDefinitions();
91
    return array_reduce($definitions, function ($carry, $current) {
92
      if (!empty($current['schema_cache_tags'])) {
93
        return Cache::mergeTags($carry, $current['schema_cache_tags']);
94
      }
95
96
      return $carry;
97
    }, $this->cacheTags);
98
  }
99
100
  /**
101
   * {@inheritdoc}
102
   */
103
  public function getCacheMaxAge() {
104
    $definitions = $this->getDefinitions();
105
    $age = Cache::PERMANENT;
106
    foreach ($definitions as $definition) {
107
      if (!isset($definition['schema_cache_max_age'])) {
108
        continue;
109
      }
110
111
      // Bail out early if the cache max age is 0.
112
      if (($age = Cache::mergeMaxAges($age, $definition['schema_cache_max_age'])) === 0) {
113
        return $age;
114
      }
115
    }
116
117
    return $age;
118
  }
119
}
120