1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\ExpressionLanguage; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
8
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage; |
9
|
|
|
use Symfony\Component\ExpressionLanguage\Lexer; |
10
|
|
|
use Symfony\Component\ExpressionLanguage\SyntaxError; |
11
|
|
|
use Symfony\Component\ExpressionLanguage\Token; |
12
|
|
|
use function array_merge; |
13
|
|
|
use function strlen; |
14
|
|
|
use function strpos; |
15
|
|
|
use function substr; |
16
|
|
|
|
17
|
|
|
class ExpressionLanguage extends BaseExpressionLanguage |
18
|
|
|
{ |
19
|
|
|
// TODO (murtukov): make names conditional |
20
|
|
|
public const KNOWN_NAMES = ['value', 'args', 'context', 'info', 'object', 'validator', 'errors', 'childrenComplexity', 'typeName', 'fieldName']; |
21
|
|
|
public const EXPRESSION_TRIGGER = '@='; |
22
|
|
|
|
23
|
|
|
public array $globalNames = []; |
24
|
|
|
|
25
|
144 |
|
public function addGlobalName(string $index, string $name): void |
26
|
|
|
{ |
27
|
144 |
|
$this->globalNames[$index] = $name; |
28
|
144 |
|
} |
29
|
|
|
|
30
|
16 |
|
public function getGlobalNames(): array |
31
|
|
|
{ |
32
|
16 |
|
return array_values($this->globalNames); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string|Expression $expression |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
53 |
|
public function compile($expression, array $names = []) |
41
|
|
|
{ |
42
|
53 |
|
return parent::compile($expression, array_merge($names, $this->globalNames)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Checks if expression string containst specific variable. |
47
|
|
|
* |
48
|
|
|
* Argument can be either an Expression object or a string with or |
49
|
|
|
* without a prefix |
50
|
|
|
* |
51
|
|
|
* @param string $name - name of the searched variable (needle) |
52
|
|
|
* @param string|Expression $expression - expression to search in (haystack) |
53
|
|
|
* |
54
|
|
|
* @throws SyntaxError |
55
|
|
|
*/ |
56
|
38 |
|
public static function expressionContainsVar(string $name, $expression): bool |
57
|
|
|
{ |
58
|
38 |
|
foreach (static::extractExpressionVarNames($expression) as $varName) { |
59
|
33 |
|
if ($name === $varName) { |
60
|
13 |
|
return true; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
33 |
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string|Expression $expression - expression to search in (haystack) |
69
|
|
|
* |
70
|
|
|
* @throws SyntaxError |
71
|
|
|
*/ |
72
|
59 |
|
public static function extractExpressionVarNames($expression): iterable |
73
|
|
|
{ |
74
|
59 |
|
if ($expression instanceof Expression) { |
75
|
2 |
|
$expression = $expression->__toString(); |
76
|
57 |
|
} elseif (self::stringHasTrigger($expression)) { |
77
|
32 |
|
$expression = self::unprefixExpression($expression); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** @var string $expression */ |
81
|
59 |
|
$stream = (new Lexer())->tokenize($expression); |
82
|
59 |
|
$current = &$stream->current; |
83
|
59 |
|
$isProperty = false; |
84
|
59 |
|
$varNames = []; |
85
|
|
|
|
86
|
59 |
|
while (!$stream->isEOF()) { |
87
|
59 |
|
if ('.' === $current->value) { |
88
|
28 |
|
$isProperty = true; |
89
|
59 |
|
} elseif (Token::NAME_TYPE === $current->type) { |
90
|
59 |
|
if (!$isProperty) { |
91
|
59 |
|
$name = $current->value; |
92
|
|
|
// Also check that it's not a function's name |
93
|
59 |
|
$stream->next(); |
94
|
59 |
|
if ('(' !== $current->value) { |
95
|
53 |
|
$varNames[] = $name; |
96
|
|
|
} |
97
|
59 |
|
continue; |
98
|
|
|
} else { |
99
|
28 |
|
$isProperty = false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
58 |
|
$stream->next(); |
104
|
|
|
} |
105
|
|
|
|
106
|
59 |
|
return $varNames; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Checks if value is a string and has the expression trigger prefix. |
111
|
|
|
* |
112
|
|
|
* @param mixed $value |
113
|
|
|
*/ |
114
|
37 |
|
public static function isStringWithTrigger($value): bool |
115
|
|
|
{ |
116
|
37 |
|
if (is_string($value)) { |
117
|
37 |
|
return self::stringHasTrigger($value); |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Checks if a string has the expression trigger prefix. |
125
|
|
|
*/ |
126
|
65 |
|
public static function stringHasTrigger(string $maybeExpression): bool |
127
|
|
|
{ |
128
|
65 |
|
return 0 === strpos($maybeExpression, self::EXPRESSION_TRIGGER); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Removes the expression trigger prefix from a string. If no prefix found, |
133
|
|
|
* returns the initial string. |
134
|
|
|
* |
135
|
|
|
* @param string $expression - String expression with a trigger prefix |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
32 |
|
public static function unprefixExpression(string $expression) |
140
|
|
|
{ |
141
|
32 |
|
$string = substr($expression, strlen(self::EXPRESSION_TRIGGER)); |
142
|
|
|
|
143
|
32 |
|
return '' !== $string ? $string : $expression; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|