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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* Proxy generator
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\ObjectManager\Code\Generator;
/**
* Class Proxy
*
* @package Magento\Framework\ObjectManager\Code\Generator
*/
class Proxy extends \Magento\Framework\Code\Generator\EntityAbstract
{
/**
* Entity type
*/
const ENTITY_TYPE = 'proxy';
/**
* Marker interface
*/
const NON_INTERCEPTABLE_INTERFACE = \Magento\Framework\ObjectManager\NoninterceptableInterface::class;
/**
* Returns default result class name
*
* @param string $modelClassName
* @return string
*/
protected function _getDefaultResultClassName($modelClassName)
{
return $modelClassName . '_' . ucfirst(static::ENTITY_TYPE);
}
/**
* Retrieve class properties
*
* @return array
*/
protected function _getClassProperties()
{
$properties = parent::_getClassProperties();
// protected $_instanceName = null;
$properties[] = [
'name' => '_instanceName',
'visibility' => 'protected',
'docblock' => [
'shortDescription' => 'Proxied instance name',
'tags' => [['name' => 'var', 'description' => 'string']],
],
];
$properties[] = [
'name' => '_subject',
'visibility' => 'protected',
'docblock' => [
'shortDescription' => 'Proxied instance',
'tags' => [['name' => 'var', 'description' => $this->getSourceClassName()]],
],
];
// protected $_shared = null;
$properties[] = [
'name' => '_isShared',
'visibility' => 'protected',
'docblock' => [
'shortDescription' => 'Instance shareability flag',
'tags' => [['name' => 'var', 'description' => 'bool']],
],
];
return $properties;
}
/**
* Returns list of methods for class generator
*
* @return array
*/
protected function _getClassMethods()
{
$construct = $this->_getDefaultConstructorDefinition();
// create proxy methods for all non-static and non-final public methods (excluding constructor)
$methods = [$construct];
$methods[] = [
'name' => '__sleep',
'body' => 'return [\'_subject\', \'_isShared\', \'_instanceName\'];',
'docblock' => ['tags' => [['name' => 'return', 'description' => 'array']]],
];
$methods[] = [
'name' => '__wakeup',
'body' => '$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();',
'docblock' => ['shortDescription' => 'Retrieve ObjectManager from global scope'],
];
$methods[] = [
'name' => '__clone',
'body' => "\$this->_subject = clone \$this->_getSubject();",
'docblock' => ['shortDescription' => 'Clone proxied instance'],
];
$methods[] = [
'name' => '_getSubject',
'visibility' => 'protected',
'body' => "if (!\$this->_subject) {\n" .
" \$this->_subject = true === \$this->_isShared\n" .
" ? \$this->_objectManager->get(\$this->_instanceName)\n" .
" : \$this->_objectManager->create(\$this->_instanceName);\n" .
"}\n" .
"return \$this->_subject;",
'docblock' => [
'shortDescription' => 'Get proxied instance',
'tags' => [['name' => 'return', 'description' => $this->getSourceClassName()]],
],
];
$reflectionClass = new \ReflectionClass($this->getSourceClassName());
$publicMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($publicMethods as $method) {
if (!(
$method->isConstructor() ||
$method->isFinal() ||
$method->isStatic() ||
$method->isDestructor()
)
&& !in_array(
$method->getName(),
['__sleep', '__wakeup', '__clone']
)
) {
$methods[] = $this->_getMethodInfo($method);
}
}
return $methods;
}
/**
* Generates code
*
* @return string
*/
protected function _generateCode()
{
$typeName = $this->getSourceClassName();
$reflection = new \ReflectionClass($typeName);
if ($reflection->isInterface()) {
$this->_classGenerator->setImplementedInterfaces([$typeName, '\\' . self::NON_INTERCEPTABLE_INTERFACE]);
} else {
$this->_classGenerator->setExtendedClass($typeName);
$this->_classGenerator->setImplementedInterfaces(['\\' . self::NON_INTERCEPTABLE_INTERFACE]);
}
return parent::_generateCode();
}
/**
* Collect method info
*
* @param \ReflectionMethod $method
* @return array
*/
protected function _getMethodInfo(\ReflectionMethod $method)
{
$parameterNames = [];
$parameters = [];
foreach ($method->getParameters() as $parameter) {
$name = $parameter->isVariadic() ? '... $' . $parameter->getName() : '$' . $parameter->getName();
$parameterNames[] = $name;
$parameters[] = $this->_getMethodParameterInfo($parameter);
}
$returnTypeValue = $this->getReturnTypeValue($method->getReturnType());
$methodInfo = [
'name' => $method->getName(),
'parameters' => $parameters,
'body' => $this->_getMethodBody(
$method->getName(),
$parameterNames,
$returnTypeValue === 'void'
),
'docblock' => ['shortDescription' => '{@inheritdoc}'],
'returntype' => $returnTypeValue,
];
return $methodInfo;
}
/**
* Get default constructor definition for generated class
*
* @return array
*/
protected function _getDefaultConstructorDefinition()
{
/*
* public function __construct(
* \Magento\Framework\ObjectManagerInterface $objectManager,
* $instanceName,
* $shared = false
* )
*/
return [
'name' => '__construct',
'parameters' => [
['name' => 'objectManager', 'type' => '\\' . \Magento\Framework\ObjectManagerInterface::class],
['name' => 'instanceName', 'defaultValue' => $this->getSourceClassName()],
['name' => 'shared', 'defaultValue' => true],
],
'body' => "\$this->_objectManager = \$objectManager;" .
"\n\$this->_instanceName = \$instanceName;" .
"\n\$this->_isShared = \$shared;",
'docblock' => [
'shortDescription' => ucfirst(static::ENTITY_TYPE) . ' constructor',
'tags' => [
[
'name' => 'param',
'description' => '\Magento\Framework\ObjectManagerInterface $objectManager',
],
['name' => 'param', 'description' => 'string $instanceName'],
['name' => 'param', 'description' => 'bool $shared'],
],
]
];
}
/**
* Build proxy method body
*
* @param string $name
* @param array $parameters
* @param bool $withoutReturn
* @return string
*/
protected function _getMethodBody(
$name,
array $parameters = [],
bool $withoutReturn = false
) {
if (count($parameters) == 0) {
$methodCall = sprintf('%s()', $name);
} else {
$methodCall = sprintf('%s(%s)', $name, implode(', ', $parameters));
}
return ($withoutReturn ? '' : 'return ')
. '$this->_getSubject()->' . $methodCall . ';';
}
/**
* Validates data
*
* @return bool
*/
protected function _validateData()
{
$result = parent::_validateData();
if ($result) {
$sourceClassName = $this->getSourceClassName();
$resultClassName = $this->_getResultClassName();
if ($resultClassName !== $sourceClassName . '\\Proxy') {
$this->_addError(
'Invalid Proxy class name [' . $resultClassName . ']. Use ' . $sourceClassName . '\\Proxy'
);
$result = false;
}
}
return $result;
}
/**
* Returns return type
*
* @param mixed $returnType
* @return null|string
*/
private function getReturnTypeValue($returnType): ?string
{
$returnTypeValue = null;
if ($returnType) {
$returnTypeValue = ($returnType->allowsNull() ? '?' : '');
$returnTypeValue .= ($returnType->getName() === 'self')
? $this->getSourceClassName()
: $returnType->getName();
}
return $returnTypeValue;
}
}