1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpToZephir\Converter; |
4
|
|
|
|
5
|
|
|
use PhpToZephir\Logger; |
6
|
|
|
use PhpToZephir\ClassCollector; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @method string pExpr_Assign(\PhpParser\Node\Expr\Assign $node) |
10
|
|
|
* @method string pImplode(array $nodes, string $glue = '') |
11
|
|
|
* @method string pVarOrNewExpr(\PhpParser\Node $node) |
12
|
|
|
* @method string pCommaSeparated(array $nodes) |
13
|
|
|
* @method string pInfixOp(string $type, \PhpParser\Node $leftNode, string $operatorString, \PhpParser\Node $rightNode) |
14
|
|
|
* @method string pPrec(\PhpParser\Node $node, integer $parentPrecedence, integer $parentAssociativity, integer $childPosition) |
|
|
|
|
15
|
|
|
* @method string pExpr_Ternary(\PhpParser\Node\Expr\Ternary $node, boolean $returnAsArray = false) |
16
|
|
|
* @method string pStmts(array $nodes, boolean $indent = true) |
17
|
|
|
* @method string pModifiers(integer $modifiers) |
18
|
|
|
* @method string|array pExpr_Array(\PhpParser\Node\Expr\Array_ $node, boolean $returnAsArray = false) |
19
|
|
|
* @method string pExpr_ArrayDimFetch(\PhpParser\Node\Expr\ArrayDimFetch $node, $returnAsArray = false) |
20
|
|
|
* @method string pObjectProperty($node) |
21
|
|
|
* @method string pPrefixOp(string $type, string $operatorString, \PhpParser\Node $node) |
22
|
|
|
* @method string pEncapsList(array $encapsList, string $quote) |
23
|
|
|
* @method string pPostfixOp(string $type, \PhpParser\Node $node, string $operatorString) |
24
|
|
|
* @method string pStmt_ElseIf(\PhpParser\Node\Stmt\ElseIf_ $node) |
25
|
|
|
* @method string pStmt_If(\PhpParser\Node\Stmt\If_ $node) |
26
|
|
|
*/ |
27
|
|
|
class Dispatcher |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const noIndentToken = '_NO_INDENT_852452555255254554'; |
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $precedenceMap = array(); |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $classes = array(); |
41
|
|
|
/** |
42
|
|
|
* @var PrinterCollection |
43
|
|
|
*/ |
44
|
|
|
private $printerCollection = null; |
45
|
|
|
/** |
46
|
|
|
* @var Logger |
47
|
|
|
*/ |
48
|
|
|
private $logger = null; |
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $lastMethod = null; |
53
|
|
|
/** |
54
|
|
|
* @var ClassMetadata |
55
|
|
|
*/ |
56
|
|
|
private $metadata = null; |
57
|
|
|
/** |
58
|
|
|
* @var ClassCollector |
59
|
|
|
*/ |
60
|
|
|
private $classCollector = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param PrinterCollection $printerCollection |
64
|
|
|
* @param array $precedenceMap |
65
|
|
|
*/ |
66
|
1 |
|
public function __construct(PrinterCollection $printerCollection, array $precedenceMap) |
67
|
|
|
{ |
68
|
1 |
|
$this->printerCollection = $printerCollection; |
69
|
1 |
|
$this->precedenceMap = $precedenceMap; |
|
|
|
|
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Pretty prints a node. |
74
|
|
|
* |
75
|
|
|
* @param \PhpParser\Node $node Node to be pretty printed |
76
|
|
|
* |
77
|
|
|
* @return string Pretty printed node |
78
|
|
|
*/ |
79
|
81 |
|
public function p() |
|
|
|
|
80
|
|
|
{ |
81
|
81 |
|
$args = func_get_args(); |
82
|
81 |
|
$node = $args[0]; |
83
|
|
|
|
84
|
81 |
|
if (null === $node) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
81 |
|
$this->logger->trace('p'.$node->getType(), $node, $this->getMetadata()->getFullQualifiedNameClass()); |
89
|
|
|
|
90
|
81 |
|
$class = $this->getClass('p'.$node->getType()); |
91
|
|
|
|
92
|
81 |
|
return call_user_func_array(array($class, "convert"), $args); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $method |
97
|
|
|
* @param array $arguments |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
81 |
|
public function __call($method, $arguments) |
102
|
|
|
{ |
103
|
81 |
|
return call_user_func_array(array($this->getClass($method), 'convert'), $arguments); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $type |
108
|
|
|
* |
109
|
|
|
* @throws \Exception |
110
|
|
|
* |
111
|
|
|
* @return object |
112
|
|
|
*/ |
113
|
81 |
|
private function getClass($type) |
114
|
|
|
{ |
115
|
81 |
|
if (isset($this->classes[$type]) === false) { |
116
|
25 |
|
if ($this->printerCollection->offsetExists($type) === false) { |
117
|
|
|
throw new \Exception(sprintf('Printer "%s" does not exist', $type)); |
118
|
|
|
} |
119
|
25 |
|
$className = $this->printerCollection->offsetGet($type); |
120
|
|
|
|
121
|
25 |
|
$this->classes[$type] = $this->dynamicConstruct($className); |
122
|
25 |
|
} |
123
|
|
|
|
124
|
81 |
|
return $this->classes[$type]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $value |
129
|
|
|
*/ |
130
|
79 |
|
public function setLastMethod($value) |
131
|
|
|
{ |
132
|
79 |
|
$this->lastMethod = $value; |
133
|
79 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
6 |
|
public function getLastMethod() |
139
|
|
|
{ |
140
|
6 |
|
return $this->lastMethod; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $className |
145
|
|
|
* |
146
|
|
|
* @return object |
147
|
|
|
*/ |
148
|
25 |
|
private function dynamicConstruct($className) |
149
|
|
|
{ |
150
|
25 |
|
$reflectionClass = new \ReflectionClass($className); |
151
|
|
|
|
152
|
25 |
|
if ($reflectionClass->getConstructor() === null) { |
153
|
5 |
|
return new $className(); |
154
|
|
|
} |
155
|
25 |
|
$dependencies = array(); |
156
|
|
|
|
157
|
25 |
|
foreach ($reflectionClass->getConstructor()->getParameters() as $nmb => $param) { |
158
|
25 |
|
$name = $param->getClass()->name; |
159
|
|
|
|
160
|
25 |
|
if ($name === 'PhpToZephir\Converter\Dispatcher') { |
161
|
25 |
|
$dependencies[] = $this; |
162
|
25 |
|
} elseif ($name === 'PhpToZephir\Logger') { |
163
|
25 |
|
$dependencies[] = $this->logger; |
164
|
25 |
|
} elseif ($name === 'PhpToZephir\ClassCollector') { |
165
|
1 |
|
$dependencies[] = $this->classCollector; |
166
|
1 |
|
} else { |
167
|
5 |
|
$dependencies[] = $this->dynamicConstruct($name); |
168
|
|
|
} |
169
|
25 |
|
} |
170
|
|
|
|
171
|
25 |
|
return $reflectionClass->newInstanceArgs($dependencies); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $type |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
64 |
|
public function getPrecedenceMap($type) |
180
|
|
|
{ |
181
|
64 |
|
return $this->precedenceMap[$type]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $type |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
64 |
|
public function issetPrecedenceMap($type) |
190
|
|
|
{ |
191
|
64 |
|
return isset($this->precedenceMap[$type]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Pretty prints an array of statements. |
196
|
|
|
* |
197
|
|
|
* @param \PhpParser\Node[] $stmts Array of statements |
198
|
|
|
* |
199
|
|
|
* @return string Pretty printed statements |
200
|
|
|
*/ |
201
|
81 |
|
public function convert(array $stmts, ClassMetadata $metadata, ClassCollector $classCollector, Logger $logger) |
202
|
|
|
{ |
203
|
81 |
|
$this->metadata = $metadata; |
|
|
|
|
204
|
81 |
|
$this->classCollector = $classCollector; |
205
|
81 |
|
$this->logger = $logger; |
|
|
|
|
206
|
|
|
|
207
|
81 |
|
return ltrim(str_replace("\n".self::noIndentToken, "\n", $this->pStmts($stmts, false))); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return ClassMetadata |
212
|
|
|
*/ |
213
|
81 |
|
public function getMetadata() |
214
|
|
|
{ |
215
|
81 |
|
return $this->metadata; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.