Skip to content

Commit

Permalink
[2.3-develop] Forwardport of #11052
Browse files Browse the repository at this point in the history
  • Loading branch information
magento-engcom-team committed Feb 6, 2018
1 parent 15d1a96 commit 490785f
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 203 deletions.
76 changes: 31 additions & 45 deletions app/code/Magento/Deploy/Model/Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Deploy\App\Mode\ConfigProvider;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Console\MaintenanceModeEnabler;
use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\App\Filesystem\DirectoryList;
Expand Down Expand Up @@ -49,11 +50,6 @@ class Mode
*/
private $reader;

/**
* @var MaintenanceMode
*/
private $maintenanceMode;

/**
* @var Filesystem
*/
Expand All @@ -78,16 +74,24 @@ class Mode
*/
private $emulatedAreaProcessor;

/**
* @var MaintenanceModeEnabler
*/
private $maintenanceModeEnabler;

/**
* @param InputInterface $input
* @param OutputInterface $output
* @param Writer $writer
* @param Reader $reader
* @param MaintenanceMode $maintenanceMode
* @param MaintenanceMode $maintenanceMode deprecated, use $maintenanceModeEnabler instead
* @param Filesystem $filesystem
* @param ConfigProvider $configProvider
* @param ProcessorFacadeFactory $processorFacadeFactory
* @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor
* @param MaintenanceModeEnabler $maintenanceModeEnabler
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
InputInterface $input,
Expand All @@ -98,13 +102,13 @@ public function __construct(
Filesystem $filesystem,
ConfigProvider $configProvider = null,
ProcessorFacadeFactory $processorFacadeFactory = null,
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null,
MaintenanceModeEnabler $maintenanceModeEnabler = null
) {
$this->input = $input;
$this->output = $output;
$this->writer = $writer;
$this->reader = $reader;
$this->maintenanceMode = $maintenanceMode;
$this->filesystem = $filesystem;

$this->configProvider =
Expand All @@ -113,6 +117,8 @@ public function __construct(
$processorFacadeFactory ?: ObjectManager::getInstance()->get(ProcessorFacadeFactory::class);
$this->emulatedAreaProcessor =
$emulatedAreaProcessor ?: ObjectManager::getInstance()->get(EmulatedAdminhtmlAreaProcessor::class);
$this->maintenanceModeEnabler =
$maintenanceModeEnabler ?: ObjectManager::getInstance()->get(MaintenanceModeEnabler::class);
}

/**
Expand All @@ -123,19 +129,23 @@ public function __construct(
*/
public function enableProductionMode()
{
$this->enableMaintenanceMode($this->output);
$previousMode = $this->getMode();
try {
// We have to turn on production mode before generation.
// We need this to enable generation of the "min" files.
$this->setStoreMode(State::MODE_PRODUCTION);
$this->filesystem->regenerateStatic($this->output);
} catch (LocalizedException $e) {
// We have to return store mode to previous state in case of error.
$this->setStoreMode($previousMode);
throw $e;
}
$this->disableMaintenanceMode($this->output);
$this->maintenanceModeEnabler->executeInMaintenanceMode(
function () {
$previousMode = $this->getMode();
try {
// We have to turn on production mode before generation.
// We need this to enable generation of the "min" files.
$this->setStoreMode(State::MODE_PRODUCTION);
$this->filesystem->regenerateStatic($this->output);
} catch (LocalizedException $e) {
// We have to return store mode to previous state in case of error.
$this->setStoreMode($previousMode);
throw $e;
}
},
$this->output,
false
);
}

/**
Expand Down Expand Up @@ -237,28 +247,4 @@ private function saveAppConfigs($mode)
$this->output->writeln('Config "' . $path . ' = ' . $value . '" has been saved.');
}
}

/**
* Enable maintenance mode
*
* @param OutputInterface $output
* @return void
*/
protected function enableMaintenanceMode(OutputInterface $output)
{
$this->maintenanceMode->set(true);
$output->writeln('Enabled maintenance mode');
}

/**
* Disable maintenance mode
*
* @param OutputInterface $output
* @return void
*/
protected function disableMaintenanceMode(OutputInterface $output)
{
$this->maintenanceMode->set(false);
$output->writeln('Disabled maintenance mode');
}
}
4 changes: 3 additions & 1 deletion app/code/Magento/Deploy/Test/Unit/Model/ModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Deploy\Model\Filesystem;
use Magento\Deploy\Model\Mode;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Console\MaintenanceModeEnabler;
use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\App\MaintenanceMode;
Expand Down Expand Up @@ -124,7 +125,8 @@ protected function setUp()
$this->filesystemMock,
$this->configProvider,
$this->processorFacadeFactory,
$this->emulatedAreaProcessor
$this->emulatedAreaProcessor,
new MaintenanceModeEnabler($this->maintenanceMock)
);
}

Expand Down
71 changes: 40 additions & 31 deletions app/code/Magento/Theme/Console/Command/ThemeUninstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

namespace Magento\Theme\Console\Command;

use Magento\Framework\App\Area;
use Magento\Framework\App\Cache;
use Magento\Framework\App\Console\MaintenanceModeEnabler;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\MaintenanceMode;
use Magento\Framework\App\State\CleanupFiles;
use Magento\Framework\Composer\ComposerInformation;
Expand Down Expand Up @@ -39,13 +40,6 @@ class ThemeUninstallCommand extends Command
const INPUT_KEY_THEMES = 'theme';
const INPUT_KEY_CLEAR_STATIC_CONTENT = 'clear-static-content';

/**
* Maintenance Mode
*
* @var MaintenanceMode
*/
private $maintenanceMode;

/**
* Composer general dependency checker
*
Expand Down Expand Up @@ -116,20 +110,28 @@ class ThemeUninstallCommand extends Command
*/
private $themeDependencyChecker;

/**
* @var MaintenanceModeEnabler
*/
private $maintenanceModeEnabler;

/**
* Constructor
*
* @param Cache $cache
* @param CleanupFiles $cleanupFiles
* @param ComposerInformation $composer
* @param MaintenanceMode $maintenanceMode
* @param MaintenanceMode $maintenanceMode deprecated, use $maintenanceModeEnabler instead
* @param DependencyChecker $dependencyChecker
* @param Collection $themeCollection
* @param BackupRollbackFactory $backupRollbackFactory
* @param ThemeValidator $themeValidator
* @param ThemePackageInfo $themePackageInfo
* @param ThemeUninstaller $themeUninstaller
* @param ThemeDependencyChecker $themeDependencyChecker
* @param MaintenanceModeEnabler $maintenanceModeEnabler
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
Cache $cache,
Expand All @@ -142,19 +144,21 @@ public function __construct(
ThemeValidator $themeValidator,
ThemePackageInfo $themePackageInfo,
ThemeUninstaller $themeUninstaller,
ThemeDependencyChecker $themeDependencyChecker
ThemeDependencyChecker $themeDependencyChecker,
MaintenanceModeEnabler $maintenanceModeEnabler = null
) {
$this->cache = $cache;
$this->cleanupFiles = $cleanupFiles;
$this->composer = $composer;
$this->maintenanceMode = $maintenanceMode;
$this->dependencyChecker = $dependencyChecker;
$this->themeCollection = $themeCollection;
$this->backupRollbackFactory = $backupRollbackFactory;
$this->themeValidator = $themeValidator;
$this->themePackageInfo = $themePackageInfo;
$this->themeUninstaller = $themeUninstaller;
$this->themeDependencyChecker = $themeDependencyChecker;
$this->maintenanceModeEnabler =
$maintenanceModeEnabler ?: ObjectManager::getInstance()->get(MaintenanceModeEnabler::class);
parent::__construct();
}

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

try {
$output->writeln('<info>Enabling maintenance mode</info>');
$this->maintenanceMode->set(true);
if ($input->getOption(self::INPUT_KEY_BACKUP_CODE)) {
$time = time();
$codeBackup = $this->backupRollbackFactory->create($output);
$codeBackup->codeBackup($time);
}

$this->themeUninstaller->uninstallRegistry($output, $themePaths);
$this->themeUninstaller->uninstallCode($output, $themePaths);
$result = $this->maintenanceModeEnabler->executeInMaintenanceMode(
function () use ($input, $output, $themePaths) {
try {
if ($input->getOption(self::INPUT_KEY_BACKUP_CODE)) {
$time = time();
$codeBackup = $this->backupRollbackFactory->create($output);
$codeBackup->codeBackup($time);
}

$this->themeUninstaller->uninstallRegistry($output, $themePaths);
$this->themeUninstaller->uninstallCode($output, $themePaths);

$this->cleanup($input, $output);
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
$output->writeln('<error>Please disable maintenance mode after you resolved above issues</error>');
// we must have an exit code higher than zero to indicate something was wrong
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
},
$output,
true
);

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

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

use Magento\Framework\App\Console\MaintenanceModeEnabler;
use Magento\Theme\Console\Command\ThemeUninstallCommand;
use Magento\Theme\Model\Theme\themePackageInfo;
use Magento\Theme\Model\Theme\ThemePackageInfo;
use Magento\Theme\Model\Theme\ThemeUninstaller;
use Magento\Theme\Model\Theme\ThemeDependencyChecker;
use Symfony\Component\Console\Tester\CommandTester;
Expand Down Expand Up @@ -56,7 +57,7 @@ class ThemeUninstallCommandTest extends \PHPUnit\Framework\TestCase
/**
* Theme Validator
*
* @var ThemeValidator|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Theme\Model\ThemeValidator|\PHPUnit_Framework_MockObject_MockObject
*/
private $themeValidator;

Expand Down Expand Up @@ -107,7 +108,8 @@ protected function setUp()
$this->themeValidator,
$this->themePackageInfo,
$this->themeUninstaller,
$this->themeDependencyChecker
$this->themeDependencyChecker,
new MaintenanceModeEnabler($this->maintenanceMode)
);
$this->tester = new CommandTester($this->command);
}
Expand Down
Loading

0 comments on commit 490785f

Please sign in to comment.