1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_twig; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait that will be attached to all GraphQL enabled Twig templates. |
7
|
|
|
*/ |
8
|
|
|
trait GraphQLTemplateTrait { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Recursively build the GraphQL query. |
12
|
|
|
* |
13
|
|
|
* Builds the templates GraphQL query by iterating through all included or |
14
|
|
|
* embedded templates recursively. |
15
|
|
|
*/ |
16
|
|
|
public function getGraphQLQuery() { |
17
|
|
|
|
18
|
|
|
$query = ''; |
19
|
|
|
$includes = []; |
20
|
|
|
|
21
|
|
|
if ($this instanceof \Twig_Template) { |
|
|
|
|
22
|
|
|
$query = $this->getGraphQLFragment(); |
23
|
|
|
|
24
|
|
|
$includes = array_keys($this->getGraphQLIncludes()); |
25
|
|
|
|
26
|
|
|
// Recursively collect all included fragments. |
27
|
|
|
$includes = array_map(function ($template) { |
28
|
|
|
return $this->loadTemplate($template)->getGraphQLFragment(); |
|
|
|
|
29
|
|
|
}, $includes); |
30
|
|
|
|
31
|
|
|
// Always add includes from parent templates. |
32
|
|
|
if ($parent = $this->getGraphQLParent()) { |
33
|
|
|
$includes += array_map(function ($template) { |
34
|
|
|
return $this->loadTemplate($template)->getGraphQLQuery(); |
|
|
|
|
35
|
|
|
}, array_keys($parent->getGraphQLIncludes())); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
return implode("\n", [-1 => $query] + $includes); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the files parent template. |
45
|
|
|
* |
46
|
|
|
* @return \Twig_Template|null |
47
|
|
|
* The parent template or null. |
48
|
|
|
*/ |
49
|
|
|
protected function getGraphQLParent() { |
50
|
|
|
return $this->graphqlParent ? $this->loadTemplate($this->graphqlParent) : NULL; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieve the files graphql fragment. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
* The GraphQL fragment. |
58
|
|
|
*/ |
59
|
|
|
public function getGraphQLFragment() { |
60
|
|
|
$query = ''; |
61
|
|
|
// If there is no query for this template, try to get one from the |
62
|
|
|
// parent template. |
63
|
|
|
if ($this->graphqlQuery) { |
64
|
|
|
$query = $this->graphqlQuery; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
elseif ($parent = $this->getGraphQLParent()) { |
67
|
|
|
$query = $parent->getGraphQLFragment(); |
68
|
|
|
} |
69
|
|
|
return $query; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieve a list of all direct or indirect included templates. |
74
|
|
|
* |
75
|
|
|
* @return string[] |
76
|
|
|
* The list of included templates. |
77
|
|
|
*/ |
78
|
|
|
public function getGraphQLIncludes() { |
79
|
|
|
$includes = array_flip($this->graphqlIncludes); |
|
|
|
|
80
|
|
|
if ($includes) { |
|
|
|
|
81
|
|
|
foreach ($includes as $include => $key) { |
82
|
|
|
$includes += $this->loadTemplate($include)->getGraphQLIncludes(); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return $includes; |
86
|
|
|
} |
87
|
|
|
} |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.