|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PHPSA; |
|
7
|
|
|
|
|
8
|
|
|
use PHPSA\Definition\ClassDefinition; |
|
9
|
|
|
use PHPSA\Definition\FunctionDefinition; |
|
10
|
|
|
use PHPSA\Definition\RuntimeClassDefinition; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
|
|
13
|
|
|
class Compiler |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ClassDefinition[] |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $classes = array(); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var FunctionDefinition[] |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $functions = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param ClassDefinition $class |
|
27
|
|
|
*/ |
|
28
|
10 |
|
public function addClass(ClassDefinition $class) |
|
29
|
|
|
{ |
|
30
|
10 |
|
$this->classes[implode('\\', [$class->getNamespace(), $class->getName()])] = $class; |
|
31
|
10 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param FunctionDefinition $function |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function addFunction(FunctionDefinition $function) |
|
37
|
|
|
{ |
|
38
|
1 |
|
$this->functions[] = $function; |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Context $context |
|
43
|
|
|
*/ |
|
44
|
10 |
|
public function compile(Context $context) |
|
45
|
|
|
{ |
|
46
|
10 |
|
$context->scopePointer = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @todo Implement class map... |
|
50
|
|
|
*/ |
|
51
|
10 |
|
foreach ($this->classes as $class) { |
|
52
|
10 |
|
$extends = $class->getExtendsClass(); |
|
53
|
10 |
|
if ($extends) { |
|
|
|
|
|
|
54
|
|
|
if (isset($this->classes[$extends])) { |
|
55
|
|
|
$class->setExtendsClassDefinition($this->classes[$extends]); |
|
56
|
|
|
} else { |
|
57
|
|
|
if (class_exists($extends, true)) { |
|
58
|
|
|
$class->setExtendsClassDefinition( |
|
59
|
|
|
new RuntimeClassDefinition( |
|
60
|
|
|
new ReflectionClass( |
|
61
|
|
|
$extends |
|
62
|
|
|
) |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
10 |
|
} |
|
69
|
|
|
|
|
70
|
10 |
|
foreach ($this->functions as $function) { |
|
71
|
|
|
/** |
|
72
|
|
|
* @todo Configuration |
|
73
|
|
|
* |
|
74
|
|
|
* Ignore functions compiling from vendor |
|
75
|
|
|
*/ |
|
76
|
1 |
|
$checkVendor = strpos($function->getFilepath(), './vendor'); |
|
77
|
1 |
|
if ($checkVendor !== false && $checkVendor < 3) { |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$function->compile($context); |
|
82
|
10 |
|
} |
|
83
|
|
|
|
|
84
|
10 |
|
foreach ($this->classes as $class) { |
|
85
|
|
|
/** |
|
86
|
|
|
* @todo Configuration |
|
87
|
|
|
* |
|
88
|
|
|
* Ignore Classes compiling from vendor |
|
89
|
|
|
*/ |
|
90
|
10 |
|
$checkVendor = strpos($class->getFilepath(), './vendor'); |
|
91
|
10 |
|
if ($checkVendor !== false && $checkVendor < 3) { |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
10 |
|
$class->compile($context); |
|
96
|
10 |
|
} |
|
97
|
10 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Try to find function with $namespace from pre-compiled function(s) |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $name |
|
103
|
|
|
* @param string|null $namespace |
|
104
|
|
|
* @return bool|FunctionDefinition |
|
105
|
|
|
*/ |
|
106
|
8 |
|
public function getFunctionNS($name, $namespace = null) |
|
107
|
|
|
{ |
|
108
|
8 |
|
foreach ($this->functions as $function) { |
|
109
|
1 |
|
if ($function->getName() == $name && $function->getNamespace() == $namespace) { |
|
110
|
|
|
return $function; |
|
111
|
|
|
} |
|
112
|
8 |
|
} |
|
113
|
|
|
|
|
114
|
8 |
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Try to find function from pre-compiled function(s) |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $name |
|
121
|
|
|
* @return bool|FunctionDefinition |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function getFunction($name) |
|
124
|
|
|
{ |
|
125
|
1 |
|
foreach ($this->functions as $function) { |
|
126
|
1 |
|
if ($function->getName() == $name) { |
|
127
|
|
|
return $function; |
|
128
|
|
|
} |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: