1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPRealCoverage\Proxy; |
4
|
|
|
|
5
|
|
|
class ProxyBuilder |
6
|
|
|
{ |
7
|
|
|
private $namespace; |
8
|
|
|
private $className; |
9
|
|
|
private $parentClass; |
10
|
|
|
private $methods = array(); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param string $className |
14
|
|
|
*/ |
15
|
|
|
public function setClassName($className) |
16
|
|
|
{ |
17
|
|
|
$this->className = $className; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $namspace |
22
|
|
|
*/ |
23
|
|
|
public function setNamespace($namspace) |
24
|
|
|
{ |
25
|
|
|
$this->namespace = $namspace; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $parentClass |
30
|
|
|
*/ |
31
|
|
|
public function setParentClass($parentClass) |
32
|
|
|
{ |
33
|
|
|
$this->parentClass = $parentClass; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $method |
38
|
|
|
*/ |
39
|
|
|
public function addMethod($method) |
40
|
|
|
{ |
41
|
|
|
$this->methods[] = $method; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function loadProxy() |
45
|
|
|
{ |
46
|
|
|
$proxyContent = $this->getProxyHeader(); |
47
|
|
|
foreach ($this->methods as $method) { |
48
|
|
|
$proxyContent .= $this->getProxyMethod($method); |
49
|
|
|
} |
50
|
|
|
$proxyContent = $this->getProxyFooter($proxyContent); |
51
|
|
|
|
52
|
|
|
eval($proxyContent); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
private function getProxyHeader() |
59
|
|
|
{ |
60
|
|
|
$proxyContent = " |
61
|
|
|
namespace $this->namespace; |
62
|
|
|
class $this->className extends $this->parentClass implements \\PHPRealCoverage\\Proxy\\Proxy |
63
|
|
|
{ |
64
|
|
|
private static \$instanceClass = '$this->parentClass'; |
65
|
|
|
private \$instance; |
66
|
|
|
private \$constructorArguments; |
67
|
|
|
|
68
|
|
|
public function __construct() |
69
|
|
|
{ |
70
|
|
|
\$this->constructorArguments = func_get_args(); |
71
|
|
|
\$this->__PROXYcreateInstance(\$this->constructorArguments); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function __PROXYcreateInstance(\$constructorArguments) |
75
|
|
|
{ |
76
|
|
|
\$reflectionClass = new \\ReflectionClass(self::\$instanceClass); |
77
|
|
|
\$this->instance = \$reflectionClass->newInstanceArgs(\$constructorArguments); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function setInstanceClass(\$instanceClass) |
81
|
|
|
{ |
82
|
|
|
self::\$instanceClass = \$instanceClass; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function __PROXYcheckInstance() |
86
|
|
|
{ |
87
|
|
|
if (get_class(\$this->instance) !== self::\$instanceClass) { |
88
|
|
|
\$this->__PROXYcreateInstance(\$this->constructorArguments); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
"; |
92
|
|
|
return $proxyContent; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $proxyContent |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
private function getProxyFooter($proxyContent) |
100
|
|
|
{ |
101
|
|
|
$proxyContent .= "}"; |
102
|
|
|
return $proxyContent; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $method |
107
|
|
|
* @internal param $proxyContent |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getProxyMethod($method) |
111
|
|
|
{ |
112
|
|
|
return " |
113
|
|
|
public function $method() |
114
|
|
|
{ |
115
|
|
|
\$this->__PROXYcheckInstance(); |
116
|
|
|
\$reflectionMethod = new \\ReflectionMethod(get_class(\$this->instance), \"$method\"); |
117
|
|
|
return \$reflectionMethod->invokeArgs(\$this->instance, func_get_args()); |
118
|
|
|
} |
119
|
|
|
"; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|