|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\ConfigurablePluginInterface; |
|
6
|
|
|
use Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface; |
|
7
|
|
|
use Youshido\GraphQL\Config\Schema\SchemaConfig; |
|
8
|
|
|
use Youshido\GraphQL\Schema\AbstractSchema; |
|
9
|
|
|
|
|
10
|
|
|
class Schema extends AbstractSchema { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The corresponding plugin for this schema. |
|
14
|
|
|
* |
|
15
|
|
|
* @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $plugin; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Schema constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param \Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface $plugin |
|
23
|
|
|
* The corresponding plugin instance for this schema. |
|
24
|
|
|
* @param array $config |
|
25
|
|
|
* The schema configuration array. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(SchemaPluginInterface $plugin, array $config = []) { |
|
28
|
|
|
parent::__construct($config); |
|
29
|
|
|
$this->plugin = $plugin; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Retrieves the schema's plugin instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @return \Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface |
|
36
|
|
|
* The schema plugin instance. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getSchemaPlugin() { |
|
39
|
|
|
if (is_array($this->plugin)) { |
|
40
|
|
|
/** @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginManager $manager */ |
|
41
|
|
|
$manager = \Drupal::service('plugin.manager.graphql.schema'); |
|
42
|
|
|
|
|
43
|
|
|
// Recover the schema plugin reference. |
|
44
|
|
|
$this->plugin = $manager->createInstance($this->plugin['id'], $this->plugin['configuration']); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $this->plugin; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function build(SchemaConfig $config) { |
|
54
|
|
|
// Nothing to do here. |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function __sleep() { |
|
|
|
|
|
|
61
|
|
|
// Instead of serializing the referenced plugin, we just serialize the |
|
62
|
|
|
// plugin id and configuration. |
|
63
|
|
|
$this->plugin = [ |
|
|
|
|
|
|
64
|
|
|
'id' => $this->plugin->getPluginId(), |
|
65
|
|
|
'configuration' => $this->plugin instanceof ConfigurablePluginInterface ? $this->plugin->getConfiguration() : [], |
|
|
|
|
|
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
return array_keys(get_object_vars($this)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.