Skip to content

Commit 590f356

Browse files
authored
[console] Improve launcher (hechoendrupal#155)
* [console] Remove Launcher classes service definition. * [console] Propagate exit code. * [console] Fix docblocks. * [console] Improve bin/drupal code.
1 parent 3f4d7cc commit 590f356

File tree

5 files changed

+64
-53
lines changed

5 files changed

+64
-53
lines changed

bin/drupal.php

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Drupal\Console\Core\Style\DrupalStyle;
55
use Drupal\Console\Core\Utils\ArgvInputReader;
66
use Drupal\Console\Core\Utils\ConfigurationManager;
7+
use Drupal\Console\Core\Utils\TranslatorManager;
78
use Drupal\Console\Core\Utils\DrupalFinder;
89
use Drupal\Console\Launcher\Application;
910
use Symfony\Component\Console\Input\ArrayInput;
@@ -24,6 +25,16 @@
2425
exit(1);
2526
}
2627

28+
$launcherType = [
29+
'local' => new Drupal\Console\Launcher\Utils\LauncherLocal(),
30+
'ssh' => new Drupal\Console\Launcher\Utils\LauncherSsh(),
31+
'container' => new Drupal\Console\Launcher\Utils\LauncherContainer()
32+
];
33+
34+
$output = new ConsoleOutput();
35+
$input = new ArrayInput([]);
36+
$io = new DrupalStyle($input, $output);
37+
2738
$argvInputReader = new ArgvInputReader();
2839
$target = $argvInputReader->get('target', null);
2940
$root = $argvInputReader->get('root', getcwd());
@@ -34,56 +45,32 @@
3445
$drupalFinder->locateRoot($root);
3546
$composerRoot = $drupalFinder->getComposerRoot();
3647
$drupalRoot = $drupalFinder->getDrupalRoot();
37-
$isValidDrupal = ($composerRoot && $drupalRoot)?true:false;
3848

39-
$drupalConsole = new DrupalConsoleCore($pharRoot, null, $drupalFinder);
40-
$container = $drupalConsole->boot();
41-
42-
/* @var ConfigurationManager $configurationManager */
43-
$configurationManager = $container->get('console.configuration_manager');
49+
$configurationManager = new ConfigurationManager();
50+
$configurationManager->loadConfiguration($drupalFinder->getComposerRoot());
4451
$configuration = $configurationManager->getConfiguration();
45-
$translator = $container->get('console.translator_manager');
4652

4753
if ($options = $configuration->get('application.options') ?: []) {
4854
$argvInputReader->setOptionsFromConfiguration($options);
4955
}
50-
$targetConfig = [];
51-
if ($target = $argvInputReader->get('target')) {
52-
$targetConfig = $container->get('console.configuration_manager')
53-
->readTarget($target);
54-
$argvInputReader->setOptionsFromTargetConfiguration($targetConfig);
55-
}
56-
57-
$argvInputReader->setOptionsAsArgv();
5856

59-
$output = new ConsoleOutput();
60-
$input = new ArrayInput([]);
61-
$io = new DrupalStyle($input, $output);
62-
63-
if ($target = $argvInputReader->get('target')) {
64-
$configurationManager->loadConfiguration($drupalFinder->getComposerRoot());
65-
$configurationManager->getSites();
66-
$options = $configurationManager->readTarget($target);
67-
if ($options) {
68-
if ($options['type'] != 'local') {
69-
$launcherType = 'console.launcher_' . $options['type'];
70-
if ($container->has($launcherType)) {
71-
$launcher = $container->get($launcherType);
72-
$launch = $launcher->launch($options);
73-
exit(0);
74-
}
57+
if ($target) {
58+
if ($targetOptions = $configurationManager->readTarget($target)) {
59+
$argvInputReader->setOptionsFromTargetConfiguration($targetOptions);
60+
$argvInputReader->setOptionsAsArgv();
61+
$type = $targetOptions['type'];
62+
if ($type !== 'local') {
63+
$launcher = $launcherType[$type];
64+
$exitCode = $launcher->launch($targetOptions);
65+
exit($exitCode);
7566
} else {
76-
$root = $options['root'];
77-
$drupalFinder = new DrupalFinder();
67+
$root = $targetOptions['root'];
7868
$drupalFinder->locateRoot($root);
79-
$composerRoot = $drupalFinder->getComposerRoot();
80-
$drupalRoot = $drupalFinder->getDrupalRoot();
81-
$isValidDrupal = ($composerRoot && $drupalRoot)?true:false;
8269
}
8370
}
8471
}
8572

86-
if ($debug || ($isValidDrupal && $command == 'list')) {
73+
if ($debug || ($drupalFinder->isValidDrupal() && $command == 'list')) {
8774
$io->writeln(
8875
sprintf(
8976
'<info>%s</info> version <comment>%s</comment>',
@@ -102,11 +89,16 @@
10289
);
10390
}
10491

105-
if ($isValidDrupal) {
106-
$launcher = $container->get('console.launcher_local');
92+
if ($drupalFinder->isValidDrupal()) {
93+
$launcher = $launcherType['local'];
10794
$exitCode = $launcher->launch($drupalFinder);
108-
10995
if ($exitCode === FALSE) {
96+
$translator = new TranslatorManager();
97+
$translator->loadCoreLanguage(
98+
$configuration->get('application.language'),
99+
$pharRoot
100+
);
101+
110102
$message = sprintf(
111103
$translator->trans('application.site.errors.not-installed'),
112104
PHP_EOL . $drupalFinder->getComposerRoot()
@@ -127,6 +119,10 @@
127119
exit($exitCode);
128120
}
129121

122+
// Restore original argv values
130123
$argvInputReader->restoreOriginalArgvValues();
124+
// Boot Launcher as standalone.
125+
$drupalConsole = new DrupalConsoleCore($pharRoot, null, $drupalFinder);
126+
$container = $drupalConsole->boot();
131127
$application = new Application($container);
132128
$application->run();

services.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
services:
2-
# DrupalConsoleLauncher Services
3-
console.launcher_local:
4-
class: Drupal\Console\Launcher\Utils\LauncherLocal
5-
console.launcher_ssh:
6-
class: Drupal\Console\Launcher\Utils\LauncherSsh
7-
console.launcher_container:
8-
class: Drupal\Console\Launcher\Utils\LauncherContainer
92
# DrupalConsoleLauncher Commands
103
console.self_update:
114
class: Drupal\Console\Launcher\Command\Self\UpdateCommand

src/Utils/LauncherContainer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
*
88
* @package Drupal\Console\Launcher\Utils
99
*/
10+
/**
11+
* Class LauncherContainer
12+
* @package Drupal\Console\Launcher\Utils
13+
*/
1014
class LauncherContainer extends Launcher
1115
{
1216
/**
@@ -31,8 +35,11 @@ public function launch($options)
3135
$pipes
3236
);
3337

34-
proc_close($process);
38+
// If process was successful, we'll return it's exit code to propagate
39+
if ($process) {
40+
return proc_close($process);
41+
}
3542

36-
return true;
43+
return false;
3744
}
3845
}

src/Utils/LauncherLocal.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class LauncherLocal extends Launcher
1313
{
1414
/**
15-
* @param $drupalFinder
15+
* @param $drupalFinder DrupalFinder
1616
*
1717
* @return bool
1818
*/
@@ -34,7 +34,7 @@ public function launch(DrupalFinder $drupalFinder)
3434
$drupalFinder->getComposerRoot()
3535
);
3636

37-
// If process was successful, we'll return it's exitcode to propagate
37+
// If process was successful, we'll return it's exit code to propagate
3838
if ($process) {
3939
return proc_close($process);
4040
}

src/Utils/LauncherSsh.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
*
1313
* @package Drupal\Console\Core\Utils
1414
*/
15+
/**
16+
* Class LauncherSsh
17+
* @package Drupal\Console\Launcher\Utils
18+
*/
1519
class LauncherSsh extends Launcher
1620
{
21+
/**
22+
* @param $options
23+
* @return bool
24+
*/
1725
public function launch($options)
1826
{
1927
$command = sprintf(
@@ -31,11 +39,18 @@ public function launch($options)
3139
$pipes
3240
);
3341

34-
proc_close($process);
42+
// If process was successful, we'll return it's exit code to propagate
43+
if ($process) {
44+
return proc_close($process);
45+
}
3546

36-
return true;
47+
return false;
3748
}
3849

50+
/**
51+
* @param $options
52+
* @return string
53+
*/
3954
private function getSshConnectionString($options)
4055
{
4156
$extraOptions = null;

0 commit comments

Comments
 (0)