From 7acdc6d549d56d2df3092179e56d4816dbd729d4 Mon Sep 17 00:00:00 2001 From: Luca Fuser Date: Sat, 8 Mar 2025 16:41:47 +0100 Subject: [PATCH 1/3] Escape disabled modules in code compilation - setup:di:compile --- .../Console/Command/DiCompileCommand.php | 74 ++++++++++++------- .../Console/Command/DiCompileCommandTest.php | 15 +++- 2 files changed, 57 insertions(+), 32 deletions(-) diff --git a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php index 76f537185aab4..64aa73d990026 100644 --- a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php @@ -1,29 +1,38 @@ componentRegistrar->getPaths(ComponentRegistrar::MODULE); + $moduleStatuses = $this->deploymentConfig->get(ConfigOptionsListConstants::KEY_MODULES); + if (!$moduleStatuses || !is_array($moduleStatuses)) { + return Cli::RETURN_FAILURE; + } + $enabledModuleStatuses = array_filter($moduleStatuses, function ($enabled) { + return $enabled === 1; + }); + $enabledModules = array_keys($enabledModuleStatuses); + + $modulePathsEnabled = array_filter($modulePaths, function ($path, $module) use ($enabledModules) { + return in_array($module, $enabledModules, true); + }, ARRAY_FILTER_USE_BOTH); + $libraryPaths = $this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY); $setupPath = $this->directoryList->getPath(DirectoryList::SETUP); $generationPath = $this->directoryList->getPath(DirectoryList::GENERATED_CODE); $this->objectManager->get(\Magento\Framework\App\Cache::class)->clean(); $compiledPathsList = [ - 'application' => $modulePaths, + 'application' => $modulePathsEnabled, 'library' => $libraryPaths, 'setup' => $setupPath, 'generated_helpers' => $generationPath ]; $this->excludedPathsList = [ - 'application' => $this->getExcludedModulePaths($modulePaths), + 'application' => $this->getExcludedModulePaths($modulePathsEnabled), 'framework' => $this->getExcludedLibraryPaths($libraryPaths), 'setup' => $this->getExcludedSetupPaths($setupPath), ]; @@ -207,11 +229,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $progressBar->display(); $this->taskManager->process( - function (OperationInterface $operation) use ($progressBar) { + function (OperationInterface $operation) use ($progressBar): void { $progressBar->setMessage($operation->getName() . '...'); $progressBar->display(); }, - function (OperationInterface $operation) use ($progressBar) { + function (OperationInterface $operation) use ($progressBar): void { $progressBar->advance(); } ); @@ -327,39 +349,35 @@ private function configureObjectManager(OutputInterface $output) { $this->objectManager->configure( [ - 'preferences' => [\Magento\Framework\App\ObjectManager\ConfigWriterInterface::class => - \Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class, - ], \Magento\Setup\Module\Di\Compiler\Config\ModificationChain::class => [ + 'preferences' => [ConfigWriterInterface::class => ObjectManager\ConfigWriter\Filesystem::class, + ], ModificationChain::class => [ 'arguments' => [ 'modificationsList' => [ 'BackslashTrim' => [ - 'instance' => - \Magento\Setup\Module\Di\Compiler\Config\Chain\BackslashTrim::class + 'instance' => BackslashTrim::class ], 'PreferencesResolving' => [ - 'instance' => - \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class + 'instance' => PreferencesResolving::class ], 'InterceptorSubstitution' => [ - 'instance' => - \Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitution::class + 'instance' => InterceptorSubstitution::class ], 'InterceptionPreferencesResolving' => [ - 'instance' => \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class + 'instance' => PreferencesResolving::class ], ] ] - ], \Magento\Setup\Module\Di\Code\Generator\PluginList::class => [ + ], PluginList::class => [ 'arguments' => [ 'cache' => [ - 'instance' => \Magento\Framework\App\Interception\Cache\CompiledConfig::class + 'instance' => CompiledConfig::class ] ] - ], \Magento\Setup\Module\Di\Code\Reader\ClassesScanner::class => [ + ], ClassesScanner::class => [ 'arguments' => [ 'excludePatterns' => $this->excludedPathsList ] - ], \Magento\Setup\Module\Di\Compiler\Log\Writer\Console::class => [ + ], Console::class => [ 'arguments' => [ 'output' => $output, ] diff --git a/setup/src/Magento/Setup/Test/Unit/Console/Command/DiCompileCommandTest.php b/setup/src/Magento/Setup/Test/Unit/Console/Command/DiCompileCommandTest.php index 00f04dbf9799d..678c6286fd733 100644 --- a/setup/src/Magento/Setup/Test/Unit/Console/Command/DiCompileCommandTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Console/Command/DiCompileCommandTest.php @@ -1,7 +1,7 @@ expects($this->atLeastOnce())->method('delete'); $this->filesystemMock->expects($this->atLeastOnce())->method('getDirectoryWrite')->willReturn($writeDirectory); - $this->deploymentConfigMock->expects($this->once()) + $this->deploymentConfigMock->expects($this->exactly(2)) ->method('get') ->with(ConfigOptionsListConstants::KEY_MODULES) - ->willReturn(['Magento_Catalog' => 1]); + ->willReturn( + [ + 'Magento_Catalog' => 1, + 'Module_Test' => 0 + ] + ); + $this->componentRegistrarMock->expects($this->exactly(2))->method('getPaths'); + $progressBar = new ProgressBar($this->outputMock); $this->objectManagerMock->expects($this->once())->method('configure'); From 7953e54d83f878c2fcd61d0a833821362f0220b5 Mon Sep 17 00:00:00 2001 From: Luca Fuser Date: Tue, 11 Mar 2025 23:29:20 +0100 Subject: [PATCH 2/3] Copyright format adjust, clean code and fix cyclomatic complexity in unit test --- .../Console/Command/DiCompileCommand.php | 12 ++++---- .../Console/Command/DiCompileCommandTest.php | 30 ++++++++----------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php index 64aa73d990026..e5e067cb13922 100644 --- a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php @@ -1,6 +1,6 @@ componentRegistrar->getPaths(ComponentRegistrar::MODULE); $moduleStatuses = $this->deploymentConfig->get(ConfigOptionsListConstants::KEY_MODULES); + if (!$moduleStatuses || !is_array($moduleStatuses)) { + $output->writeln('Deployment config not available.'); return Cli::RETURN_FAILURE; } - $enabledModuleStatuses = array_filter($moduleStatuses, function ($enabled) { - return $enabled === 1; - }); - $enabledModules = array_keys($enabledModuleStatuses); - $modulePathsEnabled = array_filter($modulePaths, function ($path, $module) use ($enabledModules) { - return in_array($module, $enabledModules, true); + $modulePathsEnabled = array_filter($modulePaths, function ($path, $module) use ($moduleStatuses) { + return ($moduleStatuses[$module] ?? 0) === 1; }, ARRAY_FILTER_USE_BOTH); $libraryPaths = $this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY); diff --git a/setup/src/Magento/Setup/Test/Unit/Console/Command/DiCompileCommandTest.php b/setup/src/Magento/Setup/Test/Unit/Console/Command/DiCompileCommandTest.php index 678c6286fd733..f638321d6fe23 100644 --- a/setup/src/Magento/Setup/Test/Unit/Console/Command/DiCompileCommandTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Console/Command/DiCompileCommandTest.php @@ -1,6 +1,6 @@ with(ProgressBar::class) ->willReturn($progressBar); + $operations = [ + OperationFactory::REPOSITORY_GENERATOR, + OperationFactory::DATA_ATTRIBUTES_GENERATOR, + OperationFactory::APPLICATION_CODE_GENERATOR, + OperationFactory::INTERCEPTION, + OperationFactory::AREA_CONFIG_GENERATOR, + OperationFactory::INTERCEPTION_CACHE, + OperationFactory::APPLICATION_ACTION_LIST_GENERATOR, + OperationFactory::PLUGIN_LIST_GENERATOR, + ]; $this->managerMock->expects($this->exactly(9))->method('addOperation') - ->willReturnCallback(function ($arg1, $arg2) { + ->willReturnCallback(function ($arg1, $arg2) use ($operations) { if ($arg1 == OperationFactory::PROXY_GENERATOR && empty($arg2)) { return null; - } elseif ($arg1 == OperationFactory::REPOSITORY_GENERATOR) { - return null; - } elseif ($arg1 == OperationFactory::DATA_ATTRIBUTES_GENERATOR) { - return null; - } elseif ($arg1 == OperationFactory::APPLICATION_CODE_GENERATOR) { - return null; - } elseif ($arg1 == OperationFactory::INTERCEPTION) { - return null; - } elseif ($arg1 == OperationFactory::AREA_CONFIG_GENERATOR) { - return null; - } elseif ($arg1 == OperationFactory::INTERCEPTION_CACHE) { - return null; - } elseif ($arg1 == OperationFactory::APPLICATION_ACTION_LIST_GENERATOR) { - return null; - } elseif ($arg1 == OperationFactory::PLUGIN_LIST_GENERATOR) { + } elseif (in_array($arg1, $operations)) { return null; } }); From d520b7c320f29a08aa491b27632cd47756b26ed0 Mon Sep 17 00:00:00 2001 From: Luca Fuser Date: Fri, 14 Mar 2025 15:24:00 +0100 Subject: [PATCH 3/3] Code review - removed unnecessary deploy config check --- setup/src/Magento/Setup/Console/Command/DiCompileCommand.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php index e5e067cb13922..745c0e5b0d721 100644 --- a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php @@ -167,11 +167,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $modulePaths = $this->componentRegistrar->getPaths(ComponentRegistrar::MODULE); $moduleStatuses = $this->deploymentConfig->get(ConfigOptionsListConstants::KEY_MODULES); - if (!$moduleStatuses || !is_array($moduleStatuses)) { - $output->writeln('Deployment config not available.'); - return Cli::RETURN_FAILURE; - } - $modulePathsEnabled = array_filter($modulePaths, function ($path, $module) use ($moduleStatuses) { return ($moduleStatuses[$module] ?? 0) === 1; }, ARRAY_FILTER_USE_BOTH);