diff --git a/lib/internal/Magento/Framework/Code/GeneratedFiles.php b/lib/internal/Magento/Framework/Code/GeneratedFiles.php index ec4182df962cf09f26c4a4560d9cb85f9e893695..5ea189305cada2136298cbe5e73fc5acee1f4c4f 100644 --- a/lib/internal/Magento/Framework/Code/GeneratedFiles.php +++ b/lib/internal/Magento/Framework/Code/GeneratedFiles.php @@ -53,36 +53,9 @@ class GeneratedFiles */ public function regenerate() { - if ($this->write->isExist(self::REGENERATE_FLAG)) { - //TODO: to be removed in scope of MAGETWO-53476 - //clean cache - $deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG); - $configPool = new ConfigFilePool(); - $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV); - if ($this->write->isExist($this->write->getRelativePath($envPath))) { - $this->saveCacheStatus($envPath); - } - //TODO: Till here - $cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE)); - $generationPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATION)); - $diPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::DI)); - - if ($this->write->isDirectory($generationPath)) { - $this->write->delete($generationPath); - } - if ($this->write->isDirectory($diPath)) { - $this->write->delete($diPath); - } - if ($this->write->isDirectory($cachePath)) { - $this->write->delete($cachePath); - } - //add to queue - - $this->write->delete(self::REGENERATE_FLAG); - } + $this->cleanGeneratedFiles(); } - /** * Clean var/generation, var/di and var/cache * @@ -92,14 +65,14 @@ class GeneratedFiles { if ($this->write->isExist(self::REGENERATE_FLAG)) { - $cacheStatus = []; + $enabledCacheTypes = []; //TODO: to be removed in scope of MAGETWO-53476 $deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG); $configPool = new ConfigFilePool(); $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV); if ($this->write->isExist($this->write->getRelativePath($envPath))) { - $cacheStatus = $this->getCacheStatus(); + $enabledCacheTypes = $this->getEnabledCacheTypes(); $this->disableAllCacheTypes(); } //TODO: Till here @@ -123,7 +96,7 @@ class GeneratedFiles $this->write->delete($cachePath); } $this->write->delete(self::REGENERATE_FLAG); - $this->restoreCacheStatus($cacheStatus); + $this->enableCacheTypes($enabledCacheTypes); } } @@ -139,66 +112,25 @@ class GeneratedFiles } /** - * Read Cache types from env.php and write to a json file. - * - * @param string $envPath - * @return void + * Reads Cache configuration from env.php and returns indexed array containing all the enabled cache types. * + * @return string [] */ - private function saveCacheStatus($envPath) + private function getEnabledCacheTypes() { - $cacheData = include $envPath; - - if (isset($cacheData['cache_types'])) { - $enabledCacheTypes = $cacheData['cache_types']; - $enabledCacheTypes = array_filter($enabledCacheTypes, function ($value) { - return $value; - }); - if (!empty($enabledCacheTypes)) { - $varDir = $this->directoryList->getPath(DirectoryList::VAR_DIR); - $this->write->writeFile( - $this->write->getRelativePath($varDir) . '/.cachestates.json', - json_encode($enabledCacheTypes) - ); - $cacheTypes = array_keys($cacheData['cache_types']); - - foreach ($cacheTypes as $cacheType) { - $cacheData['cache_types'][$cacheType] = 0; - } - - $formatter = new PhpFormatter(); - $contents = $formatter->format($cacheData); - - $this->write->writeFile($this->write->getRelativePath($envPath), $contents); - if (function_exists('opcache_invalidate')) { - opcache_invalidate( - $this->write->getAbsolutePath($envPath) - ); - } - } - } - } - - /** - * Reads Cache configuration from env.php and returns the 'cache_types' key data which is the current - * cache status. - * - * @return array - */ - private function getCacheStatus() - { - $cacheStatus = []; - if (empty($envPath)) { - $envPath = $this->getEnvPath(); - } - + $enabledCacheTypes = []; + $envPath = $this->getEnvPath(); if ($this->write->isReadable($this->write->getRelativePath($envPath))) { $envData = include $envPath; if (isset($envData['cache_types'])) { $cacheStatus = $envData['cache_types']; + $enabledCacheTypes = array_filter($cacheStatus, function ($value) { + return $value; + }); + $enabledCacheTypes = array_keys($enabledCacheTypes); } } - return $cacheStatus; + return $enabledCacheTypes; } @@ -246,24 +178,24 @@ class GeneratedFiles } /** - * restore the cacache setting in env.php + * Enables apppropriate cache types in app/etc/env.php based on the passed in $cacheTypes array * TODO: to be removed in scope of MAGETWO-53476 * - * @param array + * @param string [] * * @return void */ - private function restoreCacheStatus($cacheStatus) + private function enableCacheTypes($cacheTypes) { - if (empty($cacheStatus)) { + if (empty($cacheTypes)) { return; } $envPath = $this->getEnvPath(); if ($this->write->isReadable($this->write->getRelativePath($envPath))) { $envData = include $envPath; - foreach ($cacheStatus as $cacheType => $state) { + foreach ($cacheTypes as $cacheType) { if (isset($envData['cache_types'][$cacheType])) { - $envData['cache_types'][$cacheType] = $state; + $envData['cache_types'][$cacheType] = 1; } } diff --git a/setup/src/Magento/Setup/Console/Command/UninstallCommand.php b/setup/src/Magento/Setup/Console/Command/UninstallCommand.php index 27d1181bdc7fc1a8c4506c10a118af1bb52972e1..b4de82f66be41968b1e1d96ee8709390a78f0535 100644 --- a/setup/src/Magento/Setup/Console/Command/UninstallCommand.php +++ b/setup/src/Magento/Setup/Console/Command/UninstallCommand.php @@ -11,6 +11,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; use Magento\Setup\Model\InstallerFactory; use Magento\Framework\Setup\ConsoleLogger; +use Magento\Framework\Code\GeneratedFiles; class UninstallCommand extends AbstractSetupCommand { @@ -19,12 +20,18 @@ class UninstallCommand extends AbstractSetupCommand */ private $installerFactory; + /** + * @var GeneratedFiles + */ + private $generatedFiles; + /** * @param InstallerFactory $installerFactory */ - public function __construct(InstallerFactory $installerFactory) + public function __construct(InstallerFactory $installerFactory, GeneratedFiles $generatedFiles) { $this->installerFactory = $installerFactory; + $this->generatedFiles = $generatedFiles; parent::__construct(); } @@ -49,6 +56,7 @@ class UninstallCommand extends AbstractSetupCommand if ($helper->ask($input, $output, $question) || !$input->isInteractive()) { $installer = $this->installerFactory->create(new ConsoleLogger($output)); $installer->uninstall(); + $this->generatedFiles->requestRegeneration(); } } } diff --git a/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php b/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php index 3d86c44e40493e83e3f520b37fb1b08de582fa28..577e787bdd5f286dc238fdce41f1a016a98051b2 100644 --- a/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php +++ b/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php @@ -8,11 +8,9 @@ namespace Magento\Setup\Console\Command; use Magento\Framework\Setup\ConsoleLogger; use Magento\Setup\Model\InstallerFactory; use Magento\Setup\Model\ObjectManagerProvider; -use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Magento\Framework\Code\GeneratedFiles; /** * Command for updating installed application after the code base has changed @@ -32,14 +30,9 @@ class UpgradeCommand extends AbstractSetupCommand private $installerFactory; /** - * @var \Magento\Framework\ObjectManagerInterface + * @var \Magento\Setup\Model\ObjectManagerProvider; */ - private $objectManager; - - /** - * @var GeneratedFiles - */ - private $generatedFiles; + private $objectManagerProvider; /** * Constructor @@ -50,8 +43,7 @@ class UpgradeCommand extends AbstractSetupCommand public function __construct(InstallerFactory $installerFactory, ObjectManagerProvider $objectManagerProvider) { $this->installerFactory = $installerFactory; - /** @var \Magento\Framework\ObjectManagerInterface objectManager */ - $this->objectManager = $objectManagerProvider->get(); + $this->objectManagerProvider = $objectManagerProvider; parent::__construct(); } @@ -81,14 +73,15 @@ class UpgradeCommand extends AbstractSetupCommand */ protected function execute(InputInterface $input, OutputInterface $output) { - $this->getGeneratedFiles()->requestRegeneration(); $areaCode = 'setup'; + /** @var \Magento\Framework\ObjectManagerInterface $objectManager */ + $objectManager = $this->objectManagerProvider->get(); /** @var \Magento\Framework\App\State $appState */ - $appState = $this->objectManager->get('Magento\Framework\App\State'); + $appState = $objectManager->get('Magento\Framework\App\State'); $appState->setAreaCode($areaCode); /** @var \Magento\Framework\ObjectManager\ConfigLoaderInterface $configLoader */ - $configLoader = $this->objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface'); - $this->objectManager->configure($configLoader->load($areaCode)); + $configLoader = $objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface'); + $objectManager->configure($configLoader->load($areaCode)); $keepGenerated = $input->getOption(self::INPUT_KEY_KEEP_GENERATED); $installer = $this->installerFactory->create(new ConsoleLogger($output)); @@ -101,18 +94,4 @@ class UpgradeCommand extends AbstractSetupCommand return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } - - /** - * Get deployment config - * - * @return GeneratedFiles - * @deprecated - */ - private function getGeneratedFiles() - { - if (!($this->generatedFiles instanceof GeneratedFiles)) { - return $this->objectManager->get(GeneratedFiles::class); - } - return $this->generatedFiles; - } } diff --git a/setup/src/Magento/Setup/Test/Unit/Console/Command/UninstallCommandTest.php b/setup/src/Magento/Setup/Test/Unit/Console/Command/UninstallCommandTest.php index a3183950c8e59e80386c12da4c95e53d19d985be..cbe2da59cb6ab2ac8b8e4564cf16d9ae16b113e9 100644 --- a/setup/src/Magento/Setup/Test/Unit/Console/Command/UninstallCommandTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Console/Command/UninstallCommandTest.php @@ -10,6 +10,7 @@ use Magento\Setup\Model\InstallerFactory; use Magento\Setup\Console\Command\UninstallCommand; use Symfony\Component\Console\Tester\CommandTester; use Magento\Setup\Model\Installer; +use Magento\Framework\Code\GeneratedFiles; class UninstallCommandTest extends \PHPUnit_Framework_TestCase { @@ -18,6 +19,11 @@ class UninstallCommandTest extends \PHPUnit_Framework_TestCase */ private $installerFactory; + /** + * @var GeneratedFiles|\PHPUnit_Framework_MockObject_MockObject + */ + private $generatedFiles; + /** * @var Installer|\PHPUnit_Framework_MockObject_MockObject */ @@ -31,8 +37,9 @@ class UninstallCommandTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->installerFactory = $this->getMock('Magento\Setup\Model\InstallerFactory', [], [], '', false); + $this->generatedFiles = $this->getMock('Magento\Framework\Code\GeneratedFiles', [], [], '', false); $this->installer = $this->getMock('Magento\Setup\Model\Installer', [], [], '', false); - $this->command = new UninstallCommand($this->installerFactory); + $this->command = new UninstallCommand($this->installerFactory, $this->generatedFiles); } public function testExecuteInteractionYes() diff --git a/setup/src/Magento/Setup/Test/Unit/Console/Command/UpgradeCommandTest.php b/setup/src/Magento/Setup/Test/Unit/Console/Command/UpgradeCommandTest.php index 03fb1194316c56c1ebabb8362ef586041923d3b3..17d78718e4bf4bcdc2978c946fe98223b8169011 100644 --- a/setup/src/Magento/Setup/Test/Unit/Console/Command/UpgradeCommandTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Console/Command/UpgradeCommandTest.php @@ -27,19 +27,12 @@ class UpgradeCommandTest extends \PHPUnit_Framework_TestCase $installer->expects($this->at(1))->method('installSchema'); $installer->expects($this->at(2))->method('installDataFixtures'); $installerFactory->expects($this->once())->method('create')->willReturn($installer); - $writeFactory = $this->getMock('\Magento\Framework\Filesystem\Directory\WriteFactory', [], [], '', false); - $directoryList = $this->getMock('\Magento\Framework\App\Filesystem\DirectoryList', [], [], '', false); - $generatedFiles = $this->getMock('\Magento\Framework\Code\GeneratedFiles', [], [], '', false); - $generatedFiles->expects($this->once())->method('requestRegeneration'); - $objectManager->expects($this->exactly(3)) + $objectManager->expects($this->exactly(2)) ->method('get') ->will($this->returnValueMap([ ['Magento\Framework\App\State', $state], - ['Magento\Framework\ObjectManager\ConfigLoaderInterface', $configLoader], - ['Magento\Framework\Filesystem\Directory\WriteFactory', $writeFactory], - ['Magento\Framework\App\Filesystem\DirectoryList', $directoryList], - ['Magento\Framework\Code\GeneratedFiles', $generatedFiles] + ['Magento\Framework\ObjectManager\ConfigLoaderInterface', $configLoader] ])); $commandTester = new CommandTester(new UpgradeCommand($installerFactory, $objectManagerProvider));