-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
PublicScopeSimulator.php
184 lines (169 loc) Β· 7.36 KB
/
PublicScopeSimulator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/
namespace ProxyManager\ProxyGenerator\Util;
use Zend\Code\Generator\PropertyGenerator;
/**
* Generates code necessary to simulate a fatal error in case of unauthorized
* access to class members in magic methods even when in child classes and dealing
* with protected members.
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
class PublicScopeSimulator
{
const OPERATION_SET = 'set';
const OPERATION_GET = 'get';
const OPERATION_ISSET = 'isset';
const OPERATION_UNSET = 'unset';
/**
* Generates code for simulating access to a property from the scope that is accessing a proxy.
* This is done by introspecting `debug_backtrace()` and then binding a closure to the scope
* of the parent caller.
*
* @param string $operationType operation to execute: one of 'get', 'set', 'isset' or 'unset'
* @param string $nameParameter name of the `name` parameter of the magic method
* @param string|null $valueParameter name of the `value` parameter of the magic method
* @param PropertyGenerator $valueHolder name of the property containing the target object from which
* to read the property. `$this` if none provided
* @param string|null $returnPropertyName name of the property to which we want to assign the result of
* the operation. Return directly if none provided
*
* @return string
*
* @throws \InvalidArgumentException
*/
public static function getPublicAccessSimulationCode(
$operationType,
$nameParameter,
$valueParameter = null,
PropertyGenerator $valueHolder = null,
$returnPropertyName = null
) {
$byRef = self::getByRefReturnValue($operationType);
$value = static::OPERATION_SET === $operationType ? ', $value' : '';
$target = '$this';
if ($valueHolder) {
$target = '$this->' . $valueHolder->getName();
}
return '$realInstanceReflection = new \\ReflectionClass(get_parent_class($this));' . "\n\n"
. 'if (! $realInstanceReflection->hasProperty($' . $nameParameter . ')) {' . "\n"
. ' $targetObject = ' . $target . ';' . "\n\n"
. self::getUndefinedPropertyNotice($operationType, $nameParameter)
. ' ' . self::getOperation($operationType, $nameParameter, $valueParameter) . ";\n"
. " return;\n"
. '}' . "\n\n"
. '$targetObject = ' . self::getTargetObject($valueHolder) . ";\n"
. '$accessor = function ' . $byRef . '() use ($targetObject, $name' . $value . ') {' . "\n"
. ' ' . self::getOperation($operationType, $nameParameter, $valueParameter) . "\n"
. "};\n"
. self::getScopeReBind()
. (
$returnPropertyName
? '$' . $returnPropertyName . ' = ' . $byRef . '$accessor();'
: '$returnValue = ' . $byRef . '$accessor();' . "\n\n" . 'return $returnValue;'
);
}
/**
* This will generate code that triggers a notice if access is attempted on a non-existing property
*
* @param string $operationType
* @param string $nameParameter
*
* @return string
*/
private static function getUndefinedPropertyNotice($operationType, $nameParameter)
{
if (static::OPERATION_GET !== $operationType) {
return '';
}
//
return ' $backtrace = debug_backtrace(false);' . "\n"
. ' trigger_error(\'Undefined property: \' . get_parent_class($this) . \'::$\' . $'
. $nameParameter
. ' . \' in \' . $backtrace[0][\'file\'] . \' on line \' . $backtrace[0][\'line\'], \E_USER_NOTICE);'
. "\n";
}
/**
* Defines whether the given operation produces a reference.
*
* Note: if the object is a wrapper, the wrapped instance is accessed directly. If the object
* is a ghost or the proxy has no wrapper, then an instance of the parent class is created via
* on-the-fly unserialization
*
* @param string $operationType
*
* @return string
*/
private static function getByRefReturnValue($operationType)
{
return (static::OPERATION_GET === $operationType || static::OPERATION_SET === $operationType) ? '& ' : '';
}
/**
* Retrieves the logic to fetch the object on which access should be attempted
*
* @param PropertyGenerator $valueHolder
*
* @return string
*/
private static function getTargetObject(PropertyGenerator $valueHolder = null)
{
if ($valueHolder) {
return '$this->' . $valueHolder->getName();
}
return 'unserialize(sprintf(\'O:%d:"%s":0:{}\', strlen(get_parent_class($this)), get_parent_class($this)))';
}
/**
* @param string $operationType
* @param string $nameParameter
* @param string|null $valueParameter
*
* @return string
*
* @throws \InvalidArgumentException
*/
private static function getOperation($operationType, $nameParameter, $valueParameter)
{
switch ($operationType) {
case static::OPERATION_GET:
return 'return $targetObject->$' . $nameParameter . ";";
case static::OPERATION_SET:
if (! $valueParameter) {
throw new \InvalidArgumentException('Parameter $valueParameter not provided');
}
return 'return $targetObject->$' . $nameParameter . ' = $' . $valueParameter . ';';
case static::OPERATION_ISSET:
return 'return isset($targetObject->$' . $nameParameter . ');';
case static::OPERATION_UNSET:
return 'unset($targetObject->$' . $nameParameter . ');';
}
throw new \InvalidArgumentException(sprintf('Invalid operation "%s" provided', $operationType));
}
/**
* Generates code to bind operations to the parent scope
*
* @return string
*/
private static function getScopeReBind()
{
return ' $backtrace = debug_backtrace(true);' . "\n"
. ' $scopeObject = isset($backtrace[1][\'object\'])'
. ' ? $backtrace[1][\'object\'] : new \ProxyManager\Stub\EmptyClassStub();' . "\n"
. ' $accessor = $accessor->bindTo($scopeObject, get_class($scopeObject));' . "\n";
}
}