1 | <?php |
||
22 | class ExecutionContext implements ExecutionContextInterface |
||
23 | { |
||
24 | |||
25 | use ErrorContainerTrait; |
||
26 | |||
27 | /** @var AbstractSchema */ |
||
28 | private $schema; |
||
29 | |||
30 | /** @var Request */ |
||
31 | private $request; |
||
32 | |||
33 | /** @var ContainerInterface */ |
||
34 | private $container; |
||
35 | |||
36 | /** @var array */ |
||
37 | private $typeFieldLookupTable; |
||
38 | |||
39 | /** |
||
40 | * ExecutionContext constructor. |
||
41 | * |
||
42 | * @param AbstractSchema $schema |
||
43 | */ |
||
44 | 73 | public function __construct(AbstractSchema $schema) |
|
45 | { |
||
46 | 73 | $this->schema = $schema; |
|
47 | 73 | $this->validateSchema(); |
|
48 | |||
49 | 73 | $this->introduceIntrospectionFields(); |
|
50 | |||
51 | 73 | $this->typeFieldLookupTable = []; |
|
52 | 73 | } |
|
53 | |||
54 | /** |
||
55 | * @param AbstractObjectType $type |
||
56 | * @param string $fieldName |
||
57 | * |
||
58 | * @return Field |
||
59 | */ |
||
60 | 67 | public function getField(AbstractObjectType $type, $fieldName) |
|
61 | { |
||
62 | 67 | $typeName = $type->getName(); |
|
63 | |||
64 | 67 | if (!array_key_exists($typeName, $this->typeFieldLookupTable)) { |
|
65 | 67 | $this->typeFieldLookupTable[$typeName] = []; |
|
66 | 67 | } |
|
67 | |||
68 | 67 | if (!array_key_exists($fieldName, $this->typeFieldLookupTable[$typeName])) { |
|
69 | 67 | $this->typeFieldLookupTable[$typeName][$fieldName] = $type->getField($fieldName); |
|
70 | 67 | } |
|
71 | |||
72 | 67 | return $this->typeFieldLookupTable[$typeName][$fieldName]; |
|
73 | } |
||
74 | |||
75 | 73 | protected function validateSchema() |
|
76 | { |
||
77 | try { |
||
78 | 73 | (new SchemaValidator())->validate($this->schema); |
|
79 | 73 | } catch (\Exception $e) { |
|
80 | 1 | $this->addError($e); |
|
81 | }; |
||
82 | 73 | } |
|
83 | |||
84 | 73 | protected function introduceIntrospectionFields() |
|
85 | { |
||
86 | 73 | $schemaField = new SchemaField(); |
|
87 | 73 | $this->schema->addQueryField($schemaField); |
|
88 | 73 | $this->schema->addQueryField(new TypeDefinitionField()); |
|
89 | 73 | } |
|
90 | |||
91 | /** |
||
92 | * @return AbstractSchema |
||
93 | */ |
||
94 | 67 | public function getSchema() |
|
98 | |||
99 | /** |
||
100 | * @param AbstractSchema $schema |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function setSchema(AbstractSchema $schema) |
||
110 | |||
111 | /** |
||
112 | * @return Request |
||
113 | */ |
||
114 | 67 | public function getRequest() |
|
118 | |||
119 | /** |
||
120 | * @param Request $request |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | 67 | public function setRequest(Request $request) |
|
130 | |||
131 | public function get($id) |
||
135 | |||
136 | /** |
||
137 | * @return ContainerInterface |
||
138 | */ |
||
139 | 1 | public function getContainer() |
|
143 | |||
144 | /** |
||
145 | * @param ContainerInterface $container |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | 70 | public function setContainer(ContainerInterface $container) |
|
155 | } |
||
156 |