Skip to content

Commit 7acdc6d

Browse files
committed
Escape disabled modules in code compilation - setup:di:compile
1 parent 11be3df commit 7acdc6d

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

setup/src/Magento/Setup/Console/Command/DiCompileCommand.php

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
<?php
22
/**
3-
* Copyright 2015 Adobe
3+
* Copyright 2025 Adobe
44
* All Rights Reserved.
55
*/
66

77
namespace Magento\Setup\Console\Command;
88

9-
use Magento\Framework\App\ObjectManager;
10-
use Magento\Framework\Filesystem\DriverInterface;
11-
use Magento\Framework\Filesystem\Io\File;
12-
use Symfony\Component\Console\Input\InputInterface;
13-
use Symfony\Component\Console\Output\OutputInterface;
14-
use Magento\Framework\Filesystem;
15-
use Magento\Framework\App\Filesystem\DirectoryList;
169
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\App\Interception\Cache\CompiledConfig;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\App\ObjectManager\ConfigWriterInterface;
1714
use Magento\Framework\Component\ComponentRegistrar;
1815
use Magento\Framework\Config\ConfigOptionsListConstants;
16+
use Magento\Framework\Console\Cli;
17+
use Magento\Framework\Filesystem;
18+
use Magento\Framework\Filesystem\DriverInterface;
19+
use Magento\Framework\Filesystem\Io\File;
1920
use Magento\Setup\Model\ObjectManagerProvider;
2021
use Magento\Setup\Module\Di\App\Task\Manager;
21-
use Magento\Setup\Module\Di\App\Task\OperationFactory;
2222
use Magento\Setup\Module\Di\App\Task\OperationException;
23+
use Magento\Setup\Module\Di\App\Task\OperationFactory;
2324
use Magento\Setup\Module\Di\App\Task\OperationInterface;
25+
use Magento\Setup\Module\Di\Code\Generator\PluginList;
26+
use Magento\Setup\Module\Di\Code\Reader\ClassesScanner;
27+
use Magento\Setup\Module\Di\Compiler\Config\Chain\BackslashTrim;
28+
use Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitution;
29+
use Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving;
30+
use Magento\Setup\Module\Di\Compiler\Config\ModificationChain;
31+
use Magento\Setup\Module\Di\Compiler\Log\Writer\Console;
2432
use Symfony\Component\Console\Command\Command;
2533
use Symfony\Component\Console\Helper\ProgressBar;
26-
use Magento\Framework\Console\Cli;
34+
use Symfony\Component\Console\Input\InputInterface;
35+
use Symfony\Component\Console\Output\OutputInterface;
2736

2837
/**
2938
* Command to run compile in single-tenant mode
@@ -156,20 +165,33 @@ protected function execute(InputInterface $input, OutputInterface $output)
156165
}
157166

158167
$modulePaths = $this->componentRegistrar->getPaths(ComponentRegistrar::MODULE);
168+
$moduleStatuses = $this->deploymentConfig->get(ConfigOptionsListConstants::KEY_MODULES);
169+
if (!$moduleStatuses || !is_array($moduleStatuses)) {
170+
return Cli::RETURN_FAILURE;
171+
}
172+
$enabledModuleStatuses = array_filter($moduleStatuses, function ($enabled) {
173+
return $enabled === 1;
174+
});
175+
$enabledModules = array_keys($enabledModuleStatuses);
176+
177+
$modulePathsEnabled = array_filter($modulePaths, function ($path, $module) use ($enabledModules) {
178+
return in_array($module, $enabledModules, true);
179+
}, ARRAY_FILTER_USE_BOTH);
180+
159181
$libraryPaths = $this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY);
160182
$setupPath = $this->directoryList->getPath(DirectoryList::SETUP);
161183
$generationPath = $this->directoryList->getPath(DirectoryList::GENERATED_CODE);
162184

163185
$this->objectManager->get(\Magento\Framework\App\Cache::class)->clean();
164186
$compiledPathsList = [
165-
'application' => $modulePaths,
187+
'application' => $modulePathsEnabled,
166188
'library' => $libraryPaths,
167189
'setup' => $setupPath,
168190
'generated_helpers' => $generationPath
169191
];
170192

171193
$this->excludedPathsList = [
172-
'application' => $this->getExcludedModulePaths($modulePaths),
194+
'application' => $this->getExcludedModulePaths($modulePathsEnabled),
173195
'framework' => $this->getExcludedLibraryPaths($libraryPaths),
174196
'setup' => $this->getExcludedSetupPaths($setupPath),
175197
];
@@ -207,11 +229,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
207229
$progressBar->display();
208230

209231
$this->taskManager->process(
210-
function (OperationInterface $operation) use ($progressBar) {
232+
function (OperationInterface $operation) use ($progressBar): void {
211233
$progressBar->setMessage($operation->getName() . '...');
212234
$progressBar->display();
213235
},
214-
function (OperationInterface $operation) use ($progressBar) {
236+
function (OperationInterface $operation) use ($progressBar): void {
215237
$progressBar->advance();
216238
}
217239
);
@@ -327,39 +349,35 @@ private function configureObjectManager(OutputInterface $output)
327349
{
328350
$this->objectManager->configure(
329351
[
330-
'preferences' => [\Magento\Framework\App\ObjectManager\ConfigWriterInterface::class =>
331-
\Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class,
332-
], \Magento\Setup\Module\Di\Compiler\Config\ModificationChain::class => [
352+
'preferences' => [ConfigWriterInterface::class => ObjectManager\ConfigWriter\Filesystem::class,
353+
], ModificationChain::class => [
333354
'arguments' => [
334355
'modificationsList' => [
335356
'BackslashTrim' => [
336-
'instance' =>
337-
\Magento\Setup\Module\Di\Compiler\Config\Chain\BackslashTrim::class
357+
'instance' => BackslashTrim::class
338358
],
339359
'PreferencesResolving' => [
340-
'instance' =>
341-
\Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class
360+
'instance' => PreferencesResolving::class
342361
],
343362
'InterceptorSubstitution' => [
344-
'instance' =>
345-
\Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitution::class
363+
'instance' => InterceptorSubstitution::class
346364
],
347365
'InterceptionPreferencesResolving' => [
348-
'instance' => \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class
366+
'instance' => PreferencesResolving::class
349367
],
350368
]
351369
]
352-
], \Magento\Setup\Module\Di\Code\Generator\PluginList::class => [
370+
], PluginList::class => [
353371
'arguments' => [
354372
'cache' => [
355-
'instance' => \Magento\Framework\App\Interception\Cache\CompiledConfig::class
373+
'instance' => CompiledConfig::class
356374
]
357375
]
358-
], \Magento\Setup\Module\Di\Code\Reader\ClassesScanner::class => [
376+
], ClassesScanner::class => [
359377
'arguments' => [
360378
'excludePatterns' => $this->excludedPathsList
361379
]
362-
], \Magento\Setup\Module\Di\Compiler\Log\Writer\Console::class => [
380+
], Console::class => [
363381
'arguments' => [
364382
'output' => $output,
365383
]

setup/src/Magento/Setup/Test/Unit/Console/Command/DiCompileCommandTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -168,10 +168,17 @@ public function testExecute()
168168
$writeDirectory->expects($this->atLeastOnce())->method('delete');
169169
$this->filesystemMock->expects($this->atLeastOnce())->method('getDirectoryWrite')->willReturn($writeDirectory);
170170

171-
$this->deploymentConfigMock->expects($this->once())
171+
$this->deploymentConfigMock->expects($this->exactly(2))
172172
->method('get')
173173
->with(ConfigOptionsListConstants::KEY_MODULES)
174-
->willReturn(['Magento_Catalog' => 1]);
174+
->willReturn(
175+
[
176+
'Magento_Catalog' => 1,
177+
'Module_Test' => 0
178+
]
179+
);
180+
$this->componentRegistrarMock->expects($this->exactly(2))->method('getPaths');
181+
175182
$progressBar = new ProgressBar($this->outputMock);
176183

177184
$this->objectManagerMock->expects($this->once())->method('configure');

0 commit comments

Comments
 (0)