From a7934d218dea4f071dc7b18c8ba266b0ae1abb4d Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sat, 1 Apr 2017 14:04:34 -0700 Subject: [PATCH 01/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - Create class to collect DI configuration information --- .../Developer/Model/Di/Information.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/code/Magento/Developer/Model/Di/Information.php diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php new file mode 100644 index 0000000000000..6e76d8a8c163a --- /dev/null +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -0,0 +1,33 @@ +objectManagerConfig = $objectManagerConfig; + } + + /** + * Get info on the preference for the class + * + * @param string $className + * @return string + */ + public function getPreference($className) + { + return $this->objectManagerConfig->getPreference($className); + } +} \ No newline at end of file From 7e6d451c78b3d4ab6eda42f72da793e9ebd6010f Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sat, 1 Apr 2017 14:22:44 -0700 Subject: [PATCH 02/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - Create method to retrieve constructor parameters info --- .../Developer/Model/Di/Information.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index 6e76d8a8c163a..18464712ac236 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -12,6 +12,11 @@ class Information */ private $objectManagerConfig; + /** + * @var string[] + */ + private $preferences = []; + /** * @param \Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig */ @@ -28,6 +33,20 @@ public function __construct(\Magento\Framework\ObjectManager\ConfigInterface $ob */ public function getPreference($className) { - return $this->objectManagerConfig->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 class preference object + * + * @param $className + * @return array|null + */ + public function getConstructorParameters($className) + { + return $this->objectManagerConfig->getArguments($className); } } \ No newline at end of file From 368b26eb137de5dc01f45a7ac33c35f4dcd393c5 Mon Sep 17 00:00:00 2001 From: andra lungu Date: Sat, 1 Apr 2017 14:27:28 -0700 Subject: [PATCH 03/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - Create Class for Di info command --- .../Console/Command/DiInfoCommand.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/code/Magento/Developer/Console/Command/DiInfoCommand.php diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php new file mode 100644 index 0000000000000..250d0541a25a0 --- /dev/null +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -0,0 +1,56 @@ + + * Date: 01/04/17 + * Time: 14.02 + */ + +namespace Magento\Developer\Console\Command; + +use Magento\Developer\Model\Di\Information; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Exception\InvalidArgumentException; + +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('Generates the list of preferences for the class') + ->setDefinition([ + new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name') + ]); + + parent::configure(); + } +} \ No newline at end of file From 4dc7a922da785a514cfc86240eb45bade9dc48fe Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sat, 1 Apr 2017 14:36:20 -0700 Subject: [PATCH 04/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - Create method to retrieve virtual types of the class and its preference --- .../Developer/Model/Di/Information.php | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index 18464712ac236..b8d3775e10807 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -40,13 +40,32 @@ public function getPreference($className) } /** - * Retrieve parameters of the constructor for the class preference object + * Retrieve parameters of the constructor for the preference of the class * * @param $className * @return array|null */ public function getConstructorParameters($className) { - return $this->objectManagerConfig->getArguments($className); + $preferenceClass = $this->getPreference($className); + return $this->objectManagerConfig->getArguments($preferenceClass); + } + + /** + * Retrieve virtual types for the class and the preference of the class + * + * @param $className + * @return array + */ + public function getVirtualTypes($className) + { + $preference = $this->getPreference($className); + $virtualTypes = []; + foreach ($this->objectManagerConfig->getVirtualTypes() as $virtualType => $baseName) { + if ($baseName == $className || $baseName == $preference) { + $virtualTypes[] = $virtualType; + } + } + return $virtualTypes; } } \ No newline at end of file From 257476523cd19bf57ffb2c85a3458f23e0569d3b Mon Sep 17 00:00:00 2001 From: andra lungu Date: Sat, 1 Apr 2017 15:04:54 -0700 Subject: [PATCH 05/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - added in di.xml the dev_di_info command --- app/code/Magento/Developer/etc/di.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Developer/etc/di.xml b/app/code/Magento/Developer/etc/di.xml index 550ea10e6be9f..d384c39ebbe50 100644 --- a/app/code/Magento/Developer/etc/di.xml +++ b/app/code/Magento/Developer/etc/di.xml @@ -96,6 +96,7 @@ Magento\Developer\Console\Command\SourceThemeDeployCommand Magento\Developer\Console\Command\XmlConverterCommand Magento\Developer\Console\Command\XmlCatalogGenerateCommand + Magento\Developer\Console\Command\DiInfoCommand From 551dd4ef01276db08d556862a70aa8c9eff44087 Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sat, 1 Apr 2017 15:27:44 -0700 Subject: [PATCH 06/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - added the execute method --- .../Console/Command/DiInfoCommand.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 250d0541a25a0..7b9aab02d32f1 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -11,6 +11,9 @@ 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; class DiInfoCommand extends Command { @@ -46,11 +49,24 @@ public function __construct( protected function configure() { $this->setName(self::COMMAND_NAME) - ->setDescription('Generates the list of preferences for the class') + ->setDescription('Provides information on Dependency Injection configuration for the Command.') ->setDefinition([ new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name') ]); parent::configure(); } + + /** + * {@inheritdoc} + * @throws \InvalidArgumentException + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $className = $input->getArgument(self::CLASS_NAME); + $output->writeln(sprintf('DI configuration for the class %s', $className)); + $output->writeln(sprintf('- Preference: %s', $this->diInformation->getPreference($className))); + //$output->writeln(sprintf('- Constructor Parameters: %s', $this->diInformation->getPreference($className))); + return \Magento\Framework\Console\Cli::RETURN_SUCCESS; + } } \ No newline at end of file From 2ebd560fb456bbec228ba5519a50c6b08159997a Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sat, 1 Apr 2017 19:25:07 -0700 Subject: [PATCH 07/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - format the output of the command --- .../Console/Command/DiInfoCommand.php | 29 +++++++++++++++++-- .../Developer/Model/Di/Information.php | 26 +++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 7b9aab02d32f1..02c3b6c4ded36 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -14,6 +14,7 @@ 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 { @@ -64,9 +65,33 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $className = $input->getArgument(self::CLASS_NAME); + $output->writeln(''); $output->writeln(sprintf('DI configuration for the class %s', $className)); - $output->writeln(sprintf('- Preference: %s', $this->diInformation->getPreference($className))); - //$output->writeln(sprintf('- Constructor Parameters: %s', $this->diInformation->getPreference($className))); + $output->writeln(''); + $output->writeln(sprintf('Preference: %s', $this->diInformation->getPreference($className))); + $output->writeln(''); + $output->writeln("Constructor Parameters:"); + $parameters = []; + $diParametersConfiguration = $this->diInformation->getConfiguredConstructorParameters($className); + foreach ($this->diInformation->getConstructorParameters($className) as $parameter) { + $paramArray = [$parameter[0], $parameter[1], '']; + if (isset($diParametersConfiguration[$parameter[0]])) { + $paramArray[2] = $diParametersConfiguration[$parameter[0]]['instance']; + } + $parameters[] = $paramArray; + } + $table = new Table($output); + $table + ->setHeaders(array('Name', 'Type', 'Configured Type')) + ->setRows($parameters); + + $output->writeln($table->render()); + $output->writeln(''); + $output->writeln("Virtual Types:"); + foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) { + $output->writeln(' ' . $virtualType); + } + return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } } \ No newline at end of file diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index b8d3775e10807..715a70da93cb0 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -17,12 +17,21 @@ class Information */ private $preferences = []; + /** + * @var \Magento\Framework\ObjectManager\DefinitionInterface + */ + private $definitions; + /** * @param \Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig + * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions */ - public function __construct(\Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig) - { + public function __construct( + \Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig, + \Magento\Framework\ObjectManager\DefinitionInterface $definitions + ) { $this->objectManagerConfig = $objectManagerConfig; + $this->definitions = $definitions; } /** @@ -46,6 +55,19 @@ public function getPreference($className) * @return array|null */ public function getConstructorParameters($className) + { + $preferenceClass = $this->getPreference($className); + $parameters = $this->definitions->getParameters($preferenceClass); + return $parameters; + } + + /** + * Retrieve configured types of parameters of the constructor for the preference of the class + * + * @param $className + * @return array|null + */ + public function getConfiguredConstructorParameters($className) { $preferenceClass = $this->getPreference($className); return $this->objectManagerConfig->getArguments($preferenceClass); From 789cb19b03124dd6e7e6f733bdbf853998b7e546 Mon Sep 17 00:00:00 2001 From: andra lungu Date: Sat, 1 Apr 2017 19:35:14 -0700 Subject: [PATCH 08/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - added pluginlist model to recover the information about the plugin for the specified class --- .../Developer/Model/Di/Information.php | 22 +++- .../Magento/Developer/Model/Di/PluginList.php | 116 ++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/Developer/Model/Di/PluginList.php diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index b8d3775e10807..72c71b5d71300 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -5,6 +5,8 @@ */ namespace Magento\Developer\Model\Di; +use Magento\Developer\Model\Di\PluginList; + class Information { /** @@ -12,6 +14,11 @@ class Information */ private $objectManagerConfig; + /** + * @var \Magento\Developer\Model\Di\PluginList + */ + + private $pluginList; /** * @var string[] */ @@ -20,9 +27,12 @@ class Information /** * @param \Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig */ - public function __construct(\Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig) + public function __construct(\Magento\Framework\ObjectManager\ConfigInterface $objectManagerConfig, + \Magento\Developer\Model\Di\PluginList $pluginList + ) { $this->objectManagerConfig = $objectManagerConfig; + $this->pluginList = $pluginList; } /** @@ -68,4 +78,14 @@ public function getVirtualTypes($className) } return $virtualTypes; } + + /** + * @param $className + * @return array + */ + public function getPlugins($className) + { + return $this->pluginList->getPluginsListByClass($className); + + } } \ No newline at end of file diff --git a/app/code/Magento/Developer/Model/Di/PluginList.php b/app/code/Magento/Developer/Model/Di/PluginList.php new file mode 100644 index 0000000000000..96d4270b78766 --- /dev/null +++ b/app/code/Magento/Developer/Model/Di/PluginList.php @@ -0,0 +1,116 @@ + + * Date: 01/04/17 + * Time: 14.02 + */ + +namespace Magento\Developer\Model\Di; + +use Magento\Framework\Interception; +use Magento\Framework\Interception\DefinitionInterface; + +/** + * Provides plugin list configuration + */ +class PluginList extends Interception\PluginList\PluginList +{ + /** + * @var array + */ + private $pluginList = [ + DefinitionInterface::LISTENER_BEFORE => [], + DefinitionInterface::LISTENER_AROUND => [], + DefinitionInterface::LISTENER_AFTER => [] + ]; + /** + * @var array + */ + private $interceptedClasses; + + /** + * Returns plugins config + * + * @return array + */ + public function getPluginsConfig() + { + $this->_loadScopedData(); + + return $this->_inherited; + } + + /** + * Sets scope priority scheme + * + * @param array $areaCodes + * + * @return void + */ + public function setScopePriorityScheme($areaCodes) + { + $this->_scopePriorityScheme = $areaCodes; + } + + /** + * Whether scope code is current scope code + * + * @param string $scopeCode + * + * @return bool + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + protected function isCurrentScope($scopeCode) + { + return false; + } + + + private function getPlugins($type) + { + $this->_loadScopedData(); + if (!isset($this->_inherited[$type]) && !array_key_exists($type, $this->_inherited)) { + $this->_inheritPlugins($type); + } + return $this->_inherited[$type]; + } + + + /** + * @param $className + * + * @return array + * @throws \InvalidArgumentException + */ + public function getPluginsListByClass($className) + { + + $this->getPlugins($className); + + foreach ($this->_inherited[$className] as $pluginKey => $plugin) + { + foreach ($this->_definitions->getMethodList($plugin['instance']) as $pluginMethod => $methodTypes) { + + if ($methodTypes & DefinitionInterface::LISTENER_AROUND) { + if(!array_key_exists($plugin['instance'],$this->pluginList[DefinitionInterface::LISTENER_AROUND])) + $this->pluginList[DefinitionInterface::LISTENER_AROUND][$plugin['instance']] = []; + $this->pluginList[DefinitionInterface::LISTENER_AROUND][$plugin['instance']][] = $pluginMethod ; + + } + if ($methodTypes & DefinitionInterface::LISTENER_BEFORE) { + if(!array_key_exists($plugin['instance'],$this->pluginList[DefinitionInterface::LISTENER_BEFORE])) + $this->pluginList[DefinitionInterface::LISTENER_BEFORE][$plugin['instance']] = []; + $this->pluginList[DefinitionInterface::LISTENER_BEFORE][$plugin['instance']][] = $pluginMethod ; + + } + if ($methodTypes & DefinitionInterface::LISTENER_AFTER) { + if(!array_key_exists($plugin['instance'],$this->pluginList[DefinitionInterface::LISTENER_AFTER])) + $this->pluginList[DefinitionInterface::LISTENER_AFTER][$plugin['instance']] = []; + $this->pluginList[DefinitionInterface::LISTENER_AFTER][$plugin['instance']][] = $pluginMethod ; + } + } + } + return $this->pluginList; + } +} From 2608b1deebe4bd1ffb2cb6bfad758aa65e43cd6f Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sat, 1 Apr 2017 20:15:44 -0700 Subject: [PATCH 09/20] Merge branch 'cli-info-di' of https://github.com/springerin/magento2 into cli-info-di # Conflicts: # app/code/Magento/Developer/Model/Di/Information.php --- .../Console/Command/DiInfoCommand.php | 23 +++------ .../Developer/Model/Di/Information.php | 51 ++++++++++++++++--- .../Magento/Developer/Model/Di/PluginList.php | 44 +++++++++------- 3 files changed, 77 insertions(+), 41 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 02c3b6c4ded36..6ad48b2cdeb67 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -71,27 +71,20 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln(sprintf('Preference: %s', $this->diInformation->getPreference($className))); $output->writeln(''); $output->writeln("Constructor Parameters:"); - $parameters = []; - $diParametersConfiguration = $this->diInformation->getConfiguredConstructorParameters($className); - foreach ($this->diInformation->getConstructorParameters($className) as $parameter) { - $paramArray = [$parameter[0], $parameter[1], '']; - if (isset($diParametersConfiguration[$parameter[0]])) { - $paramArray[2] = $diParametersConfiguration[$parameter[0]]['instance']; - } - $parameters[] = $paramArray; - } $table = new Table($output); $table ->setHeaders(array('Name', 'Type', 'Configured Type')) - ->setRows($parameters); + ->setRows($this->diInformation->getParameters($className)); $output->writeln($table->render()); - $output->writeln(''); - $output->writeln("Virtual Types:"); - foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) { - $output->writeln(' ' . $virtualType); + $virtualTypes = $this->diInformation->getVirtualTypes($className); + if (!empty($virtualTypes)) { + $output->writeln(''); + $output->writeln("Virtual Types:"); + foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) { + $output->writeln(' ' . $virtualType); + } } - return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } } \ No newline at end of file diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index 2f848b0be4899..d9e3aca478dd1 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -17,13 +17,18 @@ class Information /** * @var \Magento\Developer\Model\Di\PluginList */ - private $pluginList; + /** * @var string[] */ private $preferences = []; + /** + * @var array + */ + private $virtualTypes = []; + /** * @var \Magento\Framework\ObjectManager\DefinitionInterface */ @@ -63,20 +68,40 @@ public function getPreference($className) * @param $className * @return array|null */ - public function getConstructorParameters($className) + private function getConstructorParameters($className) { $preferenceClass = $this->getPreference($className); $parameters = $this->definitions->getParameters($preferenceClass); return $parameters; } + /** + * Retrieve array of parameters for the class constructor + * + * @param $className + * @return array + */ + public function getParameters($className) + { + $result = []; + $diConfiguration = $this->getConfiguredConstructorParameters($className); + foreach ($this->getConstructorParameters($className) as $parameter) { + $paramArray = [$parameter[0], $parameter[1], '']; + if (isset($diConfiguration[$parameter[0]])) { + $paramArray[2] = $diConfiguration[$parameter[0]]['instance']; + } + $result[] = $paramArray; + } + return $result; + } + /** * Retrieve configured types of parameters of the constructor for the preference of the class * * @param $className * @return array|null */ - public function getConfiguredConstructorParameters($className) + private function getConfiguredConstructorParameters($className) { $preferenceClass = $this->getPreference($className); return $this->objectManagerConfig->getArguments($preferenceClass); @@ -91,13 +116,15 @@ public function getConfiguredConstructorParameters($className) public function getVirtualTypes($className) { $preference = $this->getPreference($className); - $virtualTypes = []; - foreach ($this->objectManagerConfig->getVirtualTypes() as $virtualType => $baseName) { - if ($baseName == $className || $baseName == $preference) { - $virtualTypes[] = $virtualType; + 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 $virtualTypes; + return $this->virtualTypes[$className]; } /** @@ -107,6 +134,14 @@ public function getVirtualTypes($className) public function getPlugins($className) { return $this->pluginList->getPluginsListByClass($className); + } + /** + * Is the class a virtual type + */ + public function isVirtualType($className) + { + $virtualTypes = $this->objectManagerConfig->getVirtualTypes(); + return isset($virtualTypes[$className]); } } \ No newline at end of file diff --git a/app/code/Magento/Developer/Model/Di/PluginList.php b/app/code/Magento/Developer/Model/Di/PluginList.php index 96d4270b78766..be25e680bf7a6 100644 --- a/app/code/Magento/Developer/Model/Di/PluginList.php +++ b/app/code/Magento/Developer/Model/Di/PluginList.php @@ -1,9 +1,7 @@ - * Date: 01/04/17 - * Time: 14.02 + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. */ namespace Magento\Developer\Model\Di; @@ -24,10 +22,6 @@ class PluginList extends Interception\PluginList\PluginList DefinitionInterface::LISTENER_AROUND => [], DefinitionInterface::LISTENER_AFTER => [] ]; - /** - * @var array - */ - private $interceptedClasses; /** * Returns plugins config @@ -66,7 +60,12 @@ protected function isCurrentScope($scopeCode) return false; } - + /** + * Load the plugins information + * + * @param $type + * @return array + */ private function getPlugins($type) { $this->_loadScopedData(); @@ -78,35 +77,44 @@ private function getPlugins($type) /** - * @param $className + * Return the list of plugins for the class * + * @param $className * @return array * @throws \InvalidArgumentException */ public function getPluginsListByClass($className) { - $this->getPlugins($className); - - foreach ($this->_inherited[$className] as $pluginKey => $plugin) - { + foreach ($this->_inherited[$className] as $pluginKey => $plugin) { foreach ($this->_definitions->getMethodList($plugin['instance']) as $pluginMethod => $methodTypes) { - if ($methodTypes & DefinitionInterface::LISTENER_AROUND) { - if(!array_key_exists($plugin['instance'],$this->pluginList[DefinitionInterface::LISTENER_AROUND])) + if (!array_key_exists( + $plugin['instance'], + $this->pluginList[DefinitionInterface::LISTENER_AROUND]) + ) { $this->pluginList[DefinitionInterface::LISTENER_AROUND][$plugin['instance']] = []; + } $this->pluginList[DefinitionInterface::LISTENER_AROUND][$plugin['instance']][] = $pluginMethod ; } if ($methodTypes & DefinitionInterface::LISTENER_BEFORE) { - if(!array_key_exists($plugin['instance'],$this->pluginList[DefinitionInterface::LISTENER_BEFORE])) + if (!array_key_exists( + $plugin['instance'], + $this->pluginList[DefinitionInterface::LISTENER_BEFORE]) + ) { $this->pluginList[DefinitionInterface::LISTENER_BEFORE][$plugin['instance']] = []; + } $this->pluginList[DefinitionInterface::LISTENER_BEFORE][$plugin['instance']][] = $pluginMethod ; } if ($methodTypes & DefinitionInterface::LISTENER_AFTER) { - if(!array_key_exists($plugin['instance'],$this->pluginList[DefinitionInterface::LISTENER_AFTER])) + if (!array_key_exists( + $plugin['instance'], + $this->pluginList[DefinitionInterface::LISTENER_AFTER]) + ) { $this->pluginList[DefinitionInterface::LISTENER_AFTER][$plugin['instance']] = []; + } $this->pluginList[DefinitionInterface::LISTENER_AFTER][$plugin['instance']][] = $pluginMethod ; } } From f580225e12fe6d39101ca3c3c2f2689a543e1010 Mon Sep 17 00:00:00 2001 From: andra lungu Date: Sat, 1 Apr 2017 20:28:34 -0700 Subject: [PATCH 10/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - added type name to plugin method instead of id --- .../Console/Command/DiInfoCommand.php | 19 +++++++++++++++ .../Magento/Developer/Model/Di/PluginList.php | 24 +++++++++---------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 6ad48b2cdeb67..c14be0fb5b512 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -85,6 +85,25 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln(' ' . $virtualType); } } + + $output->writeln(''); + $output->writeln("Plugins:"); + $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(array('Plugin', 'Method', 'Type')) + ->setRows($parameters); + + $output->writeln($table->render()); return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } } \ No newline at end of file diff --git a/app/code/Magento/Developer/Model/Di/PluginList.php b/app/code/Magento/Developer/Model/Di/PluginList.php index be25e680bf7a6..0e929eeb811ff 100644 --- a/app/code/Magento/Developer/Model/Di/PluginList.php +++ b/app/code/Magento/Developer/Model/Di/PluginList.php @@ -18,9 +18,9 @@ class PluginList extends Interception\PluginList\PluginList * @var array */ private $pluginList = [ - DefinitionInterface::LISTENER_BEFORE => [], - DefinitionInterface::LISTENER_AROUND => [], - DefinitionInterface::LISTENER_AFTER => [] + 'before' => [], + 'around' => [], + 'after' => [] ]; /** @@ -91,31 +91,31 @@ public function getPluginsListByClass($className) if ($methodTypes & DefinitionInterface::LISTENER_AROUND) { if (!array_key_exists( $plugin['instance'], - $this->pluginList[DefinitionInterface::LISTENER_AROUND]) + $this->pluginList['around']) ) { - $this->pluginList[DefinitionInterface::LISTENER_AROUND][$plugin['instance']] = []; + $this->pluginList['around'][$plugin['instance']] = []; } - $this->pluginList[DefinitionInterface::LISTENER_AROUND][$plugin['instance']][] = $pluginMethod ; + $this->pluginList['around'][$plugin['instance']][] = $pluginMethod ; } if ($methodTypes & DefinitionInterface::LISTENER_BEFORE) { if (!array_key_exists( $plugin['instance'], - $this->pluginList[DefinitionInterface::LISTENER_BEFORE]) + $this->pluginList['before']) ) { - $this->pluginList[DefinitionInterface::LISTENER_BEFORE][$plugin['instance']] = []; + $this->pluginList['before'][$plugin['instance']] = []; } - $this->pluginList[DefinitionInterface::LISTENER_BEFORE][$plugin['instance']][] = $pluginMethod ; + $this->pluginList['before'][$plugin['instance']][] = $pluginMethod ; } if ($methodTypes & DefinitionInterface::LISTENER_AFTER) { if (!array_key_exists( $plugin['instance'], - $this->pluginList[DefinitionInterface::LISTENER_AFTER]) + $this->pluginList['after']) ) { - $this->pluginList[DefinitionInterface::LISTENER_AFTER][$plugin['instance']] = []; + $this->pluginList['after'][$plugin['instance']] = []; } - $this->pluginList[DefinitionInterface::LISTENER_AFTER][$plugin['instance']][] = $pluginMethod ; + $this->pluginList['after'][$plugin['instance']][] = $pluginMethod ; } } } From e9c71c07b8dcc56b8e56440a8e952d8c273b31e5 Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sat, 1 Apr 2017 20:33:48 -0700 Subject: [PATCH 11/20] Merge branch 'cli-info-di' of https://github.com/springerin/magento2 into cli-info-di --- .../Console/Command/DiInfoCommand.php | 39 ++++++++++++------- .../Developer/Model/Di/Information.php | 26 ++++++++++--- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 6ad48b2cdeb67..79d15d009d4ad 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -67,24 +67,35 @@ protected function execute(InputInterface $input, OutputInterface $output) $className = $input->getArgument(self::CLASS_NAME); $output->writeln(''); $output->writeln(sprintf('DI configuration for the class %s', $className)); - $output->writeln(''); - $output->writeln(sprintf('Preference: %s', $this->diInformation->getPreference($className))); - $output->writeln(''); - $output->writeln("Constructor Parameters:"); - $table = new Table($output); - $table - ->setHeaders(array('Name', 'Type', 'Configured Type')) - ->setRows($this->diInformation->getParameters($className)); - $output->writeln($table->render()); - $virtualTypes = $this->diInformation->getVirtualTypes($className); - if (!empty($virtualTypes)) { + if ($this->diInformation->isVirtualType($className)) { + $output->writeln( + sprintf('It is Virtual Type for the Class %s', $this->diInformation->getVirtualTypeBase($className)) + ); + } else { + $preference = $this->diInformation->getPreference($className); $output->writeln(''); - $output->writeln("Virtual Types:"); - foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) { - $output->writeln(' ' . $virtualType); + $output->writeln(sprintf('Preference: %s', $preference)); + $output->writeln(''); + $output->writeln("Constructor Parameters:"); + + $virtualTypes = $this->diInformation->getVirtualTypes($preference); + if (!empty($virtualTypes)) { + $output->writeln(''); + $output->writeln("Virtual Types:"); + foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) { + $output->writeln(' ' . $virtualType); + } } + + $table = new Table($output); + $table + ->setHeaders(array('Name', 'Type', 'Configured Type')) + ->setRows($this->diInformation->getParameters($preference)); + + $output->writeln($table->render()); } + return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } } \ No newline at end of file diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index d9e3aca478dd1..a829382ca5963 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -70,8 +70,7 @@ public function getPreference($className) */ private function getConstructorParameters($className) { - $preferenceClass = $this->getPreference($className); - $parameters = $this->definitions->getParameters($preferenceClass); + $parameters = $this->definitions->getParameters($className); return $parameters; } @@ -103,8 +102,7 @@ public function getParameters($className) */ private function getConfiguredConstructorParameters($className) { - $preferenceClass = $this->getPreference($className); - return $this->objectManagerConfig->getArguments($preferenceClass); + return $this->objectManagerConfig->getArguments($className); } /** @@ -128,7 +126,7 @@ public function getVirtualTypes($className) } /** - * @param $className + * @param string $className * @return array */ public function getPlugins($className) @@ -138,10 +136,28 @@ public function getPlugins($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; + } } \ No newline at end of file From 2da051d79e9f8a10c11db01589790beed5c2fd31 Mon Sep 17 00:00:00 2001 From: andra lungu Date: Sat, 1 Apr 2017 20:41:02 -0700 Subject: [PATCH 12/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - added check existence of className in the inherited array --- app/code/Magento/Developer/Model/Di/PluginList.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/Magento/Developer/Model/Di/PluginList.php b/app/code/Magento/Developer/Model/Di/PluginList.php index 0e929eeb811ff..7950cd0eac8ba 100644 --- a/app/code/Magento/Developer/Model/Di/PluginList.php +++ b/app/code/Magento/Developer/Model/Di/PluginList.php @@ -86,6 +86,8 @@ private function getPlugins($type) public function getPluginsListByClass($className) { $this->getPlugins($className); + if(!isset($this->_inherited[$className])) + return $this->pluginList; foreach ($this->_inherited[$className] as $pluginKey => $plugin) { foreach ($this->_definitions->getMethodList($plugin['instance']) as $pluginMethod => $methodTypes) { if ($methodTypes & DefinitionInterface::LISTENER_AROUND) { From b85396949d917c67ec2814c806b998dbfd2fe0e0 Mon Sep 17 00:00:00 2001 From: andra lungu Date: Sat, 1 Apr 2017 20:44:58 -0700 Subject: [PATCH 13/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - added plugins info about preference --- .../Console/Command/DiInfoCommand.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 331227fc6b8be..423c6f956d0af 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -113,6 +113,25 @@ protected function execute(InputInterface $input, OutputInterface $output) ->setRows($parameters); $output->writeln($table->render()); + + $output->writeln(''); + $output->writeln("Preference Plugins:"); + $plugins = $this->diInformation->getPlugins($preference); + $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(array('Plugin', 'Method', 'Type')) + ->setRows($parameters); + + $output->writeln($table->render()); } return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } From 7cef5375bf0d6be11521f48a090f0f1c2641d1ee Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sun, 2 Apr 2017 06:35:08 -0700 Subject: [PATCH 14/20] Merge branch 'cli-info-di' of https://github.com/springerin/magento2 into cli-info-di - Added support for the virtual types --- .../Console/Command/DiInfoCommand.php | 75 +++++++++---------- .../Developer/Model/Di/Information.php | 18 ++++- 2 files changed, 52 insertions(+), 41 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 331227fc6b8be..a9d709839cbfe 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -68,52 +68,49 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln(''); $output->writeln(sprintf('DI configuration for the class %s', $className)); - if ($this->diInformation->isVirtualType($className)) { - $output->writeln( - sprintf('It is Virtual Type for the Class %s', $this->diInformation->getVirtualTypeBase($className)) - ); - } else { - $preference = $this->diInformation->getPreference($className); - $output->writeln(''); - $output->writeln(sprintf('Preference: %s', $preference)); + $output->writeln( + sprintf('It is Virtual Type for the Class %s', $this->diInformation->getVirtualTypeBase($className)) + ); + + $preference = $this->diInformation->getPreference($className); + $output->writeln(''); + $output->writeln(sprintf('Preference: %s', $preference)); + $output->writeln(''); + $output->writeln("Constructor Parameters:"); + $paramsTable = new Table($output); + $paramsTable + ->setHeaders(['Name', 'Type', 'Configured Type']); + $paramsTable->setRows([]); + $output->writeln($paramsTable->render()); + + $virtualTypes = $this->diInformation->getVirtualTypes($preference); + if (!empty($virtualTypes)) { $output->writeln(''); - $output->writeln("Constructor Parameters:"); - - $virtualTypes = $this->diInformation->getVirtualTypes($preference); - if (!empty($virtualTypes)) { - $output->writeln(''); - $output->writeln("Virtual Types:"); - foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) { - $output->writeln(' ' . $virtualType); - } + $output->writeln("Virtual Types:"); + foreach ($this->diInformation->getVirtualTypes($className) as $virtualType) { + $output->writeln(' ' . $virtualType); } + } - $table = new Table($output); - $table - ->setHeaders(array('Name', 'Type', 'Configured Type')) - ->setRows($this->diInformation->getParameters($preference)); - - $output->writeln($table->render()); - - $output->writeln(''); - $output->writeln("Plugins:"); - $plugins = $this->diInformation->getPlugins($className); - $parameters = []; - foreach ($plugins as $type => $plugin) { - foreach ($plugin as $instance => $pluginMethods){ - foreach ($pluginMethods as $pluginMethod){ - $parameters[] = [$instance, $pluginMethod, $type]; - } + $output->writeln(''); + $output->writeln("Plugins:"); + $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(array('Plugin', 'Method', 'Type')) - ->setRows($parameters); + $table = new Table($output); + $table + ->setHeaders(array('Plugin', 'Method', 'Type')) + ->setRows($parameters); + + $output->writeln($table->render()); - $output->writeln($table->render()); - } return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } } \ No newline at end of file diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index a829382ca5963..e826ca5f1f0b1 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -84,10 +84,24 @@ public function getParameters($className) { $result = []; $diConfiguration = $this->getConfiguredConstructorParameters($className); - foreach ($this->getConstructorParameters($className) as $parameter) { + $originalParameters = $this->isVirtualType($className) ? + $this->getConstructorParameters($this->getVirtualTypeBase($className)) : + $this->getConstructorParameters($className); + + foreach ($originalParameters as $parameter) { $paramArray = [$parameter[0], $parameter[1], '']; if (isset($diConfiguration[$parameter[0]])) { - $paramArray[2] = $diConfiguration[$parameter[0]]['instance']; + $configuredParameter = $diConfiguration[$parameter[0]]; + if (is_array($configuredParameter)) { + if (isset($configuredParameter['instance'])) { + $paramArray[2] = $configuredParameter['instance']; + } else { + //TODO + } + } else { + $paramArray[2] = $configuredParameter; + } + } $result[] = $paramArray; } From 3d1ffabff03278cd9133e77b5e619a5bbd4d3893 Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sun, 2 Apr 2017 07:01:01 -0700 Subject: [PATCH 15/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - added support for array type parameters --- .../Console/Command/DiInfoCommand.php | 12 +++++- .../Developer/Model/Di/Information.php | 37 +++++++++++++------ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 5762f825bd090..826cd41cc170d 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -79,8 +79,16 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln("Constructor Parameters:"); $paramsTable = new Table($output); $paramsTable - ->setHeaders(['Name', 'Type', 'Configured Type']); - $paramsTable->setRows([]); + ->setHeaders(['Name', '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()); $virtualTypes = $this->diInformation->getVirtualTypes($preference); diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index e826ca5f1f0b1..4c430d0a15d35 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -89,25 +89,38 @@ public function getParameters($className) $this->getConstructorParameters($className); foreach ($originalParameters as $parameter) { - $paramArray = [$parameter[0], $parameter[1], '']; + $paramArray = [$parameter[0], $parameter[1], is_array($parameter[3]) ? "" : $parameter[3]]; if (isset($diConfiguration[$parameter[0]])) { - $configuredParameter = $diConfiguration[$parameter[0]]; - if (is_array($configuredParameter)) { - if (isset($configuredParameter['instance'])) { - $paramArray[2] = $configuredParameter['instance']; - } else { - //TODO - } - } else { - $paramArray[2] = $configuredParameter; - } - + $paramArray[2] = $this->renderParameters($diConfiguration[$parameter[0]]); } $result[] = $paramArray; } return $result; } + /** + * Recursively retrieve array parameters + * + * @param $configuredParameter + * @return array|null + */ + private function renderParameters($configuredParameter) + { + $result = null; + if (is_array($configuredParameter)) { + if (isset($configuredParameter['instance'])) { + $result = $configuredParameter['instance']; + } else { + foreach ($configuredParameter as $keyName => $instance) { + $result[$keyName] = $this->renderParameters($instance); + } + } + } else { + $result = $configuredParameter; + } + return $result; + } + /** * Retrieve configured types of parameters of the constructor for the preference of the class * From 63b14ad58f93fabf083be1281c55dbd4fecde45b Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sun, 2 Apr 2017 13:16:21 -0700 Subject: [PATCH 16/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - added support for array type parameters --- .../Console/Command/DiInfoCommand.php | 104 +++++++++++------- .../Developer/Model/Di/Information.php | 17 +-- .../Magento/Developer/Model/Di/PluginList.php | 25 ++--- 3 files changed, 83 insertions(+), 63 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 826cd41cc170d..c0833b89a8187 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -1,9 +1,7 @@ - * Date: 01/04/17 - * Time: 14.02 + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. */ namespace Magento\Developer\Console\Command; @@ -59,27 +57,33 @@ protected function configure() } /** - * {@inheritdoc} - * @throws \InvalidArgumentException + * Print Info on Class/Interface preference + * + * @param string $className + * @param OutputInterface $output + * @return void */ - protected function execute(InputInterface $input, OutputInterface $output) + private function printPreference($className, $output) { - $className = $input->getArgument(self::CLASS_NAME); - $output->writeln(''); - $output->writeln(sprintf('DI configuration for the class %s', $className)); - - $output->writeln( - sprintf('It is Virtual Type for the Class %s', $this->diInformation->getVirtualTypeBase($className)) - ); - $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', 'Type', 'Configured Value']); + ->setHeaders(['Name', 'Requested Type', 'Configured Value']); $parameters = $this->diInformation->getParameters($className); $paramsTableArray = []; foreach ($parameters as $parameterRow) { @@ -90,8 +94,18 @@ protected function execute(InputInterface $input, OutputInterface $output) } $paramsTable->setRows($paramsTableArray); $output->writeln($paramsTable->render()); + } - $virtualTypes = $this->diInformation->getVirtualTypes($preference); + /** + * 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:"); @@ -99,14 +113,25 @@ protected function execute(InputInterface $input, OutputInterface $output) $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("Plugins:"); + $output->writeln($label); $plugins = $this->diInformation->getPlugins($className); $parameters = []; foreach ($plugins as $type => $plugin) { - foreach ($plugin as $instance => $pluginMethods){ - foreach ($pluginMethods as $pluginMethod){ + foreach ($plugin as $instance => $pluginMethods) { + foreach ($pluginMethods as $pluginMethod) { $parameters[] = [$instance, $pluginMethod, $type]; } } @@ -114,30 +139,31 @@ protected function execute(InputInterface $input, OutputInterface $output) $table = new Table($output); $table - ->setHeaders(array('Plugin', 'Method', 'Type')) + ->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->writeln(''); - $output->writeln("Preference Plugins:"); - $plugins = $this->diInformation->getPlugins($preference); - $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(array('Plugin', 'Method', 'Type')) - ->setRows($parameters); - - $output->writeln($table->render()); + $output->writeln(sprintf('DI configuration for the class %s', $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; } -} \ No newline at end of file +} diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index 4c430d0a15d35..003a32524d070 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -37,6 +37,7 @@ class Information /** * @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, @@ -65,7 +66,7 @@ public function getPreference($className) /** * Retrieve parameters of the constructor for the preference of the class * - * @param $className + * @param string $className * @return array|null */ private function getConstructorParameters($className) @@ -77,7 +78,7 @@ private function getConstructorParameters($className) /** * Retrieve array of parameters for the class constructor * - * @param $className + * @param string $className * @return array */ public function getParameters($className) @@ -101,7 +102,7 @@ public function getParameters($className) /** * Recursively retrieve array parameters * - * @param $configuredParameter + * @param string $configuredParameter * @return array|null */ private function renderParameters($configuredParameter) @@ -109,14 +110,14 @@ private function renderParameters($configuredParameter) $result = null; if (is_array($configuredParameter)) { if (isset($configuredParameter['instance'])) { - $result = $configuredParameter['instance']; + $result = 'instance of ' . $configuredParameter['instance']; } else { foreach ($configuredParameter as $keyName => $instance) { $result[$keyName] = $this->renderParameters($instance); } } } else { - $result = $configuredParameter; + $result = 'string ' . $configuredParameter; } return $result; } @@ -124,7 +125,7 @@ private function renderParameters($configuredParameter) /** * Retrieve configured types of parameters of the constructor for the preference of the class * - * @param $className + * @param string $className * @return array|null */ private function getConfiguredConstructorParameters($className) @@ -135,7 +136,7 @@ private function getConfiguredConstructorParameters($className) /** * Retrieve virtual types for the class and the preference of the class * - * @param $className + * @param string $className * @return array */ public function getVirtualTypes($className) @@ -187,4 +188,4 @@ public function getVirtualTypeBase($className) } return false; } -} \ No newline at end of file +} diff --git a/app/code/Magento/Developer/Model/Di/PluginList.php b/app/code/Magento/Developer/Model/Di/PluginList.php index 7950cd0eac8ba..ea505bc97fd75 100644 --- a/app/code/Magento/Developer/Model/Di/PluginList.php +++ b/app/code/Magento/Developer/Model/Di/PluginList.php @@ -63,7 +63,7 @@ protected function isCurrentScope($scopeCode) /** * Load the plugins information * - * @param $type + * @param string $type * @return array */ private function getPlugins($type) @@ -79,42 +79,34 @@ private function getPlugins($type) /** * Return the list of plugins for the class * - * @param $className + * @param string $className * @return array * @throws \InvalidArgumentException */ public function getPluginsListByClass($className) { $this->getPlugins($className); - if(!isset($this->_inherited[$className])) + if (!isset($this->_inherited[$className])) { return $this->pluginList; + } + foreach ($this->_inherited[$className] as $pluginKey => $plugin) { foreach ($this->_definitions->getMethodList($plugin['instance']) as $pluginMethod => $methodTypes) { if ($methodTypes & DefinitionInterface::LISTENER_AROUND) { - if (!array_key_exists( - $plugin['instance'], - $this->pluginList['around']) - ) { + if (!array_key_exists($plugin['instance'], $this->pluginList['around'])) { $this->pluginList['around'][$plugin['instance']] = []; } $this->pluginList['around'][$plugin['instance']][] = $pluginMethod ; - } if ($methodTypes & DefinitionInterface::LISTENER_BEFORE) { - if (!array_key_exists( - $plugin['instance'], - $this->pluginList['before']) - ) { + if (!array_key_exists($plugin['instance'], $this->pluginList['before'])) { $this->pluginList['before'][$plugin['instance']] = []; } $this->pluginList['before'][$plugin['instance']][] = $pluginMethod ; } if ($methodTypes & DefinitionInterface::LISTENER_AFTER) { - if (!array_key_exists( - $plugin['instance'], - $this->pluginList['after']) - ) { + if (!array_key_exists($plugin['instance'], $this->pluginList['after'])) { $this->pluginList['after'][$plugin['instance']] = []; } $this->pluginList['after'][$plugin['instance']][] = $pluginMethod ; @@ -124,3 +116,4 @@ public function getPluginsListByClass($className) return $this->pluginList; } } + From 22120a261a755b5b63b2bc48d9aed8fcc057b4cd Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Sun, 2 Apr 2017 14:01:53 -0700 Subject: [PATCH 17/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - added unit test for Information class --- .../Developer/Model/Di/Information.php | 4 +- .../Test/Unit/Model/Di/InformationTest.php | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 app/code/Magento/Developer/Test/Unit/Model/Di/InformationTest.php diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index 003a32524d070..0b122b41cd6d3 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -5,8 +5,6 @@ */ namespace Magento\Developer\Model\Di; -use Magento\Developer\Model\Di\PluginList; - class Information { /** @@ -87,7 +85,7 @@ public function getParameters($className) $diConfiguration = $this->getConfiguredConstructorParameters($className); $originalParameters = $this->isVirtualType($className) ? $this->getConstructorParameters($this->getVirtualTypeBase($className)) : - $this->getConstructorParameters($className); + $this->getConstructorParameters($this->getPreference($className)); foreach ($originalParameters as $parameter) { $paramArray = [$parameter[0], $parameter[1], is_array($parameter[3]) ? "" : $parameter[3]]; diff --git a/app/code/Magento/Developer/Test/Unit/Model/Di/InformationTest.php b/app/code/Magento/Developer/Test/Unit/Model/Di/InformationTest.php new file mode 100644 index 0000000000000..643a106cf9880 --- /dev/null +++ b/app/code/Magento/Developer/Test/Unit/Model/Di/InformationTest.php @@ -0,0 +1,74 @@ +objectManagerConfig = $this->getMockBuilder(ConfigInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->definitions = $this->getMockBuilder(DefinitionInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->pluginList = $this->getMockBuilder(PluginList::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->object = (new ObjectManager($this))->getObject(Information::class, [ + 'objectManagerConfig' => $this->objectManagerConfig, + 'definitions' => $this->definitions, + 'pluginList' => $this->pluginList, + ]); + } + + public function testGetPreference() + { + $this->objectManagerConfig->expects($this->any()) + ->method('getPreference') + ->with(Information::class) + ->willReturn(Information::class); + $this->assertEquals(Information::class, $this->object->getPreference(Information::class)); + } + + public function testGetParameters() + { + $this->objectManagerConfig->expects($this->any()) + ->method('getArguments') + ->with(Information::class) + ->willReturn(Information::class); + $this->assertEquals([], $this->object->getParameters(Information::class)); + } +} From 2d0365611a14087825148e68ede1818c4102ff50 Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Tue, 11 Apr 2017 20:37:35 -0500 Subject: [PATCH 18/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - fixed the virtual types message - added information about the area --- .../Developer/Console/Command/DiInfoCommand.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index c0833b89a8187..79fcae74cf2c1 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -152,11 +152,15 @@ private function printPlugins($className, $output, $label) 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', $className)); - $output->writeln( - sprintf('It is Virtual Type for the Class %s', $this->diInformation->getVirtualTypeBase($className)) - ); + $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); From 3b4f21f56ddd2cd13da505b2d940603006618d20 Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Tue, 11 Apr 2017 20:57:20 -0500 Subject: [PATCH 19/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - fixed unit test --- .../Developer/Test/Unit/Model/Di/InformationTest.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Developer/Test/Unit/Model/Di/InformationTest.php b/app/code/Magento/Developer/Test/Unit/Model/Di/InformationTest.php index 643a106cf9880..35fefce8834cc 100644 --- a/app/code/Magento/Developer/Test/Unit/Model/Di/InformationTest.php +++ b/app/code/Magento/Developer/Test/Unit/Model/Di/InformationTest.php @@ -65,10 +65,17 @@ public function testGetPreference() public function testGetParameters() { + $this->definitions->expects($this->any()) + ->method('getParameters') + ->with(Information::class) + ->willReturn([['information', Information::class, false, null]]); $this->objectManagerConfig->expects($this->any()) - ->method('getArguments') + ->method('getPreference') ->with(Information::class) ->willReturn(Information::class); - $this->assertEquals([], $this->object->getParameters(Information::class)); + $this->assertEquals( + [['information', Information::class, null]], + $this->object->getParameters(Information::class) + ); } } From fd9321c519ba9ca3d7d58fba4faf1bef54d448eb Mon Sep 17 00:00:00 2001 From: Eugene Tulika Date: Tue, 11 Apr 2017 21:53:22 -0500 Subject: [PATCH 20/20] magento-hackathon/Imagine2017#11: Create new CLI command: Collect Dependency Injection Info for the Interfaces and Classes - fixed issue with the empty constructor parameters list - fixed static test --- .../Developer/Model/Di/Information.php | 4 ++ .../Magento/Developer/Model/Di/PluginList.php | 72 ++++++++++++------- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/app/code/Magento/Developer/Model/Di/Information.php b/app/code/Magento/Developer/Model/Di/Information.php index 0b122b41cd6d3..37d609fa17c7e 100644 --- a/app/code/Magento/Developer/Model/Di/Information.php +++ b/app/code/Magento/Developer/Model/Di/Information.php @@ -87,6 +87,10 @@ public function getParameters($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]) ? "" : $parameter[3]]; if (isset($diConfiguration[$parameter[0]])) { diff --git a/app/code/Magento/Developer/Model/Di/PluginList.php b/app/code/Magento/Developer/Model/Di/PluginList.php index ea505bc97fd75..12bd661414efb 100644 --- a/app/code/Magento/Developer/Model/Di/PluginList.php +++ b/app/code/Magento/Developer/Model/Di/PluginList.php @@ -14,13 +14,31 @@ */ class PluginList extends Interception\PluginList\PluginList { + /**#@+ + * Constants for the plugin types + */ + const PLUGIN_TYPE_BEFORE = 'before'; + const PLUGIN_TYPE_AROUND = 'around'; + const PLUGIN_TYPE_AFTER = 'after'; + /**#@-*/ + /** * @var array */ private $pluginList = [ - 'before' => [], - 'around' => [], - 'after' => [] + self::PLUGIN_TYPE_BEFORE => [], + self::PLUGIN_TYPE_AROUND => [], + self::PLUGIN_TYPE_AFTER => [] + ]; + + /** + * Mapping of plugin type codes to plugin types + * @var array + */ + private $pluginTypeMapping = [ + DefinitionInterface::LISTENER_AROUND => self::PLUGIN_TYPE_AROUND, + DefinitionInterface::LISTENER_BEFORE => self::PLUGIN_TYPE_BEFORE, + DefinitionInterface::LISTENER_AFTER => self::PLUGIN_TYPE_AFTER ]; /** @@ -75,7 +93,6 @@ private function getPlugins($type) return $this->_inherited[$type]; } - /** * Return the list of plugins for the class * @@ -92,28 +109,35 @@ public function getPluginsListByClass($className) foreach ($this->_inherited[$className] as $pluginKey => $plugin) { foreach ($this->_definitions->getMethodList($plugin['instance']) as $pluginMethod => $methodTypes) { - if ($methodTypes & DefinitionInterface::LISTENER_AROUND) { - if (!array_key_exists($plugin['instance'], $this->pluginList['around'])) { - $this->pluginList['around'][$plugin['instance']] = []; - } - $this->pluginList['around'][$plugin['instance']][] = $pluginMethod ; - } - if ($methodTypes & DefinitionInterface::LISTENER_BEFORE) { - if (!array_key_exists($plugin['instance'], $this->pluginList['before'])) { - $this->pluginList['before'][$plugin['instance']] = []; - } - $this->pluginList['before'][$plugin['instance']][] = $pluginMethod ; - - } - if ($methodTypes & DefinitionInterface::LISTENER_AFTER) { - if (!array_key_exists($plugin['instance'], $this->pluginList['after'])) { - $this->pluginList['after'][$plugin['instance']] = []; - } - $this->pluginList['after'][$plugin['instance']][] = $pluginMethod ; - } + $this->addPluginToList($plugin['instance'], $pluginMethod, $methodTypes, + DefinitionInterface::LISTENER_AROUND + ); + $this->addPluginToList($plugin['instance'], $pluginMethod, $methodTypes, + DefinitionInterface::LISTENER_BEFORE + ); + $this->addPluginToList($plugin['instance'], $pluginMethod, $methodTypes, + DefinitionInterface::LISTENER_AFTER + ); } } return $this->pluginList; } -} + /** + * Add plugin to the appropriate type bucket + * + * @param string $pluginInstance + * @param string $pluginMethod + * @param int $methodTypes + * @param int $typeCode + */ + private function addPluginToList($pluginInstance, $pluginMethod, $methodTypes, $typeCode) + { + if ($methodTypes & $typeCode) { + if (!array_key_exists($pluginInstance, $this->pluginList[$this->pluginTypeMapping[$typeCode]])) { + $this->pluginList[$this->pluginTypeMapping[$typeCode]][$pluginInstance] = []; + } + $this->pluginList[$this->pluginTypeMapping[$typeCode]][$pluginInstance][] = $pluginMethod ; + } + } +}