From b3804fa5eb41c520f7f139ee72c81ed00cfe71e1 Mon Sep 17 00:00:00 2001 From: rostislav Date: Sun, 26 May 2024 04:53:21 +0300 Subject: [PATCH 1/9] [issue #38758] Area code parameter for dev:di:info CLI command --- .../Console/Command/DiInfoCommand.php | 80 +++++++++++++++---- 1 file changed, 64 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 2235aa5e5d9df..d8e937f465ae0 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -7,51 +7,59 @@ namespace Magento\Developer\Console\Command; use Magento\Developer\Model\Di\Information; +use Magento\Framework\App\ObjectManager; 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; +use Magento\Framework\App\AreaList; class DiInfoCommand extends Command { - /** - * Command name - */ - const COMMAND_NAME = 'dev:di:info'; + public const COMMAND_NAME = 'dev:di:info'; + + public const CLASS_NAME = 'class'; + + public const AREA_CODE = 'area'; /** - * input name + * @var Information */ - const CLASS_NAME = 'class'; + private Information $diInformation; /** - * @var Information + * @var AreaList */ - private $diInformation; + private AreaList $areaList; /** * @param Information $diInformation + * @param AreaList $areaList */ public function __construct( - Information $diInformation + Information $diInformation, + AreaList $areaList ) { + $this->areaList = $areaList; $this->diInformation = $diInformation; parent::__construct(); } /** - * {@inheritdoc} + * @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') - ]); + ->setDescription('Provides information on Dependency Injection configuration for the Command.') + ->setDefinition([ + new InputArgument(self::CLASS_NAME, InputArgument::REQUIRED, 'Class name'), + new InputArgument(self::AREA_CODE, InputArgument::OPTIONAL, 'Area Code') + ]); parent::configure(); } @@ -146,15 +154,19 @@ private function printPlugins($className, $output, $label) } /** - * {@inheritdoc} + * @inheritdoc * @throws \InvalidArgumentException */ protected function execute(InputInterface $input, OutputInterface $output) { + $area = 'GLOBAL'; + if ($area = $input->getArgument(self::AREA_CODE)) { + $this->setDiArea($area); + } $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)); + $output->writeln(sprintf('DI configuration for the class %s in the %s area', $className, strtoupper($area))); if ($this->diInformation->isVirtualType($className)) { $output->writeln( @@ -170,4 +182,40 @@ protected function execute(InputInterface $input, OutputInterface $output) return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } + + /** + * Set Area for DI Configuration + * + * @param string $area + * @return void + * @throws \InvalidArgumentException + */ + private function setDiArea($area) + { + if ($this->validateAreaCodeFromInput($area)) { + $objectManager = ObjectManager::getInstance(); + + $objectManager->configure( + $objectManager + ->get(\Magento\Framework\App\ObjectManager\ConfigLoader::class) + ->load($area) + ); + $objectManager->get(\Magento\Framework\Config\ScopeInterface::class) + ->setCurrentScope($area); + } else { + throw new InvalidArgumentException(sprintf('The "%s" area code does not exist', $area)); + } + } + + /** + * Validate Input + * + * @param string $area + * @return bool + */ + private function validateAreaCodeFromInput($area) + { + $availableAreaCodes = $this->areaList->getCodes(); + return in_array($area, $availableAreaCodes, true); + } } From f969a179639018321c8bd8bd71d01fe699f4de0f Mon Sep 17 00:00:00 2001 From: rostislav Date: Sun, 26 May 2024 05:23:24 +0300 Subject: [PATCH 2/9] [issue #38758] Area code parameter for dev:di:info CLI command ( fixes ) --- app/code/Magento/Developer/Console/Command/DiInfoCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index d8e937f465ae0..a2c07a9d27b4e 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -15,6 +15,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Helper\Table; use Magento\Framework\App\AreaList; +use Magento\Framework\App\Area; class DiInfoCommand extends Command { @@ -159,8 +160,8 @@ private function printPlugins($className, $output, $label) */ protected function execute(InputInterface $input, OutputInterface $output) { - $area = 'GLOBAL'; - if ($area = $input->getArgument(self::AREA_CODE)) { + $area = $input->getArgument(self::AREA_CODE) ?? Area::AREA_GLOBAL; + if ($area !== Area::AREA_GLOBAL) { $this->setDiArea($area); } $className = $input->getArgument(self::CLASS_NAME); From c90d2dddda7b46fcf810af357cd6d7464f4fd36e Mon Sep 17 00:00:00 2001 From: rostislav Date: Fri, 30 Aug 2024 11:58:27 +0300 Subject: [PATCH 3/9] issue-38758 added new constructor param as null and handle with Object Manager, codestyle fixes --- .../Developer/Console/Command/DiInfoCommand.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index fedb4f42c605a..c0b5563c08cff 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -50,9 +50,10 @@ class DiInfoCommand extends Command */ public function __construct( Information $diInformation, - AreaList $areaList - ) { - $this->areaList = $areaList; + ?AreaList $areaList = null + ) + { + $this->areaList = $areaList ?? \Magento\Framework\App\ObjectManager::getInstance()->get(AreaList::class); $this->diInformation = $diInformation; parent::__construct(); } @@ -202,7 +203,7 @@ protected function execute(InputInterface $input, OutputInterface $output) * @return void * @throws \InvalidArgumentException */ - private function setDiArea($area) + private function setDiArea($area): void { if ($this->validateAreaCodeFromInput($area)) { $objectManager = ObjectManager::getInstance(); @@ -225,7 +226,7 @@ private function setDiArea($area) * @param string $area * @return bool */ - private function validateAreaCodeFromInput($area) + private function validateAreaCodeFromInput($area): bool { $availableAreaCodes = $this->areaList->getCodes(); return in_array($area, $availableAreaCodes, true); From d72c7aef1fb23d1e5d296d711d1edf0d87c93b91 Mon Sep 17 00:00:00 2001 From: rostislav Date: Fri, 30 Aug 2024 17:45:39 +0300 Subject: [PATCH 4/9] issue-38758 code refactoring ( dev:di:info ) --- .../Developer/Console/Command/DiInfoCommand.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index c0b5563c08cff..97cc3a0d534b9 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -53,8 +53,8 @@ public function __construct( ?AreaList $areaList = null ) { - $this->areaList = $areaList ?? \Magento\Framework\App\ObjectManager::getInstance()->get(AreaList::class); $this->diInformation = $diInformation; + $this->areaList = $areaList ?? \Magento\Framework\App\ObjectManager::getInstance()->get(AreaList::class); parent::__construct(); } @@ -208,11 +208,12 @@ private function setDiArea($area): void if ($this->validateAreaCodeFromInput($area)) { $objectManager = ObjectManager::getInstance(); - $objectManager->configure( - $objectManager - ->get(\Magento\Framework\App\ObjectManager\ConfigLoader::class) - ->load($area) - ); + $areaOmConfiguration = $objectManager + ->get(\Magento\Framework\App\ObjectManager\ConfigLoader::class) + ->load($area); + + $objectManager->configure($areaOmConfiguration); + $objectManager->get(\Magento\Framework\Config\ScopeInterface::class) ->setCurrentScope($area); } else { From ed345a16c17e6b7fdee0327a2c86bada356eed65 Mon Sep 17 00:00:00 2001 From: rostislav Date: Wed, 4 Sep 2024 15:10:14 +0300 Subject: [PATCH 5/9] issue-38703 codestyle issues fix --- .../Console/Command/DiInfoCommand.php | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 97cc3a0d534b9..6f6993f2d04af 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -7,7 +7,7 @@ namespace Magento\Developer\Console\Command; use Magento\Developer\Model\Di\Information; -use Magento\Framework\App\ObjectManager; +use Magento\Framework\ObjectManagerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Console\Helper\Table; @@ -19,6 +19,12 @@ class DiInfoCommand extends Command { + /** + * @var ObjectManagerInterface + */ + private ObjectManagerInterface $objectManager; + + /** * Command name */ @@ -50,10 +56,12 @@ class DiInfoCommand extends Command */ public function __construct( Information $diInformation, + ObjectManagerInterface $objectManager, ?AreaList $areaList = null ) { $this->diInformation = $diInformation; + $this->objectManager = $objectManager; $this->areaList = $areaList ?? \Magento\Framework\App\ObjectManager::getInstance()->get(AreaList::class); parent::__construct(); } @@ -203,18 +211,16 @@ protected function execute(InputInterface $input, OutputInterface $output) * @return void * @throws \InvalidArgumentException */ - private function setDiArea($area): void + private function setDiArea(string $area): void { if ($this->validateAreaCodeFromInput($area)) { - $objectManager = ObjectManager::getInstance(); - - $areaOmConfiguration = $objectManager + $areaOmConfiguration = $this->objectManager ->get(\Magento\Framework\App\ObjectManager\ConfigLoader::class) ->load($area); - $objectManager->configure($areaOmConfiguration); + $this->objectManager->configure($areaOmConfiguration); - $objectManager->get(\Magento\Framework\Config\ScopeInterface::class) + $this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class) ->setCurrentScope($area); } else { throw new InvalidArgumentException(sprintf('The "%s" area code does not exist', $area)); From 6447c548773beb0c41a8714435c93bc609ce522e Mon Sep 17 00:00:00 2001 From: rostislav Date: Wed, 4 Sep 2024 15:12:18 +0300 Subject: [PATCH 6/9] issue-38703 codestyle ( prettify ) --- app/code/Magento/Developer/Console/Command/DiInfoCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 6f6993f2d04af..4a6414dcaa5a1 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -55,9 +55,9 @@ class DiInfoCommand extends Command * @param AreaList $areaList */ public function __construct( - Information $diInformation, + Information $diInformation, ObjectManagerInterface $objectManager, - ?AreaList $areaList = null + ?AreaList $areaList = null ) { $this->diInformation = $diInformation; From 0a20ef5084b55ad5e88c190454dc72aab0b7042f Mon Sep 17 00:00:00 2001 From: rostislav Date: Wed, 4 Sep 2024 16:18:39 +0300 Subject: [PATCH 7/9] issue-38758 added test coverage ( integration tests ) --- .../Console/Command/DiInfoCommandTest.php | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Developer/Console/Command/DiInfoCommandTest.php diff --git a/dev/tests/integration/testsuite/Magento/Developer/Console/Command/DiInfoCommandTest.php b/dev/tests/integration/testsuite/Magento/Developer/Console/Command/DiInfoCommandTest.php new file mode 100644 index 0000000000000..bbf6794284f6c --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Developer/Console/Command/DiInfoCommandTest.php @@ -0,0 +1,141 @@ +objectManager = Bootstrap::getObjectManager(); + $this->informationMock = $this->createMock(Information::class); + $this->areaListMock = $this->createMock(AreaList::class); + $this->command = new DiInfoCommand($this->informationMock, $this->objectManager, $this->areaListMock); + } + + /** + * @return void + */ + public function testExecuteWithGlobalArea(): void + { + $this->informationMock->expects($this->any()) + ->method('getPreference') + ->with('Magento\Framework\App\RouterList') + ->willReturn('Magento\Framework\App\RouterList'); + + $this->informationMock->expects($this->any()) + ->method('getParameters') + ->with('Magento\Framework\App\RouterList') + ->willReturn([ + ['objectManager', 'Magento\Framework\ObjectManagerInterface', null], + ['routerList', null, null] + ]); + + $this->informationMock->expects($this->once()) + ->method('getVirtualTypes') + ->with('Magento\Framework\App\RouterList') + ->willReturn([]); + + $this->informationMock->expects($this->any()) + ->method('getPlugins') + ->with('Magento\Framework\App\RouterList') + ->willReturn([ + 'before' => [], + 'around' => [], + 'after' => [] + ]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute( + [ + DiInfoCommand::CLASS_NAME => "Magento\Framework\App\RouterList", + DiInfoCommand::AREA_CODE => null + ], + ); + $this->assertStringContainsString( + 'DI configuration for the class Magento\Framework\App\RouterList in the GLOBAL area', + $commandTester->getDisplay() + ); + } + + /** + * @return void + */ + public function testExecuteWithAreaCode(): void + { + $className = "Magento\Framework\App\RouterList"; + $this->informationMock->expects($this->any()) + ->method('getPreference') + ->with($className) + ->willReturn($className); + + $this->informationMock->expects($this->any()) + ->method('getParameters') + ->with($className) + ->willReturn([ + ['objectManager', 'Magento\Framework\ObjectManagerInterface', null], + ['routerList', null, null] + ]); + + $this->informationMock->expects($this->once()) + ->method('getVirtualTypes') + ->with($className) + ->willReturn([]); + + $this->informationMock->expects($this->any()) + ->method('getPlugins') + ->with($className) + ->willReturn([ + 'before' => [], + 'around' => [], + 'after' => [] + ]); + + $this->areaListMock->expects($this->once()) + ->method('getCodes') + ->willReturn(['frontend', 'adminhtml']); + + $commandTester = new CommandTester($this->command); + $commandTester->execute( + [ + DiInfoCommand::CLASS_NAME => "$className", + DiInfoCommand::AREA_CODE => "adminhtml" + ], + ); + + $this->assertStringContainsString( + "DI configuration for the class $className in the ADMINHTML area", + $commandTester->getDisplay() + ); + } +} From 5b294149399402f824581b8b3a8f435f498dab50 Mon Sep 17 00:00:00 2001 From: rostislav Date: Wed, 11 Sep 2024 07:47:57 +0300 Subject: [PATCH 8/9] issue-38758 codestyle fixes ( static tests ) --- .../Developer/Console/Command/DiInfoCommand.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php index 4a6414dcaa5a1..81390d06df6b1 100644 --- a/app/code/Magento/Developer/Console/Command/DiInfoCommand.php +++ b/app/code/Magento/Developer/Console/Command/DiInfoCommand.php @@ -17,6 +17,9 @@ use Magento\Framework\App\AreaList; use Magento\Framework\App\Area; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class DiInfoCommand extends Command { /** @@ -24,7 +27,6 @@ class DiInfoCommand extends Command */ private ObjectManagerInterface $objectManager; - /** * Command name */ @@ -52,14 +54,14 @@ class DiInfoCommand extends Command /** * @param Information $diInformation - * @param AreaList $areaList + * @param ObjectManagerInterface $objectManager + * @param AreaList|null $areaList */ public function __construct( Information $diInformation, ObjectManagerInterface $objectManager, ?AreaList $areaList = null - ) - { + ) { $this->diInformation = $diInformation; $this->objectManager = $objectManager; $this->areaList = $areaList ?? \Magento\Framework\App\ObjectManager::getInstance()->get(AreaList::class); From 671583d1ace0ddf73c62c715e6b05b6769e33347 Mon Sep 17 00:00:00 2001 From: rostislav Date: Wed, 11 Sep 2024 07:55:00 +0300 Subject: [PATCH 9/9] issue-38758 codestyle fixes ( strict_types and copyright ) --- .../Magento/Developer/Console/Command/DiInfoCommandTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dev/tests/integration/testsuite/Magento/Developer/Console/Command/DiInfoCommandTest.php b/dev/tests/integration/testsuite/Magento/Developer/Console/Command/DiInfoCommandTest.php index bbf6794284f6c..080c512786246 100644 --- a/dev/tests/integration/testsuite/Magento/Developer/Console/Command/DiInfoCommandTest.php +++ b/dev/tests/integration/testsuite/Magento/Developer/Console/Command/DiInfoCommandTest.php @@ -1,4 +1,9 @@