Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Interceptor.php
Go to the documentation of this file.
1 <?php
8 
9 class Interceptor extends \Magento\Framework\Code\Generator\EntityAbstract
10 {
14  const ENTITY_TYPE = 'interceptor';
15 
20  protected function _getDefaultResultClassName($modelClassName)
21  {
22  return $modelClassName . '_' . ucfirst(static::ENTITY_TYPE);
23  }
24 
30  protected function _getClassProperties()
31  {
32  return [];
33  }
34 
40  protected function _getDefaultConstructorDefinition()
41  {
42  $reflectionClass = new \ReflectionClass($this->getSourceClassName());
43  $constructor = $reflectionClass->getConstructor();
44  $parameters = [];
45  $body = "\$this->___init();\n";
46  if ($constructor) {
47  foreach ($constructor->getParameters() as $parameter) {
48  $parameters[] = $this->_getMethodParameterInfo($parameter);
49  }
50  $body .= count($parameters)
51  ? "parent::__construct({$this->_getParameterList($parameters)});"
52  : "parent::__construct();";
53  }
54  return [
55  'name' => '__construct',
56  'parameters' => $parameters,
57  'body' => $body
58  ];
59  }
60 
66  protected function _getClassMethods()
67  {
69 
70  $reflectionClass = new \ReflectionClass($this->getSourceClassName());
71  $publicMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
72  foreach ($publicMethods as $method) {
73  if ($this->isInterceptedMethod($method)) {
74  $methods[] = $this->_getMethodInfo($method);
75  }
76  }
77  return $methods;
78  }
79 
86  protected function isInterceptedMethod(\ReflectionMethod $method)
87  {
88  return !($method->isConstructor() || $method->isFinal() || $method->isStatic() || $method->isDestructor()) &&
89  !in_array($method->getName(), ['__sleep', '__wakeup', '__clone']);
90  }
91 
98  protected function _getMethodInfo(\ReflectionMethod $method)
99  {
100  $parameters = [];
101  foreach ($method->getParameters() as $parameter) {
102  $parameters[] = $this->_getMethodParameterInfo($parameter);
103  }
104 
105  $returnType = $method->getReturnType();
106  $returnTypeValue = $returnType
107  ? ($returnType->allowsNull() ? '?' : '') .$returnType->getName()
108  : null;
109  $methodInfo = [
110  'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(),
111  'parameters' => $parameters,
112  'body' => str_replace(
113  [
114  '%methodName%',
115  '%return%',
116  '%parameters%'
117  ],
118  [
119  $method->getName(),
120  $returnTypeValue === 'void' ? '' : ' return',
121  $this->_getParameterList($parameters)
122  ],
123  <<<'METHOD_BODY'
124 $pluginInfo = $this->pluginList->getNext($this->subjectType, '%methodName%');
125 if (!$pluginInfo) {
126  %return% parent::%methodName%(%parameters%);
127 } else {
128  %return% $this->___callPlugins('%methodName%', func_get_args(), $pluginInfo);
129 }
130 METHOD_BODY
131  ),
132  'returnType' => $returnTypeValue,
133  'docblock' => ['shortDescription' => '{@inheritdoc}'],
134  ];
135 
136  return $methodInfo;
137  }
138 
143  protected function _getParameterList(array $parameters)
144  {
145  return implode(
146  ', ',
147  array_map(
148  function ($item) {
149  $output = '';
150  if ($item['variadic']) {
151  $output .= '... ';
152  }
153 
154  $output .= "\${$item['name']}";
155  return $output;
156  },
157  $parameters
158  )
159  );
160  }
161 
167  protected function _generateCode()
168  {
169  $typeName = $this->getSourceClassName();
170  $reflection = new \ReflectionClass($typeName);
171 
172  $interfaces = [];
173  if ($reflection->isInterface()) {
174  $interfaces[] = $typeName;
175  } else {
176  $this->_classGenerator->setExtendedClass($typeName);
177  }
178  $this->_classGenerator->addTrait('\\'. \Magento\Framework\Interception\Interceptor::class);
179  $interfaces[] = '\\'. \Magento\Framework\Interception\InterceptorInterface::class;
180  $this->_classGenerator->setImplementedInterfaces($interfaces);
181  return parent::_generateCode();
182  }
183 
187  protected function _validateData()
188  {
189  $result = parent::_validateData();
190 
191  if ($result) {
192  $sourceClassName = $this->getSourceClassName();
193  $resultClassName = $this->_getResultClassName();
194 
195  if ($resultClassName !== $sourceClassName . '\\Interceptor') {
196  $this->_addError(
197  'Invalid Interceptor class name [' .
198  $resultClassName .
199  ']. Use ' .
200  $sourceClassName .
201  '\\Interceptor'
202  );
203  $result = false;
204  }
205  }
206  return $result;
207  }
208 }
$methods
Definition: billing.phtml:71
$reflectionClass
Definition: categories.php:25
$method
Definition: info.phtml:13
___callPlugins($method, array $arguments, array $pluginInfo)
Definition: Interceptor.php:98
_getMethodParameterInfo(\ReflectionParameter $parameter)