DiInfoCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Console\Command;
  7. use Magento\Developer\Model\Di\Information;
  8. use Symfony\Component\Console\Command\Command;
  9. use Symfony\Component\Console\Exception\InvalidArgumentException;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Helper\Table;
  14. class DiInfoCommand extends Command
  15. {
  16. /**
  17. * Command name
  18. */
  19. const COMMAND_NAME = 'dev:di:info';
  20. /**
  21. * input name
  22. */
  23. const CLASS_NAME = 'class';
  24. /**
  25. * @var Information
  26. */
  27. private $diInformation;
  28. /**
  29. * @param Information $diInformation
  30. */
  31. public function __construct(
  32. Information $diInformation
  33. ) {
  34. $this->diInformation = $diInformation;
  35. parent::__construct();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. * @throws InvalidArgumentException
  40. */
  41. protected function configure()
  42. {
  43. $this->setName(self::COMMAND_NAME)
  44. ->setDescription('Provides information on Dependency Injection configuration for the Command.')
  45. ->setDefinition([
  46. new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name')
  47. ]);
  48. parent::configure();
  49. }
  50. /**
  51. * Print Info on Class/Interface preference
  52. *
  53. * @param string $className
  54. * @param OutputInterface $output
  55. * @return void
  56. */
  57. private function printPreference($className, $output)
  58. {
  59. $preference = $this->diInformation->getPreference($className);
  60. $output->writeln('');
  61. $output->writeln(sprintf('Preference: %s', $preference));
  62. $output->writeln('');
  63. }
  64. /**
  65. * Print Info on Constructor Arguments
  66. *
  67. * @param string $className
  68. * @param OutputInterface $output
  69. * @return void
  70. */
  71. private function printConstructorArguments($className, $output)
  72. {
  73. $output->writeln("Constructor Parameters:");
  74. $paramsTable = new Table($output);
  75. $paramsTable
  76. ->setHeaders(['Name', 'Requested Type', 'Configured Value']);
  77. $parameters = $this->diInformation->getParameters($className);
  78. $paramsTableArray = [];
  79. foreach ($parameters as $parameterRow) {
  80. if (is_array($parameterRow[2])) {
  81. $parameterRow[2] = json_encode($parameterRow[2], JSON_PRETTY_PRINT);
  82. }
  83. $paramsTableArray[] = $parameterRow;
  84. }
  85. $paramsTable->setRows($paramsTableArray);
  86. $output->writeln($paramsTable->render());
  87. }
  88. /**
  89. * Print Info on Virtual Types
  90. *
  91. * @param string $className
  92. * @param OutputInterface $output
  93. * @return void
  94. */
  95. private function printVirtualTypes($className, $output)
  96. {
  97. $virtualTypes = $this->diInformation->getVirtualTypes($className);
  98. if (!empty($virtualTypes)) {
  99. $output->writeln('');
  100. $output->writeln("Virtual Types:");
  101. foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) {
  102. $output->writeln(' ' . $virtualType);
  103. }
  104. }
  105. }
  106. /**
  107. * Print Info on Plugins
  108. *
  109. * @param string $className
  110. * @param OutputInterface $output
  111. * @param string $label
  112. * @return void
  113. */
  114. private function printPlugins($className, $output, $label)
  115. {
  116. $output->writeln('');
  117. $output->writeln($label);
  118. $plugins = $this->diInformation->getPlugins($className);
  119. $parameters = [];
  120. foreach ($plugins as $type => $plugin) {
  121. foreach ($plugin as $instance => $pluginMethods) {
  122. foreach ($pluginMethods as $pluginMethod) {
  123. $parameters[] = [$instance, $pluginMethod, $type];
  124. }
  125. }
  126. }
  127. $table = new Table($output);
  128. $table
  129. ->setHeaders(['Plugin', 'Method', 'Type'])
  130. ->setRows($parameters);
  131. $output->writeln($table->render());
  132. }
  133. /**
  134. * {@inheritdoc}
  135. * @throws \InvalidArgumentException
  136. */
  137. protected function execute(InputInterface $input, OutputInterface $output)
  138. {
  139. $className = $input->getArgument(self::CLASS_NAME);
  140. $output->setDecorated(true);
  141. $output->writeln('');
  142. $output->writeln(sprintf('DI configuration for the class %s in the GLOBAL area', $className));
  143. if ($this->diInformation->isVirtualType($className)) {
  144. $output->writeln(
  145. sprintf('It is Virtual Type for the Class %s', $this->diInformation->getVirtualTypeBase($className))
  146. );
  147. }
  148. $this->printPreference($className, $output);
  149. $this->printConstructorArguments($className, $output);
  150. $preference = $this->diInformation->getPreference($className);
  151. $this->printVirtualTypes($preference, $output);
  152. $this->printPlugins($className, $output, 'Plugins:');
  153. $this->printPlugins($preference, $output, 'Plugins for the Preference:');
  154. return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
  155. }
  156. }