Skip to content

Commit 70bd853

Browse files
author
Yevhen Miroshnychenko
authored
Merge pull request #2406 from magento-thunder/MAGETWO-89747
Fixed issues: - MAGETWO-89747: [Backport 2.1.x] Magento application compatible with cloud
2 parents f582704 + c93299c commit 70bd853

File tree

5 files changed

+79
-30
lines changed

5 files changed

+79
-30
lines changed

lib/internal/Magento/Framework/Console/Cli.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Shell\ComplexParameter;
1818
use Magento\Setup\Console\CompilerPreparation;
1919
use \Magento\Framework\App\ProductMetadata;
20+
use Magento\Framework\App\State;
2021

2122
/**
2223
* Magento 2 CLI Application. This is the hood for all command line tools supported by Magento
@@ -47,6 +48,11 @@ class Cli extends SymfonyApplication
4748
*/
4849
private $initException;
4950

51+
/**
52+
* @var \Magento\Framework\ObjectManagerInterface
53+
*/
54+
private $objectManager;
55+
5056
/**
5157
* @param string $name application name
5258
* @param string $version application version
@@ -56,15 +62,7 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
5662
{
5763
$this->serviceManager = \Zend\Mvc\Application::init(require BP . '/setup/config/application.config.php')
5864
->getServiceManager();
59-
$generationDirectoryAccess = new GenerationDirectoryAccess($this->serviceManager);
60-
if (!$generationDirectoryAccess->check()) {
61-
$output = new ConsoleOutput();
62-
$output->writeln(
63-
'<error>Command line user does not have read and write permissions on var/generation directory. Please'
64-
. ' address this issue before using Magento command line.</error>'
65-
);
66-
exit(0);
67-
}
65+
6866
/**
6967
* Temporary workaround until the compiler is able to clear the generation directory
7068
* @todo remove after MAGETWO-44493 resolved
@@ -74,6 +72,21 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
7472
$compilerPreparation->handleCompilerEnvironment();
7573
}
7674

75+
$bootstrapParam = new ComplexParameter(self::INPUT_KEY_BOOTSTRAP);
76+
$params = $bootstrapParam->mergeFromArgv($_SERVER, $_SERVER);
77+
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
78+
$bootstrap = Bootstrap::create(BP, $params);
79+
$this->objectManager = $bootstrap->getObjectManager();
80+
81+
if ($this->checkGenerationDirectoryAccess()) {
82+
$output = new ConsoleOutput();
83+
$output->writeln(
84+
'<error>Command line user does not have read and write permissions on var/generation directory. Please'
85+
. ' address this issue before using Magento command line.</error>'
86+
);
87+
exit(0);
88+
}
89+
7790
if ($version == 'UNKNOWN') {
7891
$directoryList = new DirectoryList(BP);
7992
$composerJsonFinder = new ComposerJsonFinder($directoryList);
@@ -83,6 +96,22 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
8396
parent::__construct($name, $version);
8497
}
8598

99+
/**
100+
* Check generation directory access.
101+
*
102+
* Skip and return true if production mode is enabled.
103+
*
104+
* @return bool
105+
*/
106+
private function checkGenerationDirectoryAccess()
107+
{
108+
$generationDirectoryAccess = new GenerationDirectoryAccess($this->serviceManager);
109+
/** @var State $state */
110+
$state = $this->objectManager->create(State::class);
111+
112+
return $state->getMode() !== State::MODE_PRODUCTION && !$generationDirectoryAccess->check();
113+
}
114+
86115
/**
87116
* Process an error happened during initialization of commands, if any
88117
*
@@ -121,31 +150,25 @@ protected function getApplicationCommands()
121150
{
122151
$commands = [];
123152
try {
124-
$bootstrapParam = new ComplexParameter(self::INPUT_KEY_BOOTSTRAP);
125-
$params = $bootstrapParam->mergeFromArgv($_SERVER, $_SERVER);
126-
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
127-
$bootstrap = Bootstrap::create(BP, $params);
128-
$objectManager = $bootstrap->getObjectManager();
129-
130153
// Specialized setup command list available before and after M2 install
131154
if (class_exists('Magento\Setup\Console\CommandList')
132155
&& class_exists('Magento\Setup\Model\ObjectManagerProvider')
133156
) {
134157
/** @var \Magento\Setup\Model\ObjectManagerProvider $omProvider */
135158
$omProvider = $this->serviceManager->get(\Magento\Setup\Model\ObjectManagerProvider::class);
136-
$omProvider->setObjectManager($objectManager);
159+
$omProvider->setObjectManager($this->objectManager);
137160
$setupCommandList = new \Magento\Setup\Console\CommandList($this->serviceManager);
138161
$commands = array_merge($commands, $setupCommandList->getCommands());
139162
}
140163

141164
// Allowing instances of all modular commands only after M2 install
142-
if ($objectManager->get(\Magento\Framework\App\DeploymentConfig::class)->isAvailable()) {
165+
if ($this->objectManager->get(\Magento\Framework\App\DeploymentConfig::class)->isAvailable()) {
143166
/** @var \Magento\Framework\Console\CommandListInterface $commandList */
144-
$commandList = $objectManager->create(\Magento\Framework\Console\CommandListInterface::class);
167+
$commandList = $this->objectManager->create(\Magento\Framework\Console\CommandListInterface::class);
145168
$commands = array_merge($commands, $commandList->getCommands());
146169
}
147170

148-
$commands = array_merge($commands, $this->getVendorCommands($objectManager));
171+
$commands = array_merge($commands, $this->getVendorCommands($this->objectManager));
149172
} catch (\Exception $e) {
150173
$this->initException = $e;
151174
}

lib/internal/Magento/Framework/Setup/FilePermissions.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Framework\Backup\Filesystem\Iterator\Filter;
1010
use Magento\Framework\Filesystem\Filter\ExcludeFilter;
1111
use Magento\Framework\Filesystem;
12+
use Magento\Framework\App\State;
13+
use Magento\Framework\App\ObjectManager;
1214

1315
class FilePermissions
1416
{
@@ -22,6 +24,11 @@ class FilePermissions
2224
*/
2325
protected $directoryList;
2426

27+
/**
28+
* @var State
29+
*/
30+
private $state;
31+
2532
/**
2633
* List of required writable directories for installation
2734
*
@@ -60,13 +67,16 @@ class FilePermissions
6067
/**
6168
* @param Filesystem $filesystem
6269
* @param DirectoryList $directoryList
70+
* @param State $state
6371
*/
6472
public function __construct(
6573
Filesystem $filesystem,
66-
DirectoryList $directoryList
74+
DirectoryList $directoryList,
75+
State $state = null
6776
) {
6877
$this->filesystem = $filesystem;
6978
$this->directoryList = $directoryList;
79+
$this->state = $state ?: ObjectManager::getInstance()->get(State::class);
7080
}
7181

7282
/**
@@ -141,12 +151,15 @@ private function checkRecursiveDirectories($directory)
141151
new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS),
142152
\RecursiveIteratorIterator::CHILD_FIRST
143153
);
144-
$noWritableFilesFolders = [
145-
$this->directoryList->getPath(DirectoryList::GENERATION) . '/',
146-
$this->directoryList->getPath(DirectoryList::DI) . '/',
147-
];
148154

149-
$directoryIterator = new Filter($directoryIterator, $noWritableFilesFolders);
155+
$generationPath = $this->directoryList->getPath(DirectoryList::GENERATION);
156+
$diPath = $this->directoryList->getPath(DirectoryList::DI);
157+
158+
if ($this->state->getMode() === State::MODE_PRODUCTION) {
159+
$directoryIterator = new ExcludeFilter($directoryIterator, [$generationPath, $diPath]);
160+
} else {
161+
$directoryIterator = new Filter($directoryIterator, [$generationPath . '/', $diPath . '/']);
162+
}
150163

151164
$directoryIterator = new ExcludeFilter(
152165
$directoryIterator,

lib/internal/Magento/Framework/Setup/Test/Unit/FilePermissionsTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use \Magento\Framework\Setup\FilePermissions;
99
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\App\State;
1011

1112
class FilePermissionsTest extends \PHPUnit_Framework_TestCase
1213
{
@@ -25,6 +26,11 @@ class FilePermissionsTest extends \PHPUnit_Framework_TestCase
2526
*/
2627
private $directoryListMock;
2728

29+
/**
30+
* @var \PHPUnit_Framework_MockObject_MockObject|State
31+
*/
32+
private $stateMock;
33+
2834
/**
2935
* @var FilePermissions
3036
*/
@@ -34,6 +40,7 @@ public function setUp()
3440
{
3541
$this->directoryWriteMock = $this->getMock('Magento\Framework\Filesystem\Directory\Write', [], [], '', false);
3642
$this->filesystemMock = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
43+
$this->stateMock = $this->getMock(State::class, [], [], '', false);
3744

3845
$this->filesystemMock
3946
->expects($this->any())
@@ -43,7 +50,8 @@ public function setUp()
4350

4451
$this->filePermissions = new FilePermissions(
4552
$this->filesystemMock,
46-
$this->directoryListMock
53+
$this->directoryListMock,
54+
$this->stateMock
4755
);
4856
}
4957

@@ -219,6 +227,7 @@ public function setUpDirectoryListInstallation()
219227
->method('getPath')
220228
->with(DirectoryList::STATIC_VIEW)
221229
->will($this->returnValue(BP . '/pub/static'));
230+
222231
}
223232

224233
public function setUpDirectoryWriteInstallation()

setup/index.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
use Zend\Mvc\Application;
7+
use Magento\Setup\Model\ObjectManagerProvider;
68

79
if (PHP_SAPI == 'cli') {
810
echo "You cannot run this from the command line." . PHP_EOL .
@@ -28,4 +30,8 @@
2830
$handler = new \Magento\Framework\App\ErrorHandler();
2931
set_error_handler([$handler, 'handler']);
3032

31-
\Zend\Mvc\Application::init(require __DIR__ . '/config/application.config.php')->run();
33+
$application = Application::init(require __DIR__ . '/config/application.config.php');
34+
$application->getServiceManager()
35+
->get(ObjectManagerProvider::class)
36+
->setObjectManager(\Magento\Framework\App\Bootstrap::create(BP, $_SERVER)->getObjectManager());
37+
$application->run();

setup/src/Magento/Setup/Mvc/Bootstrap/InitParamListener.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Setup\Mvc\Bootstrap;
87

98
use Magento\Framework\App\Bootstrap as AppBootstrap;
@@ -137,8 +136,7 @@ public function authPreDispatch($event)
137136
/** @var \Magento\Backend\Model\Auth $auth */
138137
$authentication = $objectManager->get(\Magento\Backend\Model\Auth::class);
139138

140-
if (
141-
!$authentication->isLoggedIn() ||
139+
if (!$authentication->isLoggedIn() ||
142140
!$adminSession->isAllowed('Magento_Backend::setup_wizard')
143141
) {
144142
$adminSession->destroy();

0 commit comments

Comments
 (0)