Skip to content

Commit aa28366

Browse files
Merge forwardport of #11052 to 2.3-develop branch
Applied pull request patch https://github.com/magento/magento2/pull/11052.patch (created by @jokeputs) based on commit(s): 1. 036c628 2. 7295017 3. aa1f29c 4. 5f3068b 5. b1b604e Fixed GitHub Issues in 2.3-develop branch: - #9918: Magento 2 automatically disables maintenance mode after certain actions (reported by @royvanos)
2 parents 97dd9ad + 6fe6280 commit aa28366

File tree

12 files changed

+455
-203
lines changed

12 files changed

+455
-203
lines changed

app/code/Magento/Deploy/Model/Mode.php

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Deploy\App\Mode\ConfigProvider;
1010
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\Console\MaintenanceModeEnabler;
1112
use Magento\Framework\App\DeploymentConfig\Reader;
1213
use Magento\Framework\App\DeploymentConfig\Writer;
1314
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -49,11 +50,6 @@ class Mode
4950
*/
5051
private $reader;
5152

52-
/**
53-
* @var MaintenanceMode
54-
*/
55-
private $maintenanceMode;
56-
5753
/**
5854
* @var Filesystem
5955
*/
@@ -78,16 +74,24 @@ class Mode
7874
*/
7975
private $emulatedAreaProcessor;
8076

77+
/**
78+
* @var MaintenanceModeEnabler
79+
*/
80+
private $maintenanceModeEnabler;
81+
8182
/**
8283
* @param InputInterface $input
8384
* @param OutputInterface $output
8485
* @param Writer $writer
8586
* @param Reader $reader
86-
* @param MaintenanceMode $maintenanceMode
87+
* @param MaintenanceMode $maintenanceMode deprecated, use $maintenanceModeEnabler instead
8788
* @param Filesystem $filesystem
8889
* @param ConfigProvider $configProvider
8990
* @param ProcessorFacadeFactory $processorFacadeFactory
9091
* @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor
92+
* @param MaintenanceModeEnabler $maintenanceModeEnabler
93+
*
94+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
9195
*/
9296
public function __construct(
9397
InputInterface $input,
@@ -98,13 +102,13 @@ public function __construct(
98102
Filesystem $filesystem,
99103
ConfigProvider $configProvider = null,
100104
ProcessorFacadeFactory $processorFacadeFactory = null,
101-
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null
105+
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null,
106+
MaintenanceModeEnabler $maintenanceModeEnabler = null
102107
) {
103108
$this->input = $input;
104109
$this->output = $output;
105110
$this->writer = $writer;
106111
$this->reader = $reader;
107-
$this->maintenanceMode = $maintenanceMode;
108112
$this->filesystem = $filesystem;
109113

110114
$this->configProvider =
@@ -113,6 +117,8 @@ public function __construct(
113117
$processorFacadeFactory ?: ObjectManager::getInstance()->get(ProcessorFacadeFactory::class);
114118
$this->emulatedAreaProcessor =
115119
$emulatedAreaProcessor ?: ObjectManager::getInstance()->get(EmulatedAdminhtmlAreaProcessor::class);
120+
$this->maintenanceModeEnabler =
121+
$maintenanceModeEnabler ?: ObjectManager::getInstance()->get(MaintenanceModeEnabler::class);
116122
}
117123

118124
/**
@@ -123,19 +129,23 @@ public function __construct(
123129
*/
124130
public function enableProductionMode()
125131
{
126-
$this->enableMaintenanceMode($this->output);
127-
$previousMode = $this->getMode();
128-
try {
129-
// We have to turn on production mode before generation.
130-
// We need this to enable generation of the "min" files.
131-
$this->setStoreMode(State::MODE_PRODUCTION);
132-
$this->filesystem->regenerateStatic($this->output);
133-
} catch (LocalizedException $e) {
134-
// We have to return store mode to previous state in case of error.
135-
$this->setStoreMode($previousMode);
136-
throw $e;
137-
}
138-
$this->disableMaintenanceMode($this->output);
132+
$this->maintenanceModeEnabler->executeInMaintenanceMode(
133+
function () {
134+
$previousMode = $this->getMode();
135+
try {
136+
// We have to turn on production mode before generation.
137+
// We need this to enable generation of the "min" files.
138+
$this->setStoreMode(State::MODE_PRODUCTION);
139+
$this->filesystem->regenerateStatic($this->output);
140+
} catch (LocalizedException $e) {
141+
// We have to return store mode to previous state in case of error.
142+
$this->setStoreMode($previousMode);
143+
throw $e;
144+
}
145+
},
146+
$this->output,
147+
false
148+
);
139149
}
140150

141151
/**
@@ -237,28 +247,4 @@ private function saveAppConfigs($mode)
237247
$this->output->writeln('Config "' . $path . ' = ' . $item['value'] . '" has been saved.');
238248
}
239249
}
240-
241-
/**
242-
* Enable maintenance mode
243-
*
244-
* @param OutputInterface $output
245-
* @return void
246-
*/
247-
protected function enableMaintenanceMode(OutputInterface $output)
248-
{
249-
$this->maintenanceMode->set(true);
250-
$output->writeln('Enabled maintenance mode');
251-
}
252-
253-
/**
254-
* Disable maintenance mode
255-
*
256-
* @param OutputInterface $output
257-
* @return void
258-
*/
259-
protected function disableMaintenanceMode(OutputInterface $output)
260-
{
261-
$this->maintenanceMode->set(false);
262-
$output->writeln('Disabled maintenance mode');
263-
}
264250
}

app/code/Magento/Deploy/Test/Unit/Model/ModeTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Deploy\Model\Filesystem;
1313
use Magento\Deploy\Model\Mode;
1414
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\App\Console\MaintenanceModeEnabler;
1516
use Magento\Framework\App\DeploymentConfig\Reader;
1617
use Magento\Framework\App\DeploymentConfig\Writer;
1718
use Magento\Framework\App\MaintenanceMode;
@@ -124,7 +125,8 @@ protected function setUp()
124125
$this->filesystemMock,
125126
$this->configProvider,
126127
$this->processorFacadeFactory,
127-
$this->emulatedAreaProcessor
128+
$this->emulatedAreaProcessor,
129+
new MaintenanceModeEnabler($this->maintenanceMock)
128130
);
129131
}
130132

app/code/Magento/Theme/Console/Command/ThemeUninstallCommand.php

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
namespace Magento\Theme\Console\Command;
88

9-
use Magento\Framework\App\Area;
109
use Magento\Framework\App\Cache;
10+
use Magento\Framework\App\Console\MaintenanceModeEnabler;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\App\MaintenanceMode;
1213
use Magento\Framework\App\State\CleanupFiles;
1314
use Magento\Framework\Composer\ComposerInformation;
@@ -39,13 +40,6 @@ class ThemeUninstallCommand extends Command
3940
const INPUT_KEY_THEMES = 'theme';
4041
const INPUT_KEY_CLEAR_STATIC_CONTENT = 'clear-static-content';
4142

42-
/**
43-
* Maintenance Mode
44-
*
45-
* @var MaintenanceMode
46-
*/
47-
private $maintenanceMode;
48-
4943
/**
5044
* Composer general dependency checker
5145
*
@@ -116,20 +110,28 @@ class ThemeUninstallCommand extends Command
116110
*/
117111
private $themeDependencyChecker;
118112

113+
/**
114+
* @var MaintenanceModeEnabler
115+
*/
116+
private $maintenanceModeEnabler;
117+
119118
/**
120119
* Constructor
121120
*
122121
* @param Cache $cache
123122
* @param CleanupFiles $cleanupFiles
124123
* @param ComposerInformation $composer
125-
* @param MaintenanceMode $maintenanceMode
124+
* @param MaintenanceMode $maintenanceMode deprecated, use $maintenanceModeEnabler instead
126125
* @param DependencyChecker $dependencyChecker
127126
* @param Collection $themeCollection
128127
* @param BackupRollbackFactory $backupRollbackFactory
129128
* @param ThemeValidator $themeValidator
130129
* @param ThemePackageInfo $themePackageInfo
131130
* @param ThemeUninstaller $themeUninstaller
132131
* @param ThemeDependencyChecker $themeDependencyChecker
132+
* @param MaintenanceModeEnabler $maintenanceModeEnabler
133+
*
134+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
133135
*/
134136
public function __construct(
135137
Cache $cache,
@@ -142,19 +144,21 @@ public function __construct(
142144
ThemeValidator $themeValidator,
143145
ThemePackageInfo $themePackageInfo,
144146
ThemeUninstaller $themeUninstaller,
145-
ThemeDependencyChecker $themeDependencyChecker
147+
ThemeDependencyChecker $themeDependencyChecker,
148+
MaintenanceModeEnabler $maintenanceModeEnabler = null
146149
) {
147150
$this->cache = $cache;
148151
$this->cleanupFiles = $cleanupFiles;
149152
$this->composer = $composer;
150-
$this->maintenanceMode = $maintenanceMode;
151153
$this->dependencyChecker = $dependencyChecker;
152154
$this->themeCollection = $themeCollection;
153155
$this->backupRollbackFactory = $backupRollbackFactory;
154156
$this->themeValidator = $themeValidator;
155157
$this->themePackageInfo = $themePackageInfo;
156158
$this->themeUninstaller = $themeUninstaller;
157159
$this->themeDependencyChecker = $themeDependencyChecker;
160+
$this->maintenanceModeEnabler =
161+
$maintenanceModeEnabler ?: ObjectManager::getInstance()->get(MaintenanceModeEnabler::class);
158162
parent::__construct();
159163
}
160164

@@ -214,27 +218,32 @@ protected function execute(InputInterface $input, OutputInterface $output)
214218
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
215219
}
216220

217-
try {
218-
$output->writeln('<info>Enabling maintenance mode</info>');
219-
$this->maintenanceMode->set(true);
220-
if ($input->getOption(self::INPUT_KEY_BACKUP_CODE)) {
221-
$time = time();
222-
$codeBackup = $this->backupRollbackFactory->create($output);
223-
$codeBackup->codeBackup($time);
224-
}
225-
226-
$this->themeUninstaller->uninstallRegistry($output, $themePaths);
227-
$this->themeUninstaller->uninstallCode($output, $themePaths);
221+
$result = $this->maintenanceModeEnabler->executeInMaintenanceMode(
222+
function () use ($input, $output, $themePaths) {
223+
try {
224+
if ($input->getOption(self::INPUT_KEY_BACKUP_CODE)) {
225+
$time = time();
226+
$codeBackup = $this->backupRollbackFactory->create($output);
227+
$codeBackup->codeBackup($time);
228+
}
229+
230+
$this->themeUninstaller->uninstallRegistry($output, $themePaths);
231+
$this->themeUninstaller->uninstallCode($output, $themePaths);
232+
233+
$this->cleanup($input, $output);
234+
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
235+
} catch (\Exception $e) {
236+
$output->writeln('<error>' . $e->getMessage() . '</error>');
237+
$output->writeln('<error>Please disable maintenance mode after you resolved above issues</error>');
238+
// we must have an exit code higher than zero to indicate something was wrong
239+
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
240+
}
241+
},
242+
$output,
243+
true
244+
);
228245

229-
$this->cleanup($input, $output);
230-
$output->writeln('<info>Disabling maintenance mode</info>');
231-
$this->maintenanceMode->set(false);
232-
} catch (\Exception $e) {
233-
$output->writeln('<error>' . $e->getMessage() . '</error>');
234-
$output->writeln('<error>Please disable maintenance mode after you resolved above issues</error>');
235-
// we must have an exit code higher than zero to indicate something was wrong
236-
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
237-
}
246+
return $result;
238247
}
239248

240249
/**

app/code/Magento/Theme/Test/Unit/Console/Command/ThemeUninstallCommandTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
namespace Magento\Theme\Test\Unit\Console\Command;
88

9+
use Magento\Framework\App\Console\MaintenanceModeEnabler;
910
use Magento\Theme\Console\Command\ThemeUninstallCommand;
10-
use Magento\Theme\Model\Theme\themePackageInfo;
11+
use Magento\Theme\Model\Theme\ThemePackageInfo;
1112
use Magento\Theme\Model\Theme\ThemeUninstaller;
1213
use Magento\Theme\Model\Theme\ThemeDependencyChecker;
1314
use Symfony\Component\Console\Tester\CommandTester;
@@ -56,7 +57,7 @@ class ThemeUninstallCommandTest extends \PHPUnit\Framework\TestCase
5657
/**
5758
* Theme Validator
5859
*
59-
* @var ThemeValidator|\PHPUnit_Framework_MockObject_MockObject
60+
* @var \Magento\Theme\Model\ThemeValidator|\PHPUnit_Framework_MockObject_MockObject
6061
*/
6162
private $themeValidator;
6263

@@ -107,7 +108,8 @@ protected function setUp()
107108
$this->themeValidator,
108109
$this->themePackageInfo,
109110
$this->themeUninstaller,
110-
$this->themeDependencyChecker
111+
$this->themeDependencyChecker,
112+
new MaintenanceModeEnabler($this->maintenanceMode)
111113
);
112114
$this->tester = new CommandTester($this->command);
113115
}

0 commit comments

Comments
 (0)