Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DiInfoCommand.php
Go to the documentation of this file.
1 <?php
8 
10 use Symfony\Component\Console\Command\Command;
11 use Symfony\Component\Console\Exception\InvalidArgumentException;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Input\InputArgument;
15 use Symfony\Component\Console\Helper\Table;
16 
17 class DiInfoCommand extends Command
18 {
22  const COMMAND_NAME = 'dev:di:info';
23 
27  const CLASS_NAME = 'class';
28 
32  private $diInformation;
33 
37  public function __construct(
38  Information $diInformation
39  ) {
40  $this->diInformation = $diInformation;
41  parent::__construct();
42  }
43 
48  protected function configure()
49  {
50  $this->setName(self::COMMAND_NAME)
51  ->setDescription('Provides information on Dependency Injection configuration for the Command.')
52  ->setDefinition([
53  new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name')
54  ]);
55 
56  parent::configure();
57  }
58 
66  private function printPreference($className, $output)
67  {
68  $preference = $this->diInformation->getPreference($className);
69  $output->writeln('');
70  $output->writeln(sprintf('Preference: %s', $preference));
71  $output->writeln('');
72  }
73 
81  private function printConstructorArguments($className, $output)
82  {
83  $output->writeln("Constructor Parameters:");
84  $paramsTable = new Table($output);
85  $paramsTable
86  ->setHeaders(['Name', 'Requested Type', 'Configured Value']);
87  $parameters = $this->diInformation->getParameters($className);
88  $paramsTableArray = [];
89  foreach ($parameters as $parameterRow) {
90  if (is_array($parameterRow[2])) {
91  $parameterRow[2] = json_encode($parameterRow[2], JSON_PRETTY_PRINT);
92  }
93  $paramsTableArray[] = $parameterRow;
94  }
95  $paramsTable->setRows($paramsTableArray);
96  $output->writeln($paramsTable->render());
97  }
98 
106  private function printVirtualTypes($className, $output)
107  {
108  $virtualTypes = $this->diInformation->getVirtualTypes($className);
109  if (!empty($virtualTypes)) {
110  $output->writeln('');
111  $output->writeln("Virtual Types:");
112  foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) {
113  $output->writeln(' ' . $virtualType);
114  }
115  }
116  }
117 
126  private function printPlugins($className, $output, $label)
127  {
128  $output->writeln('');
129  $output->writeln($label);
130  $plugins = $this->diInformation->getPlugins($className);
131  $parameters = [];
132  foreach ($plugins as $type => $plugin) {
133  foreach ($plugin as $instance => $pluginMethods) {
134  foreach ($pluginMethods as $pluginMethod) {
135  $parameters[] = [$instance, $pluginMethod, $type];
136  }
137  }
138  }
139 
140  $table = new Table($output);
141  $table
142  ->setHeaders(['Plugin', 'Method', 'Type'])
143  ->setRows($parameters);
144 
145  $output->writeln($table->render());
146  }
147 
152  protected function execute(InputInterface $input, OutputInterface $output)
153  {
154  $className = $input->getArgument(self::CLASS_NAME);
155  $output->setDecorated(true);
156  $output->writeln('');
157  $output->writeln(sprintf('DI configuration for the class %s in the GLOBAL area', $className));
158 
159  if ($this->diInformation->isVirtualType($className)) {
160  $output->writeln(
161  sprintf('It is Virtual Type for the Class %s', $this->diInformation->getVirtualTypeBase($className))
162  );
163  }
164  $this->printPreference($className, $output);
165  $this->printConstructorArguments($className, $output);
166  $preference = $this->diInformation->getPreference($className);
167  $this->printVirtualTypes($preference, $output);
168  $this->printPlugins($className, $output, 'Plugins:');
169  $this->printPlugins($preference, $output, 'Plugins for the Preference:');
170 
171  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
172  }
173 }
$type
Definition: item.phtml:13
$label
Definition: details.phtml:21
execute(InputInterface $input, OutputInterface $output)
$table
Definition: trigger.php:14
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31