|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPSA\Compiler; |
|
4
|
|
|
|
|
5
|
|
|
use PHPSA\CompiledExpression; |
|
6
|
|
|
use PHPSA\Context; |
|
7
|
|
|
use PhpParser\Node; |
|
8
|
|
|
use PHPSA\Compiler; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Webiny\Component\EventManager\EventManager; |
|
11
|
|
|
|
|
12
|
|
|
class Scalar |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Context |
|
16
|
|
|
*/ |
|
17
|
|
|
private $context; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var EventManager |
|
21
|
|
|
*/ |
|
22
|
|
|
private $eventManager; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Context $context |
|
26
|
|
|
* @param EventManager $eventManager |
|
27
|
|
|
*/ |
|
28
|
817 |
|
public function __construct(Context $context, EventManager $eventManager) |
|
29
|
|
|
{ |
|
30
|
817 |
|
$this->context = $context; |
|
31
|
817 |
|
$this->eventManager = $eventManager; |
|
32
|
817 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param Node\Scalar $scalar |
|
36
|
|
|
* @return CompiledExpression |
|
37
|
|
|
*/ |
|
38
|
817 |
|
public function compile(Node\Scalar $scalar) |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
817 |
|
$this->eventManager->fire( |
|
42
|
817 |
|
Event\ScalarBeforeCompile::EVENT_NAME, |
|
43
|
817 |
|
new Event\ScalarBeforeCompile( |
|
44
|
817 |
|
$scalar, |
|
45
|
817 |
|
$this->context |
|
46
|
817 |
|
) |
|
47
|
817 |
|
); |
|
48
|
|
|
|
|
49
|
817 |
|
return $this->factory($scalar); |
|
50
|
|
|
} catch (\Exception $e) { |
|
51
|
|
|
$this->context->debug('ScalarCompiler is not implemented for ' . get_class($scalar)); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Node\Scalar $scalar |
|
57
|
|
|
* @return CompiledExpression |
|
58
|
|
|
* @throws RuntimeException |
|
59
|
|
|
*/ |
|
60
|
817 |
|
protected function factory(Node\Scalar $scalar) |
|
61
|
|
|
{ |
|
62
|
817 |
|
switch (get_class($scalar)) { |
|
63
|
|
|
// Native Scalars |
|
64
|
817 |
|
case Node\Scalar\LNumber::class: |
|
65
|
528 |
|
return new CompiledExpression(CompiledExpression::INTEGER, $scalar->value); |
|
|
|
|
|
|
66
|
561 |
|
case Node\Scalar\DNumber::class: |
|
67
|
213 |
|
return new CompiledExpression(CompiledExpression::DOUBLE, $scalar->value); |
|
68
|
384 |
|
case Node\Scalar\String_::class: |
|
69
|
54 |
|
return new CompiledExpression(CompiledExpression::STRING, $scalar->value); |
|
70
|
|
|
|
|
71
|
|
|
// @todo Review this and implement support |
|
72
|
339 |
|
case Node\Scalar\EncapsedStringPart::class: |
|
73
|
|
|
return new CompiledExpression(CompiledExpression::STRING); |
|
74
|
339 |
|
case Node\Scalar\Encapsed::class: |
|
75
|
|
|
return $this->compileUncapsedString($scalar); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Fake scalars for testing |
|
79
|
|
|
*/ |
|
80
|
339 |
|
case \PHPSA\Node\Scalar\Nil::class: |
|
81
|
25 |
|
return new CompiledExpression(CompiledExpression::NULL); |
|
82
|
330 |
|
case \PHPSA\Node\Scalar\Boolean::class: |
|
83
|
241 |
|
return new CompiledExpression(CompiledExpression::BOOLEAN, $scalar->value); |
|
84
|
89 |
|
case \PHPSA\Node\Scalar\Fake::class: |
|
85
|
88 |
|
return new CompiledExpression($scalar->type, $scalar->value); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* MagicConst |
|
89
|
|
|
* @todo Review this and implement support, now We return only type |
|
90
|
|
|
*/ |
|
91
|
1 |
|
case Node\Scalar\MagicConst\Line::class: |
|
92
|
1 |
|
return new CompiledExpression(CompiledExpression::INTEGER, $scalar->getAttribute('startLine')); |
|
93
|
|
|
case Node\Scalar\MagicConst\Trait_::class: |
|
94
|
|
|
return new CompiledExpression(CompiledExpression::STRING); |
|
95
|
|
|
case Node\Scalar\MagicConst\Namespace_::class: |
|
96
|
|
|
return new CompiledExpression(CompiledExpression::STRING); |
|
97
|
|
|
case Node\Scalar\MagicConst\Class_::class: |
|
98
|
|
|
return new CompiledExpression(CompiledExpression::STRING); |
|
99
|
|
|
case Node\Scalar\MagicConst\Dir::class: |
|
100
|
|
|
return new CompiledExpression(CompiledExpression::STRING); |
|
101
|
|
|
case Node\Scalar\MagicConst\File::class: |
|
102
|
|
|
return new CompiledExpression(CompiledExpression::STRING); |
|
103
|
|
|
case Node\Scalar\MagicConst\Function_::class: |
|
104
|
|
|
return new CompiledExpression(CompiledExpression::STRING); |
|
105
|
|
|
case Node\Scalar\MagicConst\Method::class: |
|
106
|
|
|
return new CompiledExpression(CompiledExpression::STRING); |
|
107
|
|
|
|
|
108
|
|
|
default: |
|
109
|
|
|
throw new RuntimeException('Unknown scalar: ' . get_class($scalar)); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param Node\Scalar\Encapsed $scalar |
|
115
|
|
|
* @return CompiledExpression |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function compileUncapsedString(Node\Scalar\Encapsed $scalar) |
|
118
|
|
|
{ |
|
119
|
|
|
foreach ($scalar->parts as $part) { |
|
120
|
|
|
$this->context->getExpressionCompiler()->compile($part); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return new CompiledExpression(CompiledExpression::STRING); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.