5
5
*/
6
6
namespace Magento \Framework \Console ;
7
7
8
+ use Magento \Framework \App \DeploymentConfig ;
8
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
+ use Magento \Framework \App \State ;
11
+ use Magento \Framework \Code \Generator \Io ;
9
12
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 ;
15
16
use Magento \Framework \App \Bootstrap ;
16
17
use Magento \Framework \Filesystem \Driver \File ;
17
18
use Magento \Framework \Shell \ComplexParameter ;
18
19
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 ;
20
23
21
24
/**
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.
23
27
*
24
28
* {@inheritdoc}
25
29
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26
30
*/
27
- class Cli extends SymfonyApplication
31
+ class Cli extends Console \Application
28
32
{
29
33
/**
30
- * Name of input option
34
+ * Name of input option.
31
35
*/
32
36
const INPUT_KEY_BOOTSTRAP = 'bootstrap ' ;
33
37
34
- /**
35
- * Cli exit codes
38
+ /**#@+
39
+ * Cli exit codes.
36
40
*/
37
41
const RETURN_SUCCESS = 0 ;
38
42
const RETURN_FAILURE = 1 ;
43
+ /**#@-*/
39
44
40
- /** @var \Zend\ServiceManager\ServiceManager */
45
+ /**
46
+ * Service Manager.
47
+ *
48
+ * @var ServiceManager
49
+ */
41
50
private $ serviceManager ;
42
51
43
52
/**
44
- * Initialization exception
53
+ * Initialization exception.
45
54
*
46
55
* @var \Exception
47
56
*/
48
57
private $ initException ;
49
58
50
59
/**
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
54
69
*/
55
70
public function __construct ($ name = 'UNKNOWN ' , $ version = 'UNKNOWN ' )
56
71
{
57
72
$ this ->serviceManager = \Zend \Mvc \Application::init (require BP . '/setup/config/application.config.php ' )
58
73
->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 ();
76
78
77
79
if ($ version == 'UNKNOWN ' ) {
78
- $ directoryList = new DirectoryList (BP );
80
+ $ directoryList = new DirectoryList (BP );
79
81
$ composerJsonFinder = new ComposerJsonFinder ($ directoryList );
80
- $ productMetadata = new ProductMetadata ($ composerJsonFinder );
82
+ $ productMetadata = new ProductMetadata ($ composerJsonFinder );
81
83
$ version = $ productMetadata ->getVersion ();
82
84
}
85
+
83
86
parent ::__construct ($ name , $ version );
84
87
}
85
88
86
89
/**
87
- * Process an error happened during initialization of commands, if any
90
+ * {@inheritdoc}
88
91
*
89
- * @param InputInterface $input
90
- * @param OutputInterface $output
91
- * @return int
92
- * @throws \Exception
92
+ * @throws \Exception the exception in case of unexpected error
93
93
*/
94
- public function doRun (InputInterface $ input , OutputInterface $ output )
94
+ public function doRun (Console \ Input \ InputInterface $ input , Console \ Output \ OutputInterface $ output )
95
95
{
96
96
$ exitCode = parent ::doRun ($ input , $ output );
97
+
97
98
if ($ this ->initException ) {
98
99
$ output ->writeln (
99
100
"<error>We're sorry, an error occurred. Try clearing the cache and code generation directories. "
100
101
. "By default, they are: var/cache, var/di, var/generation, and var/page_cache.</error> "
101
102
);
103
+
102
104
throw $ this ->initException ;
103
105
}
106
+
104
107
return $ exitCode ;
105
108
}
106
109
@@ -113,46 +116,141 @@ protected function getDefaultCommands()
113
116
}
114
117
115
118
/**
116
- * Gets application commands
119
+ * Gets application commands.
117
120
*
118
- * @return array
121
+ * @return array a list of available application commands
119
122
*/
120
123
protected function getApplicationCommands ()
121
124
{
122
125
$ commands = [];
123
126
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
-
133
127
if (class_exists (\Magento \Setup \Console \CommandList::class)) {
134
128
$ setupCommandList = new \Magento \Setup \Console \CommandList ($ this ->serviceManager );
135
129
$ commands = array_merge ($ commands , $ setupCommandList ->getCommands ());
136
130
}
137
131
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);
141
135
$ commands = array_merge ($ commands , $ commandList ->getCommands ());
142
136
}
143
137
144
- $ commands = array_merge ($ commands , $ this ->getVendorCommands ($ objectManager ));
138
+ $ commands = array_merge (
139
+ $ commands ,
140
+ $ this ->getVendorCommands ($ this ->objectManager )
141
+ );
145
142
} catch (\Exception $ e ) {
146
143
$ this ->initException = $ e ;
147
144
}
145
+
148
146
return $ commands ;
149
147
}
150
148
151
149
/**
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.
153
234
*
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
156
254
*/
157
255
protected function getVendorCommands ($ objectManager )
158
256
{
@@ -165,6 +263,7 @@ protected function getVendorCommands($objectManager)
165
263
);
166
264
}
167
265
}
266
+
168
267
return $ commands ;
169
268
}
170
269
}
0 commit comments