1111use Magento \Framework \App \ProductMetadata ;
1212use Magento \Framework \App \State ;
1313use Magento \Framework \Composer \ComposerJsonFinder ;
14- use Magento \Framework \Exception \FileSystemException ;
14+ use Magento \Framework \Console \ Exception \GenerationDirectoryAccessException ;
1515use Magento \Framework \Filesystem \Driver \File ;
1616use Magento \Framework \ObjectManagerInterface ;
1717use Magento \Framework \Shell \ComplexParameter ;
@@ -66,17 +66,27 @@ class Cli extends Console\Application
6666 /**
6767 * @param string $name the application name
6868 * @param string $version the application version
69+ * @SuppressWarnings(PHPMD.ExitExpression)
6970 */
7071 public function __construct ($ name = 'UNKNOWN ' , $ version = 'UNKNOWN ' )
7172 {
72- $ configuration = require BP . '/setup/config/application.config.php ' ;
73- $ bootstrapApplication = new Application ();
74- $ application = $ bootstrapApplication ->bootstrap ($ configuration );
75- $ this ->serviceManager = $ application ->getServiceManager ();
73+ try {
74+ $ configuration = require BP . '/setup/config/application.config.php ' ;
75+ $ bootstrapApplication = new Application ();
76+ $ application = $ bootstrapApplication ->bootstrap ($ configuration );
77+ $ this ->serviceManager = $ application ->getServiceManager ();
78+
79+ $ this ->assertCompilerPreparation ();
80+ $ this ->initObjectManager ();
81+ $ this ->assertGenerationPermissions ();
82+ } catch (\Exception $ exception ) {
83+ $ output = new \Symfony \Component \Console \Output \ConsoleOutput ();
84+ $ output ->writeln (
85+ '<error> ' . $ exception ->getMessage () . '</error> '
86+ );
7687
77- $ this ->assertCompilerPreparation ();
78- $ this ->initObjectManager ();
79- $ this ->assertGenerationPermissions ();
88+ exit (static ::RETURN_FAILURE );
89+ }
8090
8191 if ($ version == 'UNKNOWN ' ) {
8292 $ directoryList = new DirectoryList (BP );
@@ -91,20 +101,13 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
91101 /**
92102 * {@inheritdoc}
93103 *
94- * @throws \Exception the exception in case of unexpected error
104+ * @throws \Exception The exception in case of unexpected error
95105 */
96106 public function doRun (Console \Input \InputInterface $ input , Console \Output \OutputInterface $ output )
97107 {
98108 $ exitCode = parent ::doRun ($ input , $ output );
99109
100110 if ($ this ->initException ) {
101- $ output ->writeln (
102- "<error>We're sorry, an error occurred. Try clearing the cache and code generation directories. "
103- . "By default, they are: " . $ this ->getDefaultDirectoryPath (DirectoryList::CACHE ) . ", "
104- . $ this ->getDefaultDirectoryPath (DirectoryList::GENERATED_METADATA ) . ", "
105- . $ this ->getDefaultDirectoryPath (DirectoryList::GENERATED_CODE ) . ", and var/page_cache.</error> "
106- );
107-
108111 throw $ this ->initException ;
109112 }
110113
@@ -154,24 +157,17 @@ protected function getApplicationCommands()
154157 * Object Manager initialization.
155158 *
156159 * @return void
157- * @SuppressWarnings(PHPMD.ExitExpression)
158160 */
159161 private function initObjectManager ()
160162 {
161- try {
162- $ params = (new ComplexParameter (self ::INPUT_KEY_BOOTSTRAP ))->mergeFromArgv ($ _SERVER , $ _SERVER );
163- $ params [Bootstrap::PARAM_REQUIRE_MAINTENANCE ] = null ;
164-
165- $ this ->objectManager = Bootstrap::create (BP , $ params )->getObjectManager ();
163+ $ params = (new ComplexParameter (self ::INPUT_KEY_BOOTSTRAP ))->mergeFromArgv ($ _SERVER , $ _SERVER );
164+ $ params [Bootstrap::PARAM_REQUIRE_MAINTENANCE ] = null ;
166165
167- /** @var ObjectManagerProvider $omProvider */
168- $ omProvider = $ this ->serviceManager ->get (ObjectManagerProvider::class);
169- $ omProvider ->setObjectManager ($ this ->objectManager );
170- } catch (FileSystemException $ exception ) {
171- $ this ->writeGenerationDirectoryReadError ();
166+ $ this ->objectManager = Bootstrap::create (BP , $ params )->getObjectManager ();
172167
173- exit (static ::RETURN_FAILURE );
174- }
168+ /** @var ObjectManagerProvider $omProvider */
169+ $ omProvider = $ this ->serviceManager ->get (ObjectManagerProvider::class);
170+ $ omProvider ->setObjectManager ($ this ->objectManager );
175171 }
176172
177173 /**
@@ -182,7 +178,7 @@ private function initObjectManager()
182178 * developer - application will be terminated
183179 *
184180 * @return void
185- * @SuppressWarnings(PHPMD.ExitExpression)
181+ * @throws GenerationDirectoryAccessException If generation directory is read-only in developer mode
186182 */
187183 private function assertGenerationPermissions ()
188184 {
@@ -197,17 +193,15 @@ private function assertGenerationPermissions()
197193 if ($ state ->getMode () !== State::MODE_PRODUCTION
198194 && !$ generationDirectoryAccess ->check ()
199195 ) {
200- $ this ->writeGenerationDirectoryReadError ();
201-
202- exit (static ::RETURN_FAILURE );
196+ throw new GenerationDirectoryAccessException ();
203197 }
204198 }
205199
206200 /**
207201 * Checks whether compiler is being prepared.
208202 *
209203 * @return void
210- * @SuppressWarnings(PHPMD.ExitExpression)
204+ * @throws GenerationDirectoryAccessException If generation directory is read-only
211205 */
212206 private function assertCompilerPreparation ()
213207 {
@@ -222,33 +216,10 @@ private function assertCompilerPreparation()
222216 new File ()
223217 );
224218
225- try {
226- $ compilerPreparation ->handleCompilerEnvironment ();
227- } catch (FileSystemException $ e ) {
228- $ this ->writeGenerationDirectoryReadError ();
229-
230- exit (static ::RETURN_FAILURE );
231- }
219+ $ compilerPreparation ->handleCompilerEnvironment ();
232220 }
233221 }
234222
235- /**
236- * Writes read error to console.
237- *
238- * @return void
239- */
240- private function writeGenerationDirectoryReadError ()
241- {
242- $ output = new \Symfony \Component \Console \Output \ConsoleOutput ();
243- $ output ->writeln (
244- '<error> '
245- . 'Command line user does not have read and write permissions on '
246- . $ this ->getDefaultDirectoryPath (DirectoryList::GENERATED_CODE ) . ' directory. '
247- . 'Please address this issue before using Magento command line. '
248- . '</error> '
249- );
250- }
251-
252223 /**
253224 * Retrieves vendor commands.
254225 *
@@ -270,22 +241,4 @@ protected function getVendorCommands($objectManager)
270241
271242 return $ commands ;
272243 }
273-
274- /**
275- * Get default directory path by code
276- *
277- * @param string $code
278- * @return string
279- */
280- private function getDefaultDirectoryPath ($ code )
281- {
282- $ config = DirectoryList::getDefaultConfig ();
283- $ result = '' ;
284-
285- if (isset ($ config [$ code ][DirectoryList::PATH ])) {
286- $ result = $ config [$ code ][DirectoryList::PATH ];
287- }
288-
289- return $ result ;
290- }
291244}
0 commit comments