DiInfoCommand.php 5.16 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Developer\Console\Command;

use Magento\Developer\Model\Di\Information;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Helper\Table;

class DiInfoCommand extends Command
{
    /**
     * Command name
     */
    const COMMAND_NAME = 'dev:di:info';

    /**
     * input name
     */
    const CLASS_NAME = 'class';

    /**
     * @var Information
     */
    private $diInformation;

    /**
     * @param Information $diInformation
     */
    public function __construct(
        Information $diInformation
    ) {
        $this->diInformation = $diInformation;
        parent::__construct();
    }

    /**
     * {@inheritdoc}
     * @throws InvalidArgumentException
     */
    protected function configure()
    {
        $this->setName(self::COMMAND_NAME)
             ->setDescription('Provides information on Dependency Injection configuration for the Command.')
             ->setDefinition([
                new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name')
             ]);

        parent::configure();
    }

    /**
     * Print Info on Class/Interface preference
     *
     * @param string $className
     * @param OutputInterface $output
     * @return void
     */
    private function printPreference($className, $output)
    {
        $preference = $this->diInformation->getPreference($className);
        $output->writeln('');
        $output->writeln(sprintf('Preference: %s', $preference));
        $output->writeln('');
    }

    /**
     * Print Info on Constructor Arguments
     *
     * @param string $className
     * @param OutputInterface $output
     * @return void
     */
    private function printConstructorArguments($className, $output)
    {
        $output->writeln("Constructor Parameters:");
        $paramsTable = new Table($output);
        $paramsTable
            ->setHeaders(['Name', 'Requested Type', 'Configured Value']);
        $parameters = $this->diInformation->getParameters($className);
        $paramsTableArray = [];
        foreach ($parameters as $parameterRow) {
            if (is_array($parameterRow[2])) {
                $parameterRow[2] = json_encode($parameterRow[2], JSON_PRETTY_PRINT);
            }
            $paramsTableArray[] = $parameterRow;
        }
        $paramsTable->setRows($paramsTableArray);
        $output->writeln($paramsTable->render());
    }

    /**
     * Print Info on Virtual Types
     *
     * @param string $className
     * @param OutputInterface $output
     * @return void
     */
    private function printVirtualTypes($className, $output)
    {
        $virtualTypes = $this->diInformation->getVirtualTypes($className);
        if (!empty($virtualTypes)) {
            $output->writeln('');
            $output->writeln("Virtual Types:");
            foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) {
                $output->writeln('   ' . $virtualType);
            }
        }
    }

    /**
     * Print Info on Plugins
     *
     * @param string $className
     * @param OutputInterface $output
     * @param string $label
     * @return void
     */
    private function printPlugins($className, $output, $label)
    {
        $output->writeln('');
        $output->writeln($label);
        $plugins = $this->diInformation->getPlugins($className);
        $parameters = [];
        foreach ($plugins as $type => $plugin) {
            foreach ($plugin as $instance => $pluginMethods) {
                foreach ($pluginMethods as $pluginMethod) {
                    $parameters[] = [$instance, $pluginMethod, $type];
                }
            }
        }

        $table = new Table($output);
        $table
            ->setHeaders(['Plugin', 'Method', 'Type'])
            ->setRows($parameters);

        $output->writeln($table->render());
    }

    /**
     * {@inheritdoc}
     * @throws \InvalidArgumentException
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $className = $input->getArgument(self::CLASS_NAME);
        $output->setDecorated(true);
        $output->writeln('');
        $output->writeln(sprintf('DI configuration for the class %s in the GLOBAL area', $className));

        if ($this->diInformation->isVirtualType($className)) {
            $output->writeln(
                sprintf('It is Virtual Type for the Class %s', $this->diInformation->getVirtualTypeBase($className))
            );
        }
        $this->printPreference($className, $output);
        $this->printConstructorArguments($className, $output);
        $preference = $this->diInformation->getPreference($className);
        $this->printVirtualTypes($preference, $output);
        $this->printPlugins($className, $output, 'Plugins:');
        $this->printPlugins($preference, $output, 'Plugins for the Preference:');

        return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
    }
}