1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Compiler; |
7
|
|
|
|
8
|
|
|
use PHPSA\Context; |
9
|
|
|
use PhpParser\Node; |
10
|
|
|
use PHPSA\Compiler\Statement\AbstractCompiler; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
class Statement |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param Node\Stmt $stmt |
17
|
|
|
* @return AbstractCompiler |
18
|
|
|
*/ |
19
|
|
|
protected function factory($stmt) |
20
|
|
|
{ |
21
|
|
|
switch (get_class($stmt)) { |
22
|
|
|
case 'PhpParser\Node\Stmt\Return_': |
23
|
|
|
return new Statement\ReturnSt(); |
24
|
|
|
case 'PhpParser\Node\Stmt\While_': |
25
|
|
|
return new Statement\WhileSt(); |
26
|
|
|
case 'PhpParser\Node\Stmt\Switch_': |
27
|
|
|
return new Statement\SwitchSt(); |
28
|
|
|
case 'PhpParser\Node\Stmt\If_': |
29
|
|
|
return new Statement\IfSt(); |
30
|
|
|
case 'PhpParser\Node\Stmt\Do_': |
31
|
|
|
return new Statement\DoSt(); |
32
|
|
|
case 'PhpParser\Node\Stmt\For_': |
33
|
|
|
return new Statement\ForSt(); |
34
|
|
|
case 'PhpParser\Node\Stmt\Foreach_': |
35
|
|
|
return new Statement\ForeachSt(); |
36
|
|
|
case 'PhpParser\Node\Stmt\TryCatch': |
37
|
|
|
return new Statement\TryCatchSt(); |
38
|
|
|
case Node\Stmt\Catch_::class: |
39
|
|
|
return new Statement\CatchSt(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
throw new RuntimeException('Unknown statement: ' . get_class($stmt)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Node\Stmt $stmt |
47
|
|
|
* @param Context $context |
48
|
|
|
*/ |
49
|
|
|
public function __construct(Node\Stmt $stmt, Context $context) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
/** |
53
|
|
|
* Dont show an error |
54
|
|
|
* @todo This a little bit more about own statement for break; |
55
|
|
|
*/ |
56
|
|
|
if (get_class($stmt) == 'PhpParser\Node\Stmt\Break_') { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$context->getEventManager()->fire( |
61
|
|
|
Event\StatementBeforeCompile::EVENT_NAME, |
62
|
|
|
new Event\StatementBeforeCompile( |
63
|
|
|
$stmt, |
64
|
|
|
$context |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$compiler = $this->factory($stmt); |
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
$context->debug('StatementCompiler is not implemented for ' . get_class($stmt)); |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$compiler->pass($stmt, $context); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|