Skip to content

Commit bb0184c

Browse files
committed
Merge remote-tracking branch 'falcon/MAGETWO-62655' into MAGETWO-62964
2 parents b97ff82 + 689fb1a commit bb0184c

File tree

4 files changed

+278
-107
lines changed

4 files changed

+278
-107
lines changed

bin/magento

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ try {
2828
echo "\n\n";
2929
$e = $e->getPrevious();
3030
}
31-
exit(Cli::RETURN_FAILURE);
31+
exit(Magento\Framework\Console\Cli::RETURN_FAILURE);
3232
}

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

Lines changed: 158 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,102 +5,105 @@
55
*/
66
namespace Magento\Framework\Console;
77

8+
use Magento\Framework\App\DeploymentConfig;
89
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\App\State;
11+
use Magento\Framework\Code\Generator\Io;
912
use Magento\Framework\Composer\ComposerJsonFinder;
10-
use Symfony\Component\Console\Input\InputInterface;
11-
use Symfony\Component\Console\Output\OutputInterface;
12-
use Symfony\Component\Console\Output\ConsoleOutput;
13-
use Symfony\Component\Console\Input\ArgvInput;
14-
use Symfony\Component\Console\Application as SymfonyApplication;
13+
use Magento\Framework\Exception\FileSystemException;
14+
use Magento\Setup\Model\ObjectManagerProvider;
15+
use Symfony\Component\Console;
1516
use Magento\Framework\App\Bootstrap;
1617
use Magento\Framework\Filesystem\Driver\File;
1718
use Magento\Framework\Shell\ComplexParameter;
1819
use Magento\Setup\Console\CompilerPreparation;
19-
use \Magento\Framework\App\ProductMetadata;
20+
use Magento\Framework\App\ProductMetadata;
21+
use Magento\Framework\ObjectManagerInterface;
22+
use Zend\ServiceManager\ServiceManager;
2023

2124
/**
22-
* Magento 2 CLI Application. This is the hood for all command line tools supported by Magento
25+
* Magento 2 CLI Application.
26+
* This is the hood for all command line tools supported by Magento.
2327
*
2428
* {@inheritdoc}
2529
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2630
*/
27-
class Cli extends SymfonyApplication
31+
class Cli extends Console\Application
2832
{
2933
/**
30-
* Name of input option
34+
* Name of input option.
3135
*/
3236
const INPUT_KEY_BOOTSTRAP = 'bootstrap';
3337

34-
/**
35-
* Cli exit codes
38+
/**#@+
39+
* Cli exit codes.
3640
*/
3741
const RETURN_SUCCESS = 0;
3842
const RETURN_FAILURE = 1;
43+
/**#@-*/
3944

40-
/** @var \Zend\ServiceManager\ServiceManager */
45+
/**
46+
* Service Manager.
47+
*
48+
* @var ServiceManager
49+
*/
4150
private $serviceManager;
4251

4352
/**
44-
* Initialization exception
53+
* Initialization exception.
4554
*
4655
* @var \Exception
4756
*/
4857
private $initException;
4958

5059
/**
51-
* @param string $name application name
52-
* @param string $version application version
53-
* @SuppressWarnings(PHPMD.ExitExpression)
60+
* Object Manager.
61+
*
62+
* @var ObjectManagerInterface
63+
*/
64+
private $objectManager;
65+
66+
/**
67+
* @param string $name the application name
68+
* @param string $version the application version
5469
*/
5570
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
5671
{
5772
$this->serviceManager = \Zend\Mvc\Application::init(require BP . '/setup/config/application.config.php')
5873
->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-
}
68-
/**
69-
* Temporary workaround until the compiler is able to clear the generation directory
70-
* @todo remove after MAGETWO-44493 resolved
71-
*/
72-
if (class_exists(CompilerPreparation::class)) {
73-
$compilerPreparation = new CompilerPreparation($this->serviceManager, new ArgvInput(), new File());
74-
$compilerPreparation->handleCompilerEnvironment();
75-
}
74+
75+
$this->assertCompilerPreparation();
76+
$this->initObjectManager();
77+
$this->assertGenerationPermissions();
7678

7779
if ($version == 'UNKNOWN') {
78-
$directoryList = new DirectoryList(BP);
80+
$directoryList = new DirectoryList(BP);
7981
$composerJsonFinder = new ComposerJsonFinder($directoryList);
80-
$productMetadata = new ProductMetadata($composerJsonFinder);
82+
$productMetadata = new ProductMetadata($composerJsonFinder);
8183
$version = $productMetadata->getVersion();
8284
}
85+
8386
parent::__construct($name, $version);
8487
}
8588

8689
/**
87-
* Process an error happened during initialization of commands, if any
90+
* {@inheritdoc}
8891
*
89-
* @param InputInterface $input
90-
* @param OutputInterface $output
91-
* @return int
92-
* @throws \Exception
92+
* @throws \Exception the exception in case of unexpected error
9393
*/
94-
public function doRun(InputInterface $input, OutputInterface $output)
94+
public function doRun(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
9595
{
9696
$exitCode = parent::doRun($input, $output);
97+
9798
if ($this->initException) {
9899
$output->writeln(
99100
"<error>We're sorry, an error occurred. Try clearing the cache and code generation directories. "
100101
. "By default, they are: var/cache, var/di, var/generation, and var/page_cache.</error>"
101102
);
103+
102104
throw $this->initException;
103105
}
106+
104107
return $exitCode;
105108
}
106109

@@ -113,46 +116,141 @@ protected function getDefaultCommands()
113116
}
114117

115118
/**
116-
* Gets application commands
119+
* Gets application commands.
117120
*
118-
* @return array
121+
* @return array a list of available application commands
119122
*/
120123
protected function getApplicationCommands()
121124
{
122125
$commands = [];
123126
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-
/** @var \Magento\Setup\Model\ObjectManagerProvider $omProvider */
130-
$omProvider = $this->serviceManager->get(\Magento\Setup\Model\ObjectManagerProvider::class);
131-
$omProvider->setObjectManager($objectManager);
132-
133127
if (class_exists(\Magento\Setup\Console\CommandList::class)) {
134128
$setupCommandList = new \Magento\Setup\Console\CommandList($this->serviceManager);
135129
$commands = array_merge($commands, $setupCommandList->getCommands());
136130
}
137131

138-
if ($objectManager->get(\Magento\Framework\App\DeploymentConfig::class)->isAvailable()) {
139-
/** @var \Magento\Framework\Console\CommandListInterface */
140-
$commandList = $objectManager->create(\Magento\Framework\Console\CommandListInterface::class);
132+
if ($this->objectManager->get(DeploymentConfig::class)->isAvailable()) {
133+
/** @var CommandListInterface */
134+
$commandList = $this->objectManager->create(CommandListInterface::class);
141135
$commands = array_merge($commands, $commandList->getCommands());
142136
}
143137

144-
$commands = array_merge($commands, $this->getVendorCommands($objectManager));
138+
$commands = array_merge(
139+
$commands,
140+
$this->getVendorCommands($this->objectManager)
141+
);
145142
} catch (\Exception $e) {
146143
$this->initException = $e;
147144
}
145+
148146
return $commands;
149147
}
150148

151149
/**
152-
* Gets vendor commands
150+
* Object Manager initialization.
151+
*
152+
* @return void
153+
* @SuppressWarnings(PHPMD.ExitExpression)
154+
*/
155+
private function initObjectManager()
156+
{
157+
try {
158+
$params = (new ComplexParameter(self::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER);
159+
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
160+
161+
$this->objectManager = Bootstrap::create(BP, $params)->getObjectManager();
162+
163+
/** @var ObjectManagerProvider $omProvider */
164+
$omProvider = $this->serviceManager->get(ObjectManagerProvider::class);
165+
$omProvider->setObjectManager($this->objectManager);
166+
} catch (FileSystemException $exception) {
167+
$this->writeGenerationDirectoryReadError();
168+
169+
exit(static::RETURN_FAILURE);
170+
}
171+
}
172+
173+
/**
174+
* Checks whether generation directory is read-only.
175+
* Depends on the current mode:
176+
* production - application will proceed
177+
* default - application will be terminated
178+
* developer - application will be terminated
179+
*
180+
* @return void
181+
* @SuppressWarnings(PHPMD.ExitExpression)
182+
*/
183+
private function assertGenerationPermissions()
184+
{
185+
/** @var GenerationDirectoryAccess $generationDirectoryAccess */
186+
$generationDirectoryAccess = $this->objectManager->create(
187+
GenerationDirectoryAccess::class,
188+
['serviceManager' => $this->serviceManager]
189+
);
190+
/** @var State $state */
191+
$state = $this->objectManager->get(State::class);
192+
193+
if (
194+
$state->getMode() !== State::MODE_PRODUCTION
195+
&& !$generationDirectoryAccess->check()
196+
) {
197+
$this->writeGenerationDirectoryReadError();
198+
199+
exit(static::RETURN_FAILURE);
200+
}
201+
}
202+
203+
/**
204+
* Checks whether compiler is being prepared.
205+
*
206+
* @return void
207+
* @SuppressWarnings(PHPMD.ExitExpression)
208+
*/
209+
private function assertCompilerPreparation()
210+
{
211+
/**
212+
* Temporary workaround until the compiler is able to clear the generation directory
213+
* @todo remove after MAGETWO-44493 resolved
214+
*/
215+
if (class_exists(CompilerPreparation::class)) {
216+
$compilerPreparation = new CompilerPreparation(
217+
$this->serviceManager,
218+
new Console\Input\ArgvInput(),
219+
new File()
220+
);
221+
222+
try {
223+
$compilerPreparation->handleCompilerEnvironment();
224+
} catch (FileSystemException $e) {
225+
$this->writeGenerationDirectoryReadError();
226+
227+
exit(static::RETURN_FAILURE);
228+
}
229+
}
230+
}
231+
232+
/**
233+
* Writes read error to console.
153234
*
154-
* @param \Magento\Framework\ObjectManagerInterface $objectManager
155-
* @return array
235+
* @return void
236+
*/
237+
private function writeGenerationDirectoryReadError()
238+
{
239+
$output = new Console\Output\ConsoleOutput();
240+
$output->writeln(
241+
'<error>'
242+
. 'Command line user does not have read and write permissions on ' . Io::DEFAULT_DIRECTORY . ' directory. '
243+
. 'Please address this issue before using Magento command line.'
244+
. '</error>'
245+
);
246+
}
247+
248+
/**
249+
* Retrieves vendor commands.
250+
*
251+
* @param ObjectManagerInterface $objectManager the object manager
252+
*
253+
* @return array an array with external commands
156254
*/
157255
protected function getVendorCommands($objectManager)
158256
{
@@ -165,6 +263,7 @@ protected function getVendorCommands($objectManager)
165263
);
166264
}
167265
}
266+
168267
return $commands;
169268
}
170269
}

0 commit comments

Comments
 (0)