PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/framework/ObjectManager/Profiler/Code/Generator/Logger.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 217 lines | 203 code | 2 blank | 12 comment | 0 complexity | f5f81a98c65a4107246b0f38f2fabc59 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Copyright © 2016 Magento. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Framework\ObjectManager\Profiler\Code\Generator;
  8. class Logger extends \Magento\Framework\Code\Generator\EntityAbstract
  9. {
  10. /**
  11. * Entity type
  12. */
  13. const ENTITY_TYPE = 'logger';
  14. /**
  15. * @param string $modelClassName
  16. * @return string
  17. */
  18. protected function _getDefaultResultClassName($modelClassName)
  19. {
  20. return $modelClassName . '\\' . ucfirst(static::ENTITY_TYPE);
  21. }
  22. /**
  23. * Returns list of properties for class generator
  24. *
  25. * @return array
  26. */
  27. protected function _getClassProperties()
  28. {
  29. return [
  30. [
  31. 'name' => 'log',
  32. 'visibility' => 'protected',
  33. 'docblock' => [
  34. 'shortDescription' => 'Object Manager factory log',
  35. 'tags' => [
  36. ['name' => 'var', 'description' => '\Magento\Framework\ObjectManager\Factory\Log'],
  37. ],
  38. ],
  39. ],
  40. [
  41. 'name' => 'subject',
  42. 'visibility' => 'protected',
  43. 'docblock' => [
  44. 'shortDescription' => 'Object Manager instance',
  45. 'tags' => [
  46. ['name' => 'var', 'description' => '\Magento\Framework\ObjectManagerInterface'],
  47. ],
  48. ],
  49. ],
  50. ];
  51. }
  52. /**
  53. * Get default constructor definition for generated class
  54. *
  55. * @return array
  56. */
  57. protected function _getDefaultConstructorDefinition()
  58. {
  59. return [
  60. 'name' => '__construct',
  61. 'parameters' => [
  62. ['name' => 'subject'],
  63. ['name' => 'log'],
  64. ],
  65. 'body' => "\$this->log = \$log;"
  66. . "\n\$this->subject = \$subject;"
  67. ];
  68. }
  69. /**
  70. * Returns list of methods for class generator
  71. *
  72. * @return mixed
  73. */
  74. protected function _getClassMethods()
  75. {
  76. $methods = [$this->_getDefaultConstructorDefinition()];
  77. $methods[] = [
  78. 'name' => '_invoke',
  79. 'visibility' => 'protected',
  80. 'parameters' => [
  81. ['name' => 'methodName'],
  82. ['name' => 'methodArguments', 'type' => 'array', 'passedByReference' => true],
  83. ],
  84. 'body' => $this->_getInvokeMethodBody(),
  85. 'docblock' => [
  86. 'shortDescription' => 'Invoke method',
  87. 'tags' => [
  88. ['name' => 'param', 'description' => 'string $methodName'],
  89. ['name' => 'param', 'description' => 'array $methodArguments'],
  90. ['name' => 'return', 'description' => 'mixed'],
  91. ],
  92. ],
  93. ];
  94. $methods[] = [
  95. 'name' => '__clone',
  96. 'body' => "\$this->subject = clone \$this->subject;"
  97. . "\n\$this->log->add(\$this->subject);",
  98. 'docblock' => [
  99. 'shortDescription' => 'Clone subject instance',
  100. ],
  101. ];
  102. $methods[] = [
  103. 'name' => '__sleep',
  104. 'body' => "return array('subject');",
  105. ];
  106. $methods[] = [
  107. 'name' => '__wakeUp',
  108. 'body' => "\$this->log = \\Magento\\Framework\\ObjectManager\\Profiler\\Log::getInstance();"
  109. . "\n\$this->log->add(\$this->subject);",
  110. ];
  111. $reflectionClass = new \ReflectionClass($this->getSourceClassName());
  112. $publicMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
  113. foreach ($publicMethods as $method) {
  114. if (!($method->isConstructor() || $method->isFinal() || $method->isStatic() || $method->isDestructor())
  115. && !in_array($method->getName(), ['__sleep', '__wakeup', '__clone'])
  116. ) {
  117. $methods[] = $this->_getMethodInfo($method);
  118. }
  119. }
  120. return $methods;
  121. }
  122. /**
  123. * Retrieve body of the _invoke method
  124. *
  125. * @return string
  126. */
  127. protected function _getInvokeMethodBody()
  128. {
  129. return "\n\$this->log->invoked(\$this->subject, \$methodName);"
  130. . "\n\$result = call_user_func_array(array(\$this->subject, \$methodName), \$methodArguments);"
  131. . "\nif (\$result === \$this->subject) {"
  132. . "\n return \$this;"
  133. . "\n}"
  134. . "\nreturn \$result;";
  135. }
  136. /**
  137. * Retrieve method info
  138. *
  139. * @param \ReflectionMethod $method
  140. * @return array
  141. */
  142. protected function _getMethodInfo(\ReflectionMethod $method)
  143. {
  144. $parameters = [];
  145. foreach ($method->getParameters() as $parameter) {
  146. $parameters[] = $this->_getMethodParameterInfo($parameter);
  147. }
  148. $body = "\$args = func_get_args();";
  149. foreach ($parameters as $key => $parameter) {
  150. if ($parameter['passedByReference']) {
  151. $body .= "\$args[$key] = &\$" . $parameter['name'] . ';';
  152. }
  153. }
  154. $methodInfo = [
  155. 'name' => $method->getName(),
  156. 'parameters' => $parameters,
  157. 'body' => $body . "\nreturn \$this->_invoke('{$method->getName()}', \$args);",
  158. 'docblock' => [
  159. 'shortDescription' => '{@inheritdoc}',
  160. ],
  161. ];
  162. return $methodInfo;
  163. }
  164. /**
  165. * Generate resulting class source code
  166. *
  167. * @return string
  168. */
  169. protected function _generateCode()
  170. {
  171. $typeName = $this->getSourceClassName();
  172. $reflection = new \ReflectionClass($typeName);
  173. if ($reflection->isInterface()) {
  174. $this->_classGenerator->setImplementedInterfaces([$typeName]);
  175. } else {
  176. $this->_classGenerator->setExtendedClass($typeName);
  177. }
  178. return parent::_generateCode();
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. protected function _validateData()
  184. {
  185. $result = parent::_validateData();
  186. if ($result) {
  187. $sourceClassName = $this->getSourceClassName();
  188. $resultClassName = $this->_getResultClassName();
  189. if ($resultClassName !== $sourceClassName . '\\Logger') {
  190. $this->_addError(
  191. 'Invalid Logger class name [' . $resultClassName . ']. Use ' . $sourceClassName . '\\Logger'
  192. );
  193. $result = false;
  194. }
  195. }
  196. return $result;
  197. }
  198. }