Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Extract silent configuration upgrade into class #461

Merged
merged 1 commit into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 10 additions & 40 deletions Classes/Install/InstallStepActionExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
*
*/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use Helhum\Typo3Console\Install\Upgrade\SilentConfigurationUpgrade;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Install\Controller\Action\ActionInterface;
use TYPO3\CMS\Install\Controller\Action\Step\StepInterface;
use TYPO3\CMS\Install\Controller\Exception\RedirectException;
use TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService;

/**
* This class is responsible for properly creating install tool step actions
Expand All @@ -31,13 +30,19 @@ class InstallStepActionExecutor
*/
private $objectManager;

/**
* @var SilentConfigurationUpgrade
*/
private $silentConfigurationUpgrade;

/**
* @param ObjectManager $objectManager
*/
public function __construct(ObjectManager $objectManager)
public function __construct(ObjectManager $objectManager, SilentConfigurationUpgrade $silentConfigurationUpgrade)
{
// @deprecated Object Manager can be removed, once TYPO3 7.6 support is removed
$this->objectManager = $objectManager;
$this->silentConfigurationUpgrade = $silentConfigurationUpgrade;
}

/**
Expand Down Expand Up @@ -75,6 +80,7 @@ private function createActionWithNameAndArguments($actionName, array $arguments
/**
* @param StepInterface $action
* @param bool $dryRun
* @throws \TYPO3\CMS\Install\Controller\Exception\RedirectException
* @return InstallStepResponse
*/
private function executeAction(StepInterface $action, $dryRun = false)
Expand All @@ -87,45 +93,9 @@ private function executeAction(StepInterface $action, $dryRun = false)
}
if ($needsExecution && !$dryRun) {
$messages = $action->execute();
$this->executeSilentConfigurationUpgradesIfNeeded();
$this->silentConfigurationUpgrade->executeSilentConfigurationUpgradesIfNeeded();
$needsExecution = false;
}
return new InstallStepResponse($needsExecution, $messages);
}

/**
* Call silent upgrade class, redirect to self if configuration was changed.
*
* @throws RedirectException
* @return void
*/
private function executeSilentConfigurationUpgradesIfNeeded()
{
if (!file_exists(GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->getLocalConfigurationFileLocation())) {
return;
}
$upgradeService = $this->objectManager->get(SilentConfigurationUpgradeService::class);
$count = 0;
do {
try {
$count++;
$upgradeService->execute();
$redirect = false;
} catch (RedirectException $e) {
$redirect = true;
$this->reloadConfiguration();
if ($count > 20) {
throw $e;
}
}
} while ($redirect === true);
}

/**
* Fetch the new configuration and expose it to the global array
*/
private function reloadConfiguration()
{
GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class)->exportConfiguration();
}
}
72 changes: 72 additions & 0 deletions Classes/Install/Upgrade/SilentConfigurationUpgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace Helhum\Typo3Console\Install\Upgrade;

/*
* This file is part of the TYPO3 Console project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read
* LICENSE file that was distributed with this source code.
*
*/

use TYPO3\CMS\Core\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
use TYPO3\CMS\Install\Controller\Exception\RedirectException;
use TYPO3\CMS\Install\Service\SilentConfigurationUpgradeService;

/**
* Used to migrate deprecated LocalConfiguration.php values to new values
* It is a wrapper around the TYPO3 class to properly handle redirect exceptions
*/
class SilentConfigurationUpgrade
{
/**
* @var ConfigurationManager
*/
private $configurationManager;

/**
* @var ObjectManagerInterface
*/
private $objectManager;

public function __construct(ConfigurationManager $configurationManager = null, ObjectManagerInterface $objectManager = null)
{
$this->configurationManager = $configurationManager ?: GeneralUtility::makeInstance(ConfigurationManager::class);
$this->objectManager = $objectManager ?: GeneralUtility::makeInstance(ObjectManager::class);
}

/**
* Call silent upgrade class, redirect to self if configuration was changed.
*
* @throws RedirectException
* @return void
*/
public function executeSilentConfigurationUpgradesIfNeeded()
{
if (!file_exists($this->configurationManager->getLocalConfigurationFileLocation())) {
return;
}
$upgradeService = $this->objectManager->get(SilentConfigurationUpgradeService::class);
$count = 0;
do {
try {
$count++;
$upgradeService->execute();
$redirect = false;
} catch (RedirectException $e) {
$redirect = true;
$this->configurationManager->exportConfiguration();
if ($count > 20) {
throw $e;
}
}
} while ($redirect === true);
}
}
13 changes: 9 additions & 4 deletions Tests/Unit/Install/InstallStepExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

use Helhum\Typo3Console\Install\InstallStepActionExecutor;
use Helhum\Typo3Console\Install\Upgrade\SilentConfigurationUpgrade;
use Nimut\TestingFramework\TestCase\UnitTestCase;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Install\Controller\Action\Step\AbstractStepAction;
Expand All @@ -36,7 +37,8 @@ public function needsExecutionThrowsRedirectIsDetected()
$objectManagerMock->expects($this->any())
->method('get')
->willReturn($actionMock);
$executor = new InstallStepActionExecutor($objectManagerMock);
$silentConfigUpgradeMock = $this->getMockBuilder(SilentConfigurationUpgrade::class)->disableOriginalConstructor()->getMock();
$executor = new InstallStepActionExecutor($objectManagerMock, $silentConfigUpgradeMock);
$response = $executor->executeActionWithArguments('test', []);
$this->assertTrue($response->actionNeedsReevaluation());
}
Expand All @@ -56,7 +58,8 @@ public function actionIsNeverExecutedIfNotNeeded()
$objectManagerMock->expects($this->any())
->method('get')
->willReturn($actionMock);
$executor = new InstallStepActionExecutor($objectManagerMock);
$silentConfigUpgradeMock = $this->getMockBuilder(SilentConfigurationUpgrade::class)->disableOriginalConstructor()->getMock();
$executor = new InstallStepActionExecutor($objectManagerMock, $silentConfigUpgradeMock);
$response = $executor->executeActionWithArguments('test', []);
$this->assertFalse($response->actionNeedsExecution());
}
Expand All @@ -76,7 +79,8 @@ public function actionIsNeverExecutedIfDryRun()
$objectManagerMock->expects($this->any())
->method('get')
->willReturn($actionMock);
$executor = new InstallStepActionExecutor($objectManagerMock);
$silentConfigUpgradeMock = $this->getMockBuilder(SilentConfigurationUpgrade::class)->disableOriginalConstructor()->getMock();
$executor = new InstallStepActionExecutor($objectManagerMock, $silentConfigUpgradeMock);
$response = $executor->executeActionWithArguments('test', [], true);
$this->assertTrue($response->actionNeedsExecution());
}
Expand All @@ -97,7 +101,8 @@ public function actionIsExecutedIfNeeded()
$objectManagerMock->expects($this->any())
->method('get')
->willReturn($actionMock);
$executor = new InstallStepActionExecutor($objectManagerMock);
$silentConfigUpgradeMock = $this->getMockBuilder(SilentConfigurationUpgrade::class)->disableOriginalConstructor()->getMock();
$executor = new InstallStepActionExecutor($objectManagerMock, $silentConfigUpgradeMock);
$response = $executor->executeActionWithArguments('test', []);
$this->assertFalse($response->actionNeedsExecution());
}
Expand Down