Information.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Model\Di;
  7. class Information
  8. {
  9. /**
  10. * @var \Magento\Framework\ObjectManager\ConfigInterface
  11. */
  12. private $objectManagerConfig;
  13. /**
  14. * @var \Magento\Developer\Model\Di\PluginList
  15. */
  16. private $pluginList;
  17. /**
  18. * @var string[]
  19. */
  20. private $preferences = [];
  21. /**
  22. * @var array
  23. */
  24. private $virtualTypes = [];
  25. /**
  26. * @var \Magento\Framework\ObjectManager\DefinitionInterface
  27. */
  28. private $definitions;
  29. /**
  30. * @param \Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig
  31. * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions
  32. * @param \Magento\Developer\Model\Di\PluginList $pluginList
  33. */
  34. public function __construct(
  35. \Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig,
  36. \Magento\Framework\ObjectManager\DefinitionInterface $definitions,
  37. \Magento\Developer\Model\Di\PluginList $pluginList
  38. ) {
  39. $this->objectManagerConfig = $objectManagerConfig;
  40. $this->definitions = $definitions;
  41. $this->pluginList = $pluginList;
  42. }
  43. /**
  44. * Get info on the preference for the class
  45. *
  46. * @param string $className
  47. * @return string
  48. */
  49. public function getPreference($className)
  50. {
  51. if (!isset($this->preferences[$className])) {
  52. $this->preferences[$className] = $this->objectManagerConfig->getPreference($className);
  53. }
  54. return $this->preferences[$className];
  55. }
  56. /**
  57. * Retrieve parameters of the constructor for the preference of the class
  58. *
  59. * @param string $className
  60. * @return array|null
  61. */
  62. private function getConstructorParameters($className)
  63. {
  64. $parameters = $this->definitions->getParameters($className);
  65. return $parameters;
  66. }
  67. /**
  68. * Retrieve array of parameters for the class constructor
  69. *
  70. * @param string $className
  71. * @return array
  72. */
  73. public function getParameters($className)
  74. {
  75. $result = [];
  76. $diConfiguration = $this->getConfiguredConstructorParameters($className);
  77. $originalParameters = $this->isVirtualType($className) ?
  78. $this->getConstructorParameters($this->getVirtualTypeBase($className)) :
  79. $this->getConstructorParameters($this->getPreference($className));
  80. if (!$originalParameters) {
  81. return $result;
  82. }
  83. foreach ($originalParameters as $parameter) {
  84. $paramArray = [$parameter[0], $parameter[1], is_array($parameter[3]) ? "<empty array>" : $parameter[3]];
  85. if (isset($diConfiguration[$parameter[0]])) {
  86. $paramArray[2] = $this->renderParameters($diConfiguration[$parameter[0]]);
  87. }
  88. $result[] = $paramArray;
  89. }
  90. return $result;
  91. }
  92. /**
  93. * Recursively retrieve array parameters
  94. *
  95. * @param string $configuredParameter
  96. * @return array|null
  97. */
  98. private function renderParameters($configuredParameter)
  99. {
  100. $result = null;
  101. if (is_array($configuredParameter)) {
  102. if (isset($configuredParameter['instance'])) {
  103. $result = 'instance of ' . $configuredParameter['instance'];
  104. } else {
  105. foreach ($configuredParameter as $keyName => $instance) {
  106. $result[$keyName] = $this->renderParameters($instance);
  107. }
  108. }
  109. } else {
  110. $result = 'string ' . $configuredParameter;
  111. }
  112. return $result;
  113. }
  114. /**
  115. * Retrieve configured types of parameters of the constructor for the preference of the class
  116. *
  117. * @param string $className
  118. * @return array|null
  119. */
  120. private function getConfiguredConstructorParameters($className)
  121. {
  122. return $this->objectManagerConfig->getArguments($className);
  123. }
  124. /**
  125. * Retrieve virtual types for the class and the preference of the class
  126. *
  127. * @param string $className
  128. * @return array
  129. */
  130. public function getVirtualTypes($className)
  131. {
  132. $preference = $this->getPreference($className);
  133. if (!isset($this->virtualTypes[$className])) {
  134. $this->virtualTypes[$className] = [];
  135. foreach ($this->objectManagerConfig->getVirtualTypes() as $virtualType => $baseName) {
  136. if ($baseName == $className || $baseName == $preference) {
  137. $this->virtualTypes[$className][] = $virtualType;
  138. }
  139. }
  140. }
  141. return $this->virtualTypes[$className];
  142. }
  143. /**
  144. * @param string $className
  145. * @return array
  146. */
  147. public function getPlugins($className)
  148. {
  149. return $this->pluginList->getPluginsListByClass($className);
  150. }
  151. /**
  152. * Is the class a virtual type
  153. *
  154. * @param string $className
  155. * @return boolean
  156. */
  157. public function isVirtualType($className)
  158. {
  159. $virtualTypes = $this->objectManagerConfig->getVirtualTypes();
  160. return isset($virtualTypes[$className]);
  161. }
  162. /**
  163. * Get base class for the Virtual Type
  164. *
  165. * @param string $className
  166. * @return string|boolean
  167. */
  168. public function getVirtualTypeBase($className)
  169. {
  170. $virtualTypes = $this->objectManagerConfig->getVirtualTypes();
  171. if (isset($virtualTypes[$className])) {
  172. return $virtualTypes[$className];
  173. }
  174. return false;
  175. }
  176. }