Skip to content

Commit 107569c

Browse files
author
Cari Spruiell
committed
Merge remote-tracking branch 'ogre/MAGETWO-54205-cache-disable-issue' into MAGETWO-53121-502-Bad-Gateway
2 parents 2e93753 + 69644ea commit 107569c

File tree

5 files changed

+121
-89
lines changed

5 files changed

+121
-89
lines changed

lib/internal/Magento/Framework/App/ObjectManagerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function create(array $arguments)
111111
{
112112
$writeFactory = new \Magento\Framework\Filesystem\Directory\WriteFactory($this->driverPool);
113113
$generatedFiles = new GeneratedFiles($this->directoryList, $writeFactory);
114-
$generatedFiles->regenerate();
114+
$generatedFiles->cleanGeneratedFiles();
115115

116116
$deploymentConfig = $this->createDeploymentConfig($this->directoryList, $this->configFilePool, $arguments);
117117
$arguments = array_merge($deploymentConfig->get(), $arguments);

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

Lines changed: 108 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Framework\Code;
77

8-
use Magento\Framework\Config\Data\ConfigData;
98
use Magento\Framework\App\Filesystem\DirectoryList;
109
use Magento\Framework\Config\File\ConfigFilePool;
1110
use Magento\Framework\Filesystem\Directory\WriteFactory;
@@ -48,67 +47,127 @@ public function __construct(DirectoryList $directoryList, WriteFactory $writeFac
4847
* Clean generated code and DI configuration
4948
*
5049
* @return void
50+
*
51+
* @deprecated
52+
* @see \Magento\Framework\Code\GeneratedFiles::cleanGeneratedFiles
5153
*/
5254
public function regenerate()
55+
{
56+
$this->cleanGeneratedFiles();
57+
}
58+
59+
/**
60+
* Clean var/generation, var/di and var/cache
61+
*
62+
* @return void
63+
*/
64+
public function cleanGeneratedFiles()
5365
{
5466
if ($this->write->isExist(self::REGENERATE_FLAG)) {
67+
68+
$enabledCacheTypes = [];
69+
5570
//TODO: to be removed in scope of MAGETWO-53476
56-
//clean cache
5771
$deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
5872
$configPool = new ConfigFilePool();
5973
$envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
6074
if ($this->write->isExist($this->write->getRelativePath($envPath))) {
61-
$this->saveCacheStatus($envPath);
75+
$enabledCacheTypes = $this->getEnabledCacheTypes();
76+
$this->disableAllCacheTypes();
6277
}
6378
//TODO: Till here
79+
6480
$cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE));
6581
$generationPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATION));
6682
$diPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::DI));
6783

84+
// Clean var/generation dir
6885
if ($this->write->isDirectory($generationPath)) {
6986
$this->write->delete($generationPath);
7087
}
88+
89+
// Clean var/di
7190
if ($this->write->isDirectory($diPath)) {
7291
$this->write->delete($diPath);
7392
}
93+
94+
// Clean var/cache
7495
if ($this->write->isDirectory($cachePath)) {
7596
$this->write->delete($cachePath);
7697
}
77-
//add to queue
78-
7998
$this->write->delete(self::REGENERATE_FLAG);
99+
$this->enableCacheTypes($enabledCacheTypes);
80100
}
81101
}
82102

83103
/**
84-
* Read Cache types from env.php and write to a json file.
104+
* Create flag for cleaning up var/generation, var/di and var/cache directories for subsequent
105+
* regeneration of this content
85106
*
86-
* @param string $envPath
87107
* @return void
88108
*/
89-
private function saveCacheStatus($envPath)
109+
public function requestRegeneration()
110+
{
111+
$this->write->touch(self::REGENERATE_FLAG);
112+
}
113+
114+
/**
115+
* Reads Cache configuration from env.php and returns indexed array containing all the enabled cache types.
116+
*
117+
* @return string[]
118+
*/
119+
private function getEnabledCacheTypes()
90120
{
91-
$cacheData = include $envPath;
92-
93-
if (isset($cacheData['cache_types'])) {
94-
$enabledCacheTypes = $cacheData['cache_types'];
95-
$enabledCacheTypes = array_filter($enabledCacheTypes, function ($value) {
96-
return $value;
97-
});
98-
if (!empty($enabledCacheTypes)) {
99-
$varDir = $this->directoryList->getPath(DirectoryList::VAR_DIR);
100-
$this->write->writeFile(
101-
$this->write->getRelativePath($varDir) . '/.cachestates.json',
102-
json_encode($enabledCacheTypes)
103-
);
104-
$cacheTypes = array_keys($cacheData['cache_types']);
121+
$enabledCacheTypes = [];
122+
$envPath = $this->getEnvPath();
123+
if ($this->write->isReadable($this->write->getRelativePath($envPath))) {
124+
$envData = include $envPath;
125+
if (isset($envData['cache_types'])) {
126+
$cacheStatus = $envData['cache_types'];
127+
$enabledCacheTypes = array_filter($cacheStatus, function ($value) {
128+
return $value;
129+
});
130+
$enabledCacheTypes = array_keys($enabledCacheTypes);
131+
}
132+
}
133+
return $enabledCacheTypes;
134+
}
135+
136+
137+
/**
138+
* Returns path to env.php file
139+
*
140+
* @return string
141+
* @throws \Exception
142+
*/
143+
private function getEnvPath()
144+
{
145+
$deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
146+
$configPool = new ConfigFilePool();
147+
$envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
148+
return $envPath;
149+
}
150+
151+
/**
152+
* Disables all cache types by updating env.php.
153+
*
154+
* @return void
155+
*/
156+
private function disableAllCacheTypes()
157+
{
158+
$envPath = $this->getEnvPath();
159+
if ($this->write->isWritable($this->write->getRelativePath($envPath))) {
160+
$envData = include $envPath;
161+
162+
if (isset($envData['cache_types'])) {
163+
$cacheTypes = array_keys($envData['cache_types']);
105164

106165
foreach ($cacheTypes as $cacheType) {
107-
$cacheData['cache_types'][$cacheType] = 0;
166+
$envData['cache_types'][$cacheType] = 0;
108167
}
109168

110169
$formatter = new PhpFormatter();
111-
$contents = $formatter->format($cacheData);
170+
$contents = $formatter->format($envData);
112171

113172
$this->write->writeFile($this->write->getRelativePath($envPath), $contents);
114173
if (function_exists('opcache_invalidate')) {
@@ -121,12 +180,34 @@ private function saveCacheStatus($envPath)
121180
}
122181

123182
/**
124-
* Create flag for regeneration of code and di
183+
* Enables apppropriate cache types in app/etc/env.php based on the passed in $cacheTypes array
184+
* TODO: to be removed in scope of MAGETWO-53476
185+
*
186+
* @param string[]
125187
*
126188
* @return void
127189
*/
128-
public function requestRegeneration()
190+
private function enableCacheTypes($cacheTypes)
129191
{
130-
$this->write->touch(self::REGENERATE_FLAG);
192+
if (empty($cacheTypes)) {
193+
return;
194+
}
195+
$envPath = $this->getEnvPath();
196+
if ($this->write->isReadable($this->write->getRelativePath($envPath))) {
197+
$envData = include $envPath;
198+
foreach ($cacheTypes as $cacheType) {
199+
if (isset($envData['cache_types'][$cacheType])) {
200+
$envData['cache_types'][$cacheType] = 1;
201+
}
202+
}
203+
204+
$formatter = new PhpFormatter();
205+
$contents = $formatter->format($envData);
206+
207+
$this->write->writeFile($this->write->getRelativePath($envPath), $contents);
208+
if (function_exists('opcache_invalidate')) {
209+
opcache_invalidate($this->write->getAbsolutePath($envPath));
210+
}
211+
}
131212
}
132213
}

lib/internal/Magento/Framework/Code/Test/Unit/GeneratedFilesTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ protected function setUp()
4545
* @param array $getPathMap
4646
* @param array $isDirectoryMap
4747
* @param array $deleteMap
48-
* @dataProvider regenerateDataProvider
48+
* @dataProvider cleanGeneratedFilesDataProvider
4949
*/
50-
public function testRegenerate($getPathMap, $isDirectoryMap, $deleteMap)
50+
public function testCleanGeneratedFiles($getPathMap, $isDirectoryMap, $deleteMap)
5151
{
5252

5353
$this->writeInterface
@@ -62,20 +62,21 @@ public function testRegenerate($getPathMap, $isDirectoryMap, $deleteMap)
6262
$this->writeInterface->expects($this->any())->method('getRelativePath')->willReturnMap($getPathMap);
6363
$this->writeInterface->expects($this->any())->method('isDirectory')->willReturnMap($isDirectoryMap);
6464
$this->writeInterface->expects($this->exactly(1))->method('delete')->willReturnMap($deleteMap);
65-
$this->model->regenerate();
65+
$this->model->cleanGeneratedFiles();
6666
}
6767

6868
/**
6969
* @return array
7070
*/
71-
public function regenerateDataProvider()
71+
public function cleanGeneratedFilesDataProvider()
7272
{
7373
$pathToGeneration = 'path/to/generation';
7474
$pathToDi = 'path/to/di';
7575
$pathToCache = 'path/to/di';
7676
$pathToConfig = 'path/to/config';
7777

78-
$getPathMap = [[DirectoryList::GENERATION, $pathToGeneration],
78+
$getPathMap = [
79+
[DirectoryList::GENERATION, $pathToGeneration],
7980
[DirectoryList::DI, $pathToDi],
8081
[DirectoryList::CACHE, $pathToCache],
8182
[DirectoryList::CONFIG, $pathToConfig],
@@ -98,7 +99,7 @@ public function regenerateDataProvider()
9899
];
99100
}
100101

101-
public function testRegenerateWithNoFlag()
102+
public function testCleanGeneratedFilesWithNoFlag()
102103
{
103104
$this->writeInterface
104105
->expects($this->once())
@@ -108,7 +109,7 @@ public function testRegenerateWithNoFlag()
108109
$this->directoryList->expects($this->never())->method('getPath');
109110
$this->writeInterface->expects($this->never())->method('getPath');
110111
$this->writeInterface->expects($this->never())->method('delete');
111-
$this->model->regenerate();
112+
$this->model->cleanGeneratedFiles();
112113
}
113114

114115
public function testRequestRegeneration()

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

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
*/
66
namespace Magento\Setup\Console\Command;
77

8-
use Magento\Framework\App\Filesystem\DirectoryList;
9-
use Magento\Backend\Console\Command\AbstractCacheManageCommand;
10-
use Magento\Framework\ObjectManagerInterface;
118
use Magento\Framework\Setup\ConsoleLogger;
129
use Magento\Setup\Model\InstallerFactory;
1310
use Magento\Setup\Model\ObjectManagerProvider;
14-
use Symfony\Component\Console\Input\ArrayInput;
1511
use Symfony\Component\Console\Input\InputInterface;
1612
use Symfony\Component\Console\Input\InputOption;
1713
use Symfony\Component\Console\Output\OutputInterface;
@@ -34,9 +30,7 @@ class UpgradeCommand extends AbstractSetupCommand
3430
private $installerFactory;
3531

3632
/**
37-
* Object Manager
38-
*
39-
* @var ObjectManagerProvider
33+
* @var \Magento\Setup\Model\ObjectManagerProvider;
4034
*/
4135
private $objectManagerProvider;
4236

@@ -79,9 +73,9 @@ protected function configure()
7973
*/
8074
protected function execute(InputInterface $input, OutputInterface $output)
8175
{
76+
$areaCode = 'setup';
8277
/** @var \Magento\Framework\ObjectManagerInterface $objectManager */
8378
$objectManager = $this->objectManagerProvider->get();
84-
$areaCode = 'setup';
8579
/** @var \Magento\Framework\App\State $appState */
8680
$appState = $objectManager->get('Magento\Framework\App\State');
8781
$appState->setAreaCode($areaCode);
@@ -98,40 +92,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
9892
$output->writeln('<info>Please re-run Magento compile command</info>');
9993
}
10094

101-
return $this->enableCaches($objectManager, $output);
102-
}
103-
104-
/**
105-
* Enables cache if cachestates exists
106-
* TODO: to be removed in scope of MAGETWO-53476
107-
*
108-
* @param \Magento\Framework\ObjectManagerInterface $objectManager
109-
* @param \Symfony\Component\Console\Output\OutputInterface $output
110-
* @return int
111-
*/
112-
private function enableCaches($objectManager, $output)
113-
{
114-
$writeFactory = $objectManager->get('Magento\Framework\Filesystem\Directory\WriteFactory');
115-
$write = $writeFactory->create(BP);
116-
/** @var \Magento\Framework\App\Filesystem\DirectoryList $dirList */
117-
$dirList = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList');
118-
119-
$pathToCacheStatus = $write->getRelativePath($dirList->getPath(DirectoryList::VAR_DIR) . '/.cachestates.json');
120-
121-
if ($write->isExist($pathToCacheStatus)) {
122-
$params = array_keys(json_decode($write->readFile($pathToCacheStatus), true));
123-
$command = $this->getApplication()->find('cache:enable');
124-
125-
$arguments = ['command' => 'cache:enable', AbstractCacheManageCommand::INPUT_KEY_TYPES => $params ];
126-
$returnCode = $command->run(new ArrayInput($arguments), $output);
127-
128-
$write->delete($pathToCacheStatus);
129-
if (isset($returnCode) && $returnCode > 0) {
130-
$message = '<error> Error occured during upgrade. Error code: ' . $returnCode . '</error>';
131-
$output->writeln($message);
132-
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
133-
}
134-
}
13595
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
13696
}
13797
}

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,11 @@ public function testExecute()
2828
$installer->expects($this->at(2))->method('installDataFixtures');
2929
$installerFactory->expects($this->once())->method('create')->willReturn($installer);
3030

31-
$pathToCacheStatus = '/path/to/cachefile';
32-
$writeFactory = $this->getMock('\Magento\Framework\Filesystem\Directory\WriteFactory', [], [], '', false);
33-
$write = $this->getMock('\Magento\Framework\Filesystem\Directory\Write', [], [], '', false);
34-
$write->expects($this->once())->method('isExist')->with('/path/to/cachefile')->willReturn(false);
35-
$write->expects($this->once())->method('getRelativePath')->willReturn($pathToCacheStatus);
36-
37-
$writeFactory->expects($this->once())->method('create')->willReturn($write);
38-
$directoryList = $this->getMock('\Magento\Framework\App\Filesystem\DirectoryList', [], [], '', false);
39-
$objectManager->expects($this->exactly(4))
31+
$objectManager->expects($this->exactly(2))
4032
->method('get')
4133
->will($this->returnValueMap([
4234
['Magento\Framework\App\State', $state],
43-
['Magento\Framework\ObjectManager\ConfigLoaderInterface', $configLoader],
44-
['Magento\Framework\Filesystem\Directory\WriteFactory', $writeFactory],
45-
['Magento\Framework\App\Filesystem\DirectoryList', $directoryList],
35+
['Magento\Framework\ObjectManager\ConfigLoaderInterface', $configLoader]
4636
]));
4737

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

0 commit comments

Comments
 (0)