Skip to content

Commit a67daa3

Browse files
committed
MAGETWO-54205: After Disable/Enable Magento modules via CLI all cache types become disabled
1 parent a83acb7 commit a67daa3

File tree

5 files changed

+47
-128
lines changed

5 files changed

+47
-128
lines changed

lib/internal/Magento/Framework/Code/GeneratedFiles.php

Lines changed: 20 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,9 @@ public function __construct(DirectoryList $directoryList, WriteFactory $writeFac
5353
*/
5454
public function regenerate()
5555
{
56-
if ($this->write->isExist(self::REGENERATE_FLAG)) {
57-
//TODO: to be removed in scope of MAGETWO-53476
58-
//clean cache
59-
$deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
60-
$configPool = new ConfigFilePool();
61-
$envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
62-
if ($this->write->isExist($this->write->getRelativePath($envPath))) {
63-
$this->saveCacheStatus($envPath);
64-
}
65-
//TODO: Till here
66-
$cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE));
67-
$generationPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATION));
68-
$diPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::DI));
69-
70-
if ($this->write->isDirectory($generationPath)) {
71-
$this->write->delete($generationPath);
72-
}
73-
if ($this->write->isDirectory($diPath)) {
74-
$this->write->delete($diPath);
75-
}
76-
if ($this->write->isDirectory($cachePath)) {
77-
$this->write->delete($cachePath);
78-
}
79-
//add to queue
80-
81-
$this->write->delete(self::REGENERATE_FLAG);
82-
}
56+
$this->cleanGeneratedFiles();
8357
}
8458

85-
8659
/**
8760
* Clean var/generation, var/di and var/cache
8861
*
@@ -92,14 +65,14 @@ public function cleanGeneratedFiles()
9265
{
9366
if ($this->write->isExist(self::REGENERATE_FLAG)) {
9467

95-
$cacheStatus = [];
68+
$enabledCacheTypes = [];
9669

9770
//TODO: to be removed in scope of MAGETWO-53476
9871
$deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
9972
$configPool = new ConfigFilePool();
10073
$envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
10174
if ($this->write->isExist($this->write->getRelativePath($envPath))) {
102-
$cacheStatus = $this->getCacheStatus();
75+
$enabledCacheTypes = $this->getEnabledCacheTypes();
10376
$this->disableAllCacheTypes();
10477
}
10578
//TODO: Till here
@@ -123,7 +96,7 @@ public function cleanGeneratedFiles()
12396
$this->write->delete($cachePath);
12497
}
12598
$this->write->delete(self::REGENERATE_FLAG);
126-
$this->restoreCacheStatus($cacheStatus);
99+
$this->enableCacheTypes($enabledCacheTypes);
127100
}
128101
}
129102

@@ -139,66 +112,25 @@ public function requestRegeneration()
139112
}
140113

141114
/**
142-
* Read Cache types from env.php and write to a json file.
143-
*
144-
* @param string $envPath
145-
* @return void
115+
* Reads Cache configuration from env.php and returns indexed array containing all the enabled cache types.
146116
*
117+
* @return string []
147118
*/
148-
private function saveCacheStatus($envPath)
119+
private function getEnabledCacheTypes()
149120
{
150-
$cacheData = include $envPath;
151-
152-
if (isset($cacheData['cache_types'])) {
153-
$enabledCacheTypes = $cacheData['cache_types'];
154-
$enabledCacheTypes = array_filter($enabledCacheTypes, function ($value) {
155-
return $value;
156-
});
157-
if (!empty($enabledCacheTypes)) {
158-
$varDir = $this->directoryList->getPath(DirectoryList::VAR_DIR);
159-
$this->write->writeFile(
160-
$this->write->getRelativePath($varDir) . '/.cachestates.json',
161-
json_encode($enabledCacheTypes)
162-
);
163-
$cacheTypes = array_keys($cacheData['cache_types']);
164-
165-
foreach ($cacheTypes as $cacheType) {
166-
$cacheData['cache_types'][$cacheType] = 0;
167-
}
168-
169-
$formatter = new PhpFormatter();
170-
$contents = $formatter->format($cacheData);
171-
172-
$this->write->writeFile($this->write->getRelativePath($envPath), $contents);
173-
if (function_exists('opcache_invalidate')) {
174-
opcache_invalidate(
175-
$this->write->getAbsolutePath($envPath)
176-
);
177-
}
178-
}
179-
}
180-
}
181-
182-
/**
183-
* Reads Cache configuration from env.php and returns the 'cache_types' key data which is the current
184-
* cache status.
185-
*
186-
* @return array
187-
*/
188-
private function getCacheStatus()
189-
{
190-
$cacheStatus = [];
191-
if (empty($envPath)) {
192-
$envPath = $this->getEnvPath();
193-
}
194-
121+
$enabledCacheTypes = [];
122+
$envPath = $this->getEnvPath();
195123
if ($this->write->isReadable($this->write->getRelativePath($envPath))) {
196124
$envData = include $envPath;
197125
if (isset($envData['cache_types'])) {
198126
$cacheStatus = $envData['cache_types'];
127+
$enabledCacheTypes = array_filter($cacheStatus, function ($value) {
128+
return $value;
129+
});
130+
$enabledCacheTypes = array_keys($enabledCacheTypes);
199131
}
200132
}
201-
return $cacheStatus;
133+
return $enabledCacheTypes;
202134
}
203135

204136

@@ -246,24 +178,24 @@ private function disableAllCacheTypes()
246178
}
247179

248180
/**
249-
* restore the cacache setting in env.php
181+
* Enables apppropriate cache types in app/etc/env.php based on the passed in $cacheTypes array
250182
* TODO: to be removed in scope of MAGETWO-53476
251183
*
252-
* @param array
184+
* @param string []
253185
*
254186
* @return void
255187
*/
256-
private function restoreCacheStatus($cacheStatus)
188+
private function enableCacheTypes($cacheTypes)
257189
{
258-
if (empty($cacheStatus)) {
190+
if (empty($cacheTypes)) {
259191
return;
260192
}
261193
$envPath = $this->getEnvPath();
262194
if ($this->write->isReadable($this->write->getRelativePath($envPath))) {
263195
$envData = include $envPath;
264-
foreach ($cacheStatus as $cacheType => $state) {
196+
foreach ($cacheTypes as $cacheType) {
265197
if (isset($envData['cache_types'][$cacheType])) {
266-
$envData['cache_types'][$cacheType] = $state;
198+
$envData['cache_types'][$cacheType] = 1;
267199
}
268200
}
269201

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Symfony\Component\Console\Question\ConfirmationQuestion;
1212
use Magento\Setup\Model\InstallerFactory;
1313
use Magento\Framework\Setup\ConsoleLogger;
14+
use Magento\Framework\Code\GeneratedFiles;
1415

1516
class UninstallCommand extends AbstractSetupCommand
1617
{
@@ -19,12 +20,18 @@ class UninstallCommand extends AbstractSetupCommand
1920
*/
2021
private $installerFactory;
2122

23+
/**
24+
* @var GeneratedFiles
25+
*/
26+
private $generatedFiles;
27+
2228
/**
2329
* @param InstallerFactory $installerFactory
2430
*/
25-
public function __construct(InstallerFactory $installerFactory)
31+
public function __construct(InstallerFactory $installerFactory, GeneratedFiles $generatedFiles)
2632
{
2733
$this->installerFactory = $installerFactory;
34+
$this->generatedFiles = $generatedFiles;
2835
parent::__construct();
2936
}
3037

@@ -49,6 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4956
if ($helper->ask($input, $output, $question) || !$input->isInteractive()) {
5057
$installer = $this->installerFactory->create(new ConsoleLogger($output));
5158
$installer->uninstall();
59+
$this->generatedFiles->requestRegeneration();
5260
}
5361
}
5462
}

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

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
use Magento\Framework\Setup\ConsoleLogger;
99
use Magento\Setup\Model\InstallerFactory;
1010
use Magento\Setup\Model\ObjectManagerProvider;
11-
use Symfony\Component\Console\Input\ArrayInput;
1211
use Symfony\Component\Console\Input\InputInterface;
1312
use Symfony\Component\Console\Input\InputOption;
1413
use Symfony\Component\Console\Output\OutputInterface;
15-
use Magento\Framework\Code\GeneratedFiles;
1614

1715
/**
1816
* Command for updating installed application after the code base has changed
@@ -32,14 +30,9 @@ class UpgradeCommand extends AbstractSetupCommand
3230
private $installerFactory;
3331

3432
/**
35-
* @var \Magento\Framework\ObjectManagerInterface
33+
* @var \Magento\Setup\Model\ObjectManagerProvider;
3634
*/
37-
private $objectManager;
38-
39-
/**
40-
* @var GeneratedFiles
41-
*/
42-
private $generatedFiles;
35+
private $objectManagerProvider;
4336

4437
/**
4538
* Constructor
@@ -50,8 +43,7 @@ class UpgradeCommand extends AbstractSetupCommand
5043
public function __construct(InstallerFactory $installerFactory, ObjectManagerProvider $objectManagerProvider)
5144
{
5245
$this->installerFactory = $installerFactory;
53-
/** @var \Magento\Framework\ObjectManagerInterface objectManager */
54-
$this->objectManager = $objectManagerProvider->get();
46+
$this->objectManagerProvider = $objectManagerProvider;
5547
parent::__construct();
5648
}
5749

@@ -81,14 +73,15 @@ protected function configure()
8173
*/
8274
protected function execute(InputInterface $input, OutputInterface $output)
8375
{
84-
$this->getGeneratedFiles()->requestRegeneration();
8576
$areaCode = 'setup';
77+
/** @var \Magento\Framework\ObjectManagerInterface $objectManager */
78+
$objectManager = $this->objectManagerProvider->get();
8679
/** @var \Magento\Framework\App\State $appState */
87-
$appState = $this->objectManager->get('Magento\Framework\App\State');
80+
$appState = $objectManager->get('Magento\Framework\App\State');
8881
$appState->setAreaCode($areaCode);
8982
/** @var \Magento\Framework\ObjectManager\ConfigLoaderInterface $configLoader */
90-
$configLoader = $this->objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface');
91-
$this->objectManager->configure($configLoader->load($areaCode));
83+
$configLoader = $objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface');
84+
$objectManager->configure($configLoader->load($areaCode));
9285

9386
$keepGenerated = $input->getOption(self::INPUT_KEY_KEEP_GENERATED);
9487
$installer = $this->installerFactory->create(new ConsoleLogger($output));
@@ -101,18 +94,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
10194

10295
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
10396
}
104-
105-
/**
106-
* Get deployment config
107-
*
108-
* @return GeneratedFiles
109-
* @deprecated
110-
*/
111-
private function getGeneratedFiles()
112-
{
113-
if (!($this->generatedFiles instanceof GeneratedFiles)) {
114-
return $this->objectManager->get(GeneratedFiles::class);
115-
}
116-
return $this->generatedFiles;
117-
}
11897
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Setup\Console\Command\UninstallCommand;
1111
use Symfony\Component\Console\Tester\CommandTester;
1212
use Magento\Setup\Model\Installer;
13+
use Magento\Framework\Code\GeneratedFiles;
1314

1415
class UninstallCommandTest extends \PHPUnit_Framework_TestCase
1516
{
@@ -18,6 +19,11 @@ class UninstallCommandTest extends \PHPUnit_Framework_TestCase
1819
*/
1920
private $installerFactory;
2021

22+
/**
23+
* @var GeneratedFiles|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $generatedFiles;
26+
2127
/**
2228
* @var Installer|\PHPUnit_Framework_MockObject_MockObject
2329
*/
@@ -31,8 +37,9 @@ class UninstallCommandTest extends \PHPUnit_Framework_TestCase
3137
public function setUp()
3238
{
3339
$this->installerFactory = $this->getMock('Magento\Setup\Model\InstallerFactory', [], [], '', false);
40+
$this->generatedFiles = $this->getMock('Magento\Framework\Code\GeneratedFiles', [], [], '', false);
3441
$this->installer = $this->getMock('Magento\Setup\Model\Installer', [], [], '', false);
35-
$this->command = new UninstallCommand($this->installerFactory);
42+
$this->command = new UninstallCommand($this->installerFactory, $this->generatedFiles);
3643
}
3744

3845
public function testExecuteInteractionYes()

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,12 @@ public function testExecute()
2727
$installer->expects($this->at(1))->method('installSchema');
2828
$installer->expects($this->at(2))->method('installDataFixtures');
2929
$installerFactory->expects($this->once())->method('create')->willReturn($installer);
30-
$writeFactory = $this->getMock('\Magento\Framework\Filesystem\Directory\WriteFactory', [], [], '', false);
31-
$directoryList = $this->getMock('\Magento\Framework\App\Filesystem\DirectoryList', [], [], '', false);
32-
$generatedFiles = $this->getMock('\Magento\Framework\Code\GeneratedFiles', [], [], '', false);
33-
$generatedFiles->expects($this->once())->method('requestRegeneration');
3430

35-
$objectManager->expects($this->exactly(3))
31+
$objectManager->expects($this->exactly(2))
3632
->method('get')
3733
->will($this->returnValueMap([
3834
['Magento\Framework\App\State', $state],
39-
['Magento\Framework\ObjectManager\ConfigLoaderInterface', $configLoader],
40-
['Magento\Framework\Filesystem\Directory\WriteFactory', $writeFactory],
41-
['Magento\Framework\App\Filesystem\DirectoryList', $directoryList],
42-
['Magento\Framework\Code\GeneratedFiles', $generatedFiles]
35+
['Magento\Framework\ObjectManager\ConfigLoaderInterface', $configLoader]
4336
]));
4437

4538
$commandTester = new CommandTester(new UpgradeCommand($installerFactory, $objectManagerProvider));

0 commit comments

Comments
 (0)