|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
namespace PHPSA; |
|
8
|
|
|
|
|
9
|
|
|
use PHPSA\Analyzer\EventListener\ExpressionListener; |
|
10
|
|
|
use PHPSA\Analyzer\EventListener\ScalarListener; |
|
11
|
|
|
use PHPSA\Analyzer\EventListener\StatementListener; |
|
12
|
|
|
use PHPSA\Analyzer\Pass\AnalyzerPassInterface; |
|
13
|
|
|
use Webiny\Component\EventManager\EventManager; |
|
14
|
|
|
|
|
15
|
|
|
class Analyzer |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EventManager |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $eventManager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var []AnalyzerPassInterface[] |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $bindOnExpressions = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var []AnalyzerPassInterface[] |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $bindOnStatements = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var []AnalyzerPassInterface[] |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $bindOnScalars = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param EventManager $eventManager |
|
39
|
|
|
*/ |
|
40
|
43 |
|
public function __construct(EventManager $eventManager) |
|
41
|
|
|
{ |
|
42
|
43 |
|
$this->eventManager = $eventManager; |
|
43
|
43 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array $expressionPasses all the expression analyzers |
|
47
|
|
|
* @throws \RuntimeException if the analyzer does not implement the required interface |
|
48
|
|
|
*/ |
|
49
|
43 |
|
public function registerExpressionPasses(array $expressionPasses) |
|
50
|
|
|
{ |
|
51
|
43 |
|
foreach ($expressionPasses as $pass) { |
|
52
|
22 |
|
if (!$pass instanceof AnalyzerPassInterface) { |
|
53
|
|
|
throw new \RuntimeException('Analyzer pass must implement AnalyzerPassInterface'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
22 |
|
$bindOnExpressions = $pass->getRegister(); |
|
57
|
22 |
|
foreach ($bindOnExpressions as $bindOnExpression) { |
|
58
|
22 |
|
if (isset($this->bindOnExpressions[$bindOnExpression])) { |
|
59
|
|
|
$this->bindOnExpressions[$bindOnExpression][] = $pass; |
|
60
|
|
|
} else { |
|
61
|
22 |
|
$this->bindOnExpressions[$bindOnExpression] = [$pass]; |
|
62
|
|
|
} |
|
63
|
22 |
|
} |
|
64
|
43 |
|
} |
|
65
|
43 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $statementPasses all the statement analyzers |
|
69
|
|
|
* @throws \RuntimeException if the analyzer does not implement the required interface |
|
70
|
|
|
*/ |
|
71
|
43 |
|
public function registerStatementPasses(array $statementPasses) |
|
72
|
|
|
{ |
|
73
|
43 |
|
foreach ($statementPasses as $pass) { |
|
74
|
20 |
|
if (!$pass instanceof AnalyzerPassInterface) { |
|
75
|
|
|
throw new \RuntimeException('Analyzer pass must implement AnalyzerPassInterface'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
20 |
|
$bindOnStatements = $pass->getRegister(); |
|
79
|
20 |
|
foreach ($bindOnStatements as $bindOnStatement) { |
|
80
|
20 |
|
if (isset($this->bindOnStatements[$bindOnStatement])) { |
|
81
|
|
|
$this->bindOnStatements[$bindOnStatement][] = $pass; |
|
82
|
|
|
} else { |
|
83
|
20 |
|
$this->bindOnStatements[$bindOnStatement] = [$pass]; |
|
84
|
|
|
} |
|
85
|
20 |
|
} |
|
86
|
43 |
|
} |
|
87
|
43 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param array $scalarPasses all the scalar analyzers |
|
91
|
|
|
* @throws \RuntimeException if the analyzer does not implement the required interface |
|
92
|
|
|
*/ |
|
93
|
43 |
|
public function registerScalarPasses(array $scalarPasses) |
|
94
|
|
|
{ |
|
95
|
43 |
|
foreach ($scalarPasses as $pass) { |
|
96
|
1 |
|
if (!$pass instanceof AnalyzerPassInterface) { |
|
97
|
|
|
throw new \RuntimeException('Analyzer pass must implement AnalyzerPassInterface'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
$bindOnScalars = $pass->getRegister(); |
|
101
|
1 |
|
foreach ($bindOnScalars as $bindOnScalar) { |
|
102
|
1 |
|
if (isset($this->bindOnScalars[$bindOnScalar])) { |
|
103
|
|
|
$this->bindOnScalars[$bindOnScalar][] = $pass; |
|
104
|
|
|
} else { |
|
105
|
1 |
|
$this->bindOnScalars[$bindOnScalar] = [$pass]; |
|
106
|
|
|
} |
|
107
|
1 |
|
} |
|
108
|
43 |
|
} |
|
109
|
43 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* binds the listeners |
|
113
|
|
|
*/ |
|
114
|
43 |
|
public function bind() |
|
115
|
|
|
{ |
|
116
|
43 |
|
$this->eventManager->listen(Compiler\Event\ExpressionBeforeCompile::EVENT_NAME) |
|
117
|
43 |
|
->handler( |
|
118
|
43 |
|
new ExpressionListener($this->bindOnExpressions) |
|
119
|
43 |
|
) |
|
120
|
43 |
|
->method('beforeCompile'); |
|
121
|
|
|
|
|
122
|
43 |
|
$this->eventManager->listen(Compiler\Event\StatementBeforeCompile::EVENT_NAME) |
|
123
|
43 |
|
->handler( |
|
124
|
43 |
|
new StatementListener($this->bindOnStatements) |
|
125
|
43 |
|
) |
|
126
|
43 |
|
->method('beforeCompile'); |
|
127
|
|
|
|
|
128
|
43 |
|
$this->eventManager->listen(Compiler\Event\ScalarBeforeCompile::EVENT_NAME) |
|
129
|
43 |
|
->handler( |
|
130
|
43 |
|
new ScalarListener($this->bindOnScalars) |
|
131
|
43 |
|
) |
|
132
|
43 |
|
->method('beforeCompile'); |
|
133
|
43 |
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|