1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_twig; |
4
|
|
|
|
5
|
|
|
use Twig_Environment; |
6
|
|
|
use Twig_Node; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Scans a Twig template for query fragments and references to other templates. |
10
|
|
|
*/ |
11
|
|
|
class GraphQLNodeVisitor extends \Twig_BaseNodeVisitor { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The query string. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $query = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The parent template identifier. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $parent = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A list of referenced templates (include, embed). |
29
|
|
|
* |
30
|
|
|
* @var string[][] |
31
|
|
|
*/ |
32
|
|
|
protected $includes = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function getPriority() { |
38
|
|
|
return 0; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function doEnterNode(Twig_Node $node, Twig_Environment $env) { |
45
|
|
|
|
46
|
|
|
if ($node instanceof \Twig_Node_Module) { |
|
|
|
|
47
|
|
|
|
48
|
|
|
// If there is a parent node (created by `extends` or `embed`), |
49
|
|
|
// store it's identifier. |
50
|
|
|
if ($node->hasNode('parent')) { |
51
|
|
|
$parent = $node->getNode('parent'); |
52
|
|
|
if ($parent instanceof \Twig_Node_Expression_Constant) { |
|
|
|
|
53
|
|
|
$this->parent = $parent->getAttribute('value'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Recurse into embedded templates. |
58
|
|
|
foreach ($node->getAttribute('embedded_templates') as $embed) { |
59
|
|
|
$this->doEnterNode($embed, $env); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Store identifiers of any static includes. |
64
|
|
|
// There is no way to make this work for dynamic includes. |
65
|
|
|
if ($node instanceof \Twig_Node_Include && !($node instanceof \Twig_Node_Embed)) { |
|
|
|
|
66
|
|
|
$ref = $node->getNode('expr'); |
67
|
|
|
if ($ref instanceof \Twig_Node_Expression_Constant) { |
|
|
|
|
68
|
|
|
$this->includes[$node->getTemplateName()][] = $ref->getAttribute('value'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// When encountering a GraphQL fragment, add it to the current query. |
73
|
|
|
if ($node instanceof GraphQLFragmentNode) { |
74
|
|
|
$this->query .= $node->fragment; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $node; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
protected function doLeaveNode(Twig_Node $node, Twig_Environment $env) { |
84
|
|
|
if ($node instanceof \Twig_Node_Module) { |
|
|
|
|
85
|
|
|
// Store current query information to be compiled into the templates |
86
|
|
|
// `class_end`. |
87
|
|
|
$includes = isset($this->includes[$node->getTemplateName()]) ? $this->includes[$node->getTemplateName()] : []; |
88
|
|
|
$node->setNode('class_end', new GraphQLNode($this->query, $this->parent, $includes)); |
89
|
|
|
|
90
|
|
|
// Reset query information for the next module. |
91
|
|
|
$this->query = ''; |
92
|
|
|
$this->parent = ''; |
93
|
|
|
} |
94
|
|
|
return $node; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
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.