Skip to content

Commit 2d09bd0

Browse files
author
Dmytro Vilchynskyi
committed
Merge remote-tracking branch 'origin/MAGETWO-70571' into PR_09_08
# Conflicts: # app/code/Magento/Deploy/Model/Mode.php # app/code/Magento/Deploy/Test/Unit/Model/ModeTest.php
2 parents ea214fe + 409997b commit 2d09bd0

File tree

8 files changed

+411
-13
lines changed

8 files changed

+411
-13
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Deploy\App\Mode;
7+
8+
/**
9+
* This class is responsible for providing configuration while switching application mode
10+
*/
11+
class ConfigProvider
12+
{
13+
/**
14+
* Configuration for combinations of $currentMode and $targetMode
15+
* For example:
16+
* [
17+
* 'developer' => [
18+
* 'production' => [
19+
* {{setting_path}} => {{setting_value}}
20+
* ]
21+
* ]
22+
* ]
23+
*
24+
* @var array
25+
*/
26+
private $config;
27+
28+
/**
29+
* @param array $config
30+
*/
31+
public function __construct(array $config = [])
32+
{
33+
$this->config = $config;
34+
}
35+
36+
/**
37+
* Provide configuration while switching from $currentMode to $targetMode
38+
* This method used in \Magento\Deploy\Model\Mode::setStoreMode
39+
*
40+
* For example: while switching from developer mode to production mode
41+
* need to turn off 'dev/debug/debug_logging' setting in this case method
42+
* will return array
43+
* [
44+
* {{setting_path}} => {{setting_value}}
45+
* ]
46+
*
47+
* @param string $currentMode
48+
* @param string $targetMode
49+
* @return array
50+
*/
51+
public function getConfigs($currentMode, $targetMode)
52+
{
53+
if (isset($this->config[$currentMode][$targetMode])) {
54+
return $this->config[$currentMode][$targetMode];
55+
}
56+
return [];
57+
}
58+
}

app/code/Magento/Deploy/Model/Mode.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Deploy\Model;
88

9+
use Magento\Deploy\App\Mode\ConfigProvider;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
911
use Magento\Framework\App\DeploymentConfig\Reader;
1012
use Magento\Framework\App\DeploymentConfig\Writer;
1113
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -14,6 +16,8 @@
1416
use Magento\Framework\Config\File\ConfigFilePool;
1517
use Symfony\Component\Console\Input\InputInterface;
1618
use Symfony\Component\Console\Output\OutputInterface;
19+
use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
20+
use Magento\Config\Console\Command\EmulatedAdminhtmlAreaProcessor;
1721
use Magento\Framework\Exception\LocalizedException;
1822

1923
/**
@@ -44,33 +48,66 @@ class Mode
4448
*/
4549
private $reader;
4650

51+
/**
52+
* @var MaintenanceMode
53+
*/
54+
private $maintenanceMode;
55+
4756
/**
4857
* @var Filesystem
4958
*/
5059
private $filesystem;
5160

61+
/**
62+
* @var ConfigProvider
63+
*/
64+
private $configProvider;
65+
66+
/**
67+
* The factory for processor facade.
68+
*
69+
* @var ProcessorFacadeFactory
70+
*/
71+
private $processorFacadeFactory;
72+
73+
/**
74+
* Emulator adminhtml area for CLI command.
75+
*
76+
* @var EmulatedAdminhtmlAreaProcessor
77+
*/
78+
private $emulatedAreaProcessor;
79+
5280
/**
5381
* @param InputInterface $input
5482
* @param OutputInterface $output
5583
* @param Writer $writer
5684
* @param Reader $reader
5785
* @param MaintenanceMode $maintenanceMode
5886
* @param Filesystem $filesystem
87+
* @param ConfigProvider $configProvider
88+
* @param ProcessorFacadeFactory $processorFacadeFactory
89+
* @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor
5990
*/
6091
public function __construct(
6192
InputInterface $input,
6293
OutputInterface $output,
6394
Writer $writer,
6495
Reader $reader,
6596
MaintenanceMode $maintenanceMode,
66-
Filesystem $filesystem
97+
Filesystem $filesystem,
98+
ConfigProvider $configProvider,
99+
ProcessorFacadeFactory $processorFacadeFactory,
100+
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor
67101
) {
68102
$this->input = $input;
69103
$this->output = $output;
70104
$this->writer = $writer;
71105
$this->reader = $reader;
72106
$this->maintenanceMode = $maintenanceMode;
73107
$this->filesystem = $filesystem;
108+
$this->configProvider = $configProvider;
109+
$this->processorFacadeFactory = $processorFacadeFactory;
110+
$this->emulatedAreaProcessor = $emulatedAreaProcessor;
74111
}
75112

76113
/**
@@ -145,6 +182,7 @@ public function getMode()
145182
*/
146183
protected function setStoreMode($mode)
147184
{
185+
$this->saveAppConfigs($mode);
148186
$data = [
149187
ConfigFilePool::APP_ENV => [
150188
State::PARAM_MODE => $mode
@@ -153,6 +191,29 @@ protected function setStoreMode($mode)
153191
$this->writer->saveConfig($data);
154192
}
155193

194+
/**
195+
* Save application configs while switching mode
196+
*
197+
* @param string $mode
198+
* @return void
199+
*/
200+
private function saveAppConfigs($mode)
201+
{
202+
$configs = $this->configProvider->getConfigs($this->getMode(), $mode);
203+
foreach ($configs as $path => $value) {
204+
$this->emulatedAreaProcessor->process(function () use ($path, $value) {
205+
$this->processorFacadeFactory->create()->process(
206+
$path,
207+
$value,
208+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
209+
null,
210+
true
211+
);
212+
});
213+
$this->output->writeln('Config "' . $path . ' = ' . $value . '" has been saved.');
214+
}
215+
}
216+
156217
/**
157218
* Enable maintenance mode
158219
*
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Deploy\Test\Unit\App\Mode;
7+
8+
use Magento\Deploy\App\Mode\ConfigProvider;
9+
10+
class ConfigProviderTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testGetConfigs()
13+
{
14+
$expectedValue = [
15+
'{{setting_path}}' => '{{setting_value}}'
16+
];
17+
$configProvider = new ConfigProvider(
18+
[
19+
'developer' => [
20+
'production' => $expectedValue
21+
]
22+
]
23+
);
24+
$this->assertEquals($expectedValue, $configProvider->getConfigs('developer', 'production'));
25+
$this->assertEquals([], $configProvider->getConfigs('undefined', 'production'));
26+
}
27+
}

app/code/Magento/Deploy/Test/Unit/Model/ModeTest.php

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
*/
66
namespace Magento\Deploy\Test\Unit\Model;
77

8+
use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
9+
use Magento\Config\Console\Command\ConfigSet\ProcessorFacade;
10+
use Magento\Config\Console\Command\EmulatedAdminhtmlAreaProcessor;
11+
use Magento\Deploy\App\Mode\ConfigProvider;
812
use Magento\Deploy\Model\Filesystem;
913
use Magento\Deploy\Model\Mode;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
1015
use Magento\Framework\App\DeploymentConfig\Reader;
1116
use Magento\Framework\App\DeploymentConfig\Writer;
1217
use Magento\Framework\App\MaintenanceMode;
@@ -19,6 +24,7 @@
1924

2025
/**
2126
* @inheritdoc
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2228
*/
2329
class ModeTest extends \PHPUnit\Framework\TestCase
2430
{
@@ -57,6 +63,26 @@ class ModeTest extends \PHPUnit\Framework\TestCase
5763
*/
5864
private $filesystemMock;
5965

66+
/**
67+
* @var ConfigProvider|Mock
68+
*/
69+
private $configProvider;
70+
71+
/**
72+
* @var ProcessorFacadeFactory|Mock
73+
*/
74+
private $processorFacadeFactory;
75+
76+
/**
77+
* @var ProcessorFacade|Mock
78+
*/
79+
private $processorFacade;
80+
81+
/**
82+
* @var EmulatedAdminhtmlAreaProcessor|Mock
83+
*/
84+
private $emulatedAreaProcessor;
85+
6086
protected function setUp()
6187
{
6288
$this->inputMock = $this->getMockBuilder(InputInterface::class)
@@ -75,14 +101,30 @@ protected function setUp()
75101
$this->filesystemMock = $this->getMockBuilder(Filesystem::class)
76102
->disableOriginalConstructor()
77103
->getMock();
104+
$this->configProvider = $this->getMockBuilder(ConfigProvider::class)
105+
->disableOriginalConstructor()
106+
->getMock();
107+
$this->processorFacadeFactory = $this->getMockBuilder(ProcessorFacadeFactory::class)
108+
->disableOriginalConstructor()
109+
->setMethods(['create'])
110+
->getMockForAbstractClass();
111+
$this->processorFacade = $this->getMockBuilder(ProcessorFacade::class)
112+
->disableOriginalConstructor()
113+
->getMock();
114+
$this->emulatedAreaProcessor = $this->getMockBuilder(EmulatedAdminhtmlAreaProcessor::class)
115+
->disableOriginalConstructor()
116+
->getMock();
78117

79118
$this->model = new Mode(
80119
$this->inputMock,
81120
$this->outputMock,
82121
$this->writerMock,
83122
$this->readerMock,
84123
$this->maintenanceMock,
85-
$this->filesystemMock
124+
$this->filesystemMock,
125+
$this->configProvider,
126+
$this->processorFacadeFactory,
127+
$this->emulatedAreaProcessor
86128
);
87129
}
88130

@@ -165,4 +207,41 @@ public function testEnableDeveloperModeOnFail()
165207
$this->model->enableProductionMode();
166208
$this->assertEquals(State::MODE_PRODUCTION, $mode);
167209
}
210+
211+
public function testEnableProductionModeMinimal()
212+
{
213+
$this->readerMock->expects($this->once())
214+
->method('load')
215+
->willReturn([State::PARAM_MODE => State::MODE_DEVELOPER]);
216+
$this->configProvider->expects($this->once())
217+
->method('getConfigs')
218+
->with('developer', 'production')
219+
->willReturn([
220+
'dev/debug/debug_logging' => 0
221+
]);
222+
$this->emulatedAreaProcessor->expects($this->once())
223+
->method('process')
224+
->willReturnCallback(function (\Closure $closure) {
225+
return $closure->call($this->model);
226+
});
227+
228+
$this->processorFacadeFactory->expects($this->once())
229+
->method('create')
230+
->willReturn($this->processorFacade);
231+
$this->processorFacade
232+
->expects($this->once())
233+
->method('process')
234+
->with(
235+
'dev/debug/debug_logging',
236+
0,
237+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
238+
null,
239+
true
240+
);
241+
$this->outputMock->expects($this->once())
242+
->method('writeln')
243+
->with('Config "dev/debug/debug_logging = 0" has been saved.');
244+
245+
$this->model->enableProductionModeMinimal();
246+
}
168247
}

app/code/Magento/Deploy/etc/di.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,15 @@
7070
</argument>
7171
</arguments>
7272
</type>
73+
<type name="Magento\Deploy\App\Mode\ConfigProvider">
74+
<arguments>
75+
<argument name="config" xsi:type="array">
76+
<item name="developer" xsi:type="array">
77+
<item name="production" xsi:type="array">
78+
<item name="dev/debug/debug_logging" xsi:type="string">0</item>
79+
</item>
80+
</item>
81+
</argument>
82+
</arguments>
83+
</type>
7384
</config>

app/code/Magento/Developer/Model/Logger/Handler/Debug.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public function isHandling(array $record)
6060
if ($this->deploymentConfig->isAvailable()) {
6161
return
6262
parent::isHandling($record)
63-
&& $this->state->getMode() !== State::MODE_PRODUCTION
6463
&& $this->scopeConfig->getValue('dev/debug/debug_logging', ScopeInterface::SCOPE_STORE);
6564
}
6665

app/code/Magento/Developer/Test/Unit/Model/Logger/Handler/DebugTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ public function testHandle()
8585
$this->deploymentConfigMock->expects($this->once())
8686
->method('isAvailable')
8787
->willReturn(true);
88-
$this->stateMock->expects($this->once())
89-
->method('getMode')
90-
->willReturn(State::MODE_DEVELOPER);
88+
$this->stateMock->expects($this->never())
89+
->method('getMode');
9190
$this->scopeConfigMock->expects($this->once())
9291
->method('getValue')
9392
->with('dev/debug/debug_logging', ScopeInterface::SCOPE_STORE, null)
@@ -101,10 +100,9 @@ public function testHandleDisabledByProduction()
101100
$this->deploymentConfigMock->expects($this->once())
102101
->method('isAvailable')
103102
->willReturn(true);
104-
$this->stateMock->expects($this->once())
105-
->method('getMode')
106-
->willReturn(State::MODE_PRODUCTION);
107-
$this->scopeConfigMock->expects($this->never())
103+
$this->stateMock->expects($this->never())
104+
->method('getMode');
105+
$this->scopeConfigMock->expects($this->once())
108106
->method('getValue');
109107

110108
$this->assertFalse($this->model->isHandling(['formatted' => false, 'level' => Logger::DEBUG]));
@@ -115,9 +113,8 @@ public function testHandleDisabledByConfig()
115113
$this->deploymentConfigMock->expects($this->once())
116114
->method('isAvailable')
117115
->willReturn(true);
118-
$this->stateMock->expects($this->once())
119-
->method('getMode')
120-
->willReturn(State::MODE_DEVELOPER);
116+
$this->stateMock->expects($this->never())
117+
->method('getMode');
121118
$this->scopeConfigMock->expects($this->once())
122119
->method('getValue')
123120
->with('dev/debug/debug_logging', ScopeInterface::SCOPE_STORE, null)

0 commit comments

Comments
 (0)