10 class Proxy extends \Magento\Framework\Code\Generator\EntityAbstract
28 return $modelClassName .
'_' . ucfirst(static::ENTITY_TYPE);
42 'name' =>
'_instanceName',
43 'visibility' =>
'protected',
45 'shortDescription' =>
'Proxied instance name',
46 'tags' => [[
'name' =>
'var',
'description' =>
'string']],
52 'visibility' =>
'protected',
54 'shortDescription' =>
'Proxied instance',
61 'name' =>
'_isShared',
62 'visibility' =>
'protected',
64 'shortDescription' =>
'Instance shareability flag',
65 'tags' => [[
'name' =>
'var',
'description' =>
'bool']],
84 'body' =>
'return [\'_subject\', \'_isShared\', \'_instanceName\'];',
85 'docblock' => [
'tags' => [[
'name' =>
'return',
'description' =>
'array']]],
89 'body' =>
'$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();',
90 'docblock' => [
'shortDescription' =>
'Retrieve ObjectManager from global scope'],
94 'body' =>
"\$this->_subject = clone \$this->_getSubject();",
95 'docblock' => [
'shortDescription' =>
'Clone proxied instance'],
99 'name' =>
'_getSubject',
100 'visibility' =>
'protected',
101 'body' =>
"if (!\$this->_subject) {\n" .
102 " \$this->_subject = true === \$this->_isShared\n" .
103 " ? \$this->_objectManager->get(\$this->_instanceName)\n" .
104 " : \$this->_objectManager->create(\$this->_instanceName);\n" .
106 "return \$this->_subject;",
108 'shortDescription' =>
'Get proxied instance',
113 $publicMethods =
$reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
114 foreach ($publicMethods as
$method) {
115 if (!(
$method->isConstructor() ||
118 $method->isDestructor()) && !in_array(
120 [
'__sleep',
'__wakeup',
'__clone']
136 $reflection = new \ReflectionClass($typeName);
138 if ($reflection->isInterface()) {
139 $this->_classGenerator->setImplementedInterfaces([$typeName,
'\\' . self::NON_INTERCEPTABLE_INTERFACE]);
141 $this->_classGenerator->setExtendedClass($typeName);
142 $this->_classGenerator->setImplementedInterfaces([
'\\' . self::NON_INTERCEPTABLE_INTERFACE]);
144 return parent::_generateCode();
155 $parameterNames = [];
157 foreach (
$method->getParameters() as $parameter) {
158 $name = $parameter->isVariadic() ?
'... $' . $parameter->getName() :
'$' . $parameter->getName();
159 $parameterNames[] =
$name;
163 $returnType =
$method->getReturnType();
164 $returnTypeValue = $returnType
165 ? ($returnType->allowsNull() ?
'?' :
'') .$returnType->getName()
169 'parameters' => $parameters,
173 $returnTypeValue ===
'void' 175 'docblock' => [
'shortDescription' =>
'{@inheritdoc}'],
176 'returntype' => $returnTypeValue,
197 'name' =>
'__construct',
199 [
'name' =>
'objectManager',
'type' =>
'\\' . \Magento\Framework\ObjectManagerInterface::class],
201 [
'name' =>
'shared',
'defaultValue' =>
true],
203 'body' =>
"\$this->_objectManager = \$objectManager;" .
204 "\n\$this->_instanceName = \$instanceName;" .
205 "\n\$this->_isShared = \$shared;",
207 'shortDescription' => ucfirst(static::ENTITY_TYPE) .
' constructor',
211 'description' =>
'\Magento\Framework\ObjectManagerInterface $objectManager',
213 [
'name' =>
'param',
'description' =>
'string $instanceName'],
214 [
'name' =>
'param',
'description' =>
'bool $shared'],
230 array $parameters = [],
231 bool $withoutReturn =
false 233 if (count($parameters) == 0) {
234 $methodCall = sprintf(
'%s()',
$name);
236 $methodCall = sprintf(
'%s(%s)',
$name, implode(
', ', $parameters));
239 return ($withoutReturn ?
'' :
'return ')
240 .
'$this->_getSubject()->' . $methodCall .
';';
248 $result = parent::_validateData();
253 if ($resultClassName !== $sourceClassName .
'\\Proxy') {
255 'Invalid Proxy class name [' . $resultClassName .
']. Use ' . $sourceClassName .
'\\Proxy'
const NON_INTERCEPTABLE_INTERFACE
_getDefaultResultClassName($modelClassName)
_getDefaultConstructorDefinition()
_getMethodBody( $name, array $parameters=[], bool $withoutReturn=false)
_getMethodParameterInfo(\ReflectionParameter $parameter)
_getMethodInfo(\ReflectionMethod $method)
if(!isset($_GET['name'])) $name