Skip to content

Cli info di #9103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a7934d2
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 1, 2017
7e6d451
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 1, 2017
368b26e
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
springerin Apr 1, 2017
e69a307
Merge remote-tracking branch 'origin/cli-info-di' into cli-info-di
springerin Apr 1, 2017
4dc7a92
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 1, 2017
fe068fb
Merge branch 'cli-info-di' of https://github.com/springerin/magento2 …
vrann Apr 1, 2017
2574765
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
springerin Apr 1, 2017
db61414
Merge remote-tracking branch 'origin/cli-info-di' into cli-info-di
springerin Apr 1, 2017
551dd4e
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 1, 2017
2ebd560
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 2, 2017
789cb19
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
springerin Apr 2, 2017
32479cd
Merge branch 'cli-info-di' of https://github.com/springerin/magento2 …
vrann Apr 2, 2017
2608b1d
Merge branch 'cli-info-di' of https://github.com/springerin/magento2 …
vrann Apr 2, 2017
f580225
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
springerin Apr 2, 2017
e9c71c0
Merge branch 'cli-info-di' of https://github.com/springerin/magento2 …
vrann Apr 2, 2017
2cb46ff
Merge branch 'cli-info-di' of https://github.com/springerin/magento2 …
vrann Apr 2, 2017
2da051d
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
springerin Apr 2, 2017
2ac9182
Merge remote-tracking branch 'origin/cli-info-di' into cli-info-di
springerin Apr 2, 2017
b853969
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
springerin Apr 2, 2017
7cef537
Merge branch 'cli-info-di' of https://github.com/springerin/magento2 …
vrann Apr 2, 2017
1077a97
Merge branch 'cli-info-di' of https://github.com/springerin/magento2 …
vrann Apr 2, 2017
3d1ffab
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 2, 2017
63b14ad
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 2, 2017
22120a2
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 2, 2017
2d03656
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 12, 2017
9633c46
Merge remote-tracking branch 'mainline/develop' into cli-info-di
vrann Apr 12, 2017
3b4f21f
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 12, 2017
fd9321c
magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dep…
vrann Apr 12, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions app/code/Magento/Developer/Console/Command/DiInfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
193 changes: 193 additions & 0 deletions app/code/Magento/Developer/Model/Di/Information.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Developer\Model\Di;

class Information
{
/**
* @var \Magento\Framework\ObjectManager\ConfigInterface
*/
private $objectManagerConfig;

/**
* @var \Magento\Developer\Model\Di\PluginList
*/
private $pluginList;

/**
* @var string[]
*/
private $preferences = [];

/**
* @var array
*/
private $virtualTypes = [];

/**
* @var \Magento\Framework\ObjectManager\DefinitionInterface
*/
private $definitions;

/**
* @param \Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig
* @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions
* @param \Magento\Developer\Model\Di\PluginList $pluginList
*/
public function __construct(
\Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig,
\Magento\Framework\ObjectManager\DefinitionInterface $definitions,
\Magento\Developer\Model\Di\PluginList $pluginList
) {
$this->objectManagerConfig = $objectManagerConfig;
$this->definitions = $definitions;
$this->pluginList = $pluginList;
}

/**
* Get info on the preference for the class
*
* @param string $className
* @return string
*/
public function getPreference($className)
{
if (!isset($this->preferences[$className])) {
$this->preferences[$className] = $this->objectManagerConfig->getPreference($className);
}
return $this->preferences[$className];
}

/**
* Retrieve parameters of the constructor for the preference of the class
*
* @param string $className
* @return array|null
*/
private function getConstructorParameters($className)
{
$parameters = $this->definitions->getParameters($className);
return $parameters;
}

/**
* Retrieve array of parameters for the class constructor
*
* @param string $className
* @return array
*/
public function getParameters($className)
{
$result = [];
$diConfiguration = $this->getConfiguredConstructorParameters($className);
$originalParameters = $this->isVirtualType($className) ?
$this->getConstructorParameters($this->getVirtualTypeBase($className)) :
$this->getConstructorParameters($this->getPreference($className));

if (!$originalParameters) {
return $result;
}

foreach ($originalParameters as $parameter) {
$paramArray = [$parameter[0], $parameter[1], is_array($parameter[3]) ? "<empty array>" : $parameter[3]];
if (isset($diConfiguration[$parameter[0]])) {
$paramArray[2] = $this->renderParameters($diConfiguration[$parameter[0]]);
}
$result[] = $paramArray;
}
return $result;
}

/**
* Recursively retrieve array parameters
*
* @param string $configuredParameter
* @return array|null
*/
private function renderParameters($configuredParameter)
{
$result = null;
if (is_array($configuredParameter)) {
if (isset($configuredParameter['instance'])) {
$result = 'instance of ' . $configuredParameter['instance'];
} else {
foreach ($configuredParameter as $keyName => $instance) {
$result[$keyName] = $this->renderParameters($instance);
}
}
} else {
$result = 'string ' . $configuredParameter;
}
return $result;
}

/**
* Retrieve configured types of parameters of the constructor for the preference of the class
*
* @param string $className
* @return array|null
*/
private function getConfiguredConstructorParameters($className)
{
return $this->objectManagerConfig->getArguments($className);
}

/**
* Retrieve virtual types for the class and the preference of the class
*
* @param string $className
* @return array
*/
public function getVirtualTypes($className)
{
$preference = $this->getPreference($className);
if (!isset($this->virtualTypes[$className])) {
$this->virtualTypes[$className] = [];
foreach ($this->objectManagerConfig->getVirtualTypes() as $virtualType => $baseName) {
if ($baseName == $className || $baseName == $preference) {
$this->virtualTypes[$className][] = $virtualType;
}
}
}
return $this->virtualTypes[$className];
}

/**
* @param string $className
* @return array
*/
public function getPlugins($className)
{
return $this->pluginList->getPluginsListByClass($className);
}

/**
* Is the class a virtual type
*
* @param string $className
* @return boolean
*/
public function isVirtualType($className)
{
$virtualTypes = $this->objectManagerConfig->getVirtualTypes();
return isset($virtualTypes[$className]);
}

/**
* Get base class for the Virtual Type
*
* @param string $className
* @return string|boolean
*/
public function getVirtualTypeBase($className)
{
$virtualTypes = $this->objectManagerConfig->getVirtualTypes();
if (isset($virtualTypes[$className])) {
return $virtualTypes[$className];
}
return false;
}
}
Loading