Skip to content

Commit

Permalink
[console] Improve launcher (#155)
Browse files Browse the repository at this point in the history
* [console] Remove Launcher classes service definition.

* [console] Propagate exit code.

* [console] Fix docblocks.

* [console] Improve bin/drupal code.
  • Loading branch information
jmolivas authored Feb 18, 2018
1 parent 3f4d7cc commit 590f356
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 53 deletions.
76 changes: 36 additions & 40 deletions bin/drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Core\Utils\ArgvInputReader;
use Drupal\Console\Core\Utils\ConfigurationManager;
use Drupal\Console\Core\Utils\TranslatorManager;
use Drupal\Console\Core\Utils\DrupalFinder;
use Drupal\Console\Launcher\Application;
use Symfony\Component\Console\Input\ArrayInput;
Expand All @@ -24,6 +25,16 @@
exit(1);
}

$launcherType = [
'local' => new Drupal\Console\Launcher\Utils\LauncherLocal(),
'ssh' => new Drupal\Console\Launcher\Utils\LauncherSsh(),
'container' => new Drupal\Console\Launcher\Utils\LauncherContainer()
];

$output = new ConsoleOutput();
$input = new ArrayInput([]);
$io = new DrupalStyle($input, $output);

$argvInputReader = new ArgvInputReader();
$target = $argvInputReader->get('target', null);
$root = $argvInputReader->get('root', getcwd());
Expand All @@ -34,56 +45,32 @@
$drupalFinder->locateRoot($root);
$composerRoot = $drupalFinder->getComposerRoot();
$drupalRoot = $drupalFinder->getDrupalRoot();
$isValidDrupal = ($composerRoot && $drupalRoot)?true:false;

$drupalConsole = new DrupalConsoleCore($pharRoot, null, $drupalFinder);
$container = $drupalConsole->boot();

/* @var ConfigurationManager $configurationManager */
$configurationManager = $container->get('console.configuration_manager');
$configurationManager = new ConfigurationManager();
$configurationManager->loadConfiguration($drupalFinder->getComposerRoot());
$configuration = $configurationManager->getConfiguration();
$translator = $container->get('console.translator_manager');

if ($options = $configuration->get('application.options') ?: []) {
$argvInputReader->setOptionsFromConfiguration($options);
}
$targetConfig = [];
if ($target = $argvInputReader->get('target')) {
$targetConfig = $container->get('console.configuration_manager')
->readTarget($target);
$argvInputReader->setOptionsFromTargetConfiguration($targetConfig);
}

$argvInputReader->setOptionsAsArgv();

$output = new ConsoleOutput();
$input = new ArrayInput([]);
$io = new DrupalStyle($input, $output);

if ($target = $argvInputReader->get('target')) {
$configurationManager->loadConfiguration($drupalFinder->getComposerRoot());
$configurationManager->getSites();
$options = $configurationManager->readTarget($target);
if ($options) {
if ($options['type'] != 'local') {
$launcherType = 'console.launcher_' . $options['type'];
if ($container->has($launcherType)) {
$launcher = $container->get($launcherType);
$launch = $launcher->launch($options);
exit(0);
}
if ($target) {
if ($targetOptions = $configurationManager->readTarget($target)) {
$argvInputReader->setOptionsFromTargetConfiguration($targetOptions);
$argvInputReader->setOptionsAsArgv();
$type = $targetOptions['type'];
if ($type !== 'local') {
$launcher = $launcherType[$type];
$exitCode = $launcher->launch($targetOptions);
exit($exitCode);
} else {
$root = $options['root'];
$drupalFinder = new DrupalFinder();
$root = $targetOptions['root'];
$drupalFinder->locateRoot($root);
$composerRoot = $drupalFinder->getComposerRoot();
$drupalRoot = $drupalFinder->getDrupalRoot();
$isValidDrupal = ($composerRoot && $drupalRoot)?true:false;
}
}
}

if ($debug || ($isValidDrupal && $command == 'list')) {
if ($debug || ($drupalFinder->isValidDrupal() && $command == 'list')) {
$io->writeln(
sprintf(
'<info>%s</info> version <comment>%s</comment>',
Expand All @@ -102,11 +89,16 @@
);
}

if ($isValidDrupal) {
$launcher = $container->get('console.launcher_local');
if ($drupalFinder->isValidDrupal()) {
$launcher = $launcherType['local'];
$exitCode = $launcher->launch($drupalFinder);

if ($exitCode === FALSE) {
$translator = new TranslatorManager();
$translator->loadCoreLanguage(
$configuration->get('application.language'),
$pharRoot
);

$message = sprintf(
$translator->trans('application.site.errors.not-installed'),
PHP_EOL . $drupalFinder->getComposerRoot()
Expand All @@ -127,6 +119,10 @@
exit($exitCode);
}

// Restore original argv values
$argvInputReader->restoreOriginalArgvValues();
// Boot Launcher as standalone.
$drupalConsole = new DrupalConsoleCore($pharRoot, null, $drupalFinder);
$container = $drupalConsole->boot();
$application = new Application($container);
$application->run();
7 changes: 0 additions & 7 deletions services.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
services:
# DrupalConsoleLauncher Services
console.launcher_local:
class: Drupal\Console\Launcher\Utils\LauncherLocal
console.launcher_ssh:
class: Drupal\Console\Launcher\Utils\LauncherSsh
console.launcher_container:
class: Drupal\Console\Launcher\Utils\LauncherContainer
# DrupalConsoleLauncher Commands
console.self_update:
class: Drupal\Console\Launcher\Command\Self\UpdateCommand
Expand Down
11 changes: 9 additions & 2 deletions src/Utils/LauncherContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*
* @package Drupal\Console\Launcher\Utils
*/
/**
* Class LauncherContainer
* @package Drupal\Console\Launcher\Utils
*/
class LauncherContainer extends Launcher
{
/**
Expand All @@ -31,8 +35,11 @@ public function launch($options)
$pipes
);

proc_close($process);
// If process was successful, we'll return it's exit code to propagate
if ($process) {
return proc_close($process);
}

return true;
return false;
}
}
4 changes: 2 additions & 2 deletions src/Utils/LauncherLocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class LauncherLocal extends Launcher
{
/**
* @param $drupalFinder
* @param $drupalFinder DrupalFinder
*
* @return bool
*/
Expand All @@ -34,7 +34,7 @@ public function launch(DrupalFinder $drupalFinder)
$drupalFinder->getComposerRoot()
);

// If process was successful, we'll return it's exitcode to propagate
// If process was successful, we'll return it's exit code to propagate
if ($process) {
return proc_close($process);
}
Expand Down
19 changes: 17 additions & 2 deletions src/Utils/LauncherSsh.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@
*
* @package Drupal\Console\Core\Utils
*/
/**
* Class LauncherSsh
* @package Drupal\Console\Launcher\Utils
*/
class LauncherSsh extends Launcher
{
/**
* @param $options
* @return bool
*/
public function launch($options)
{
$command = sprintf(
Expand All @@ -31,11 +39,18 @@ public function launch($options)
$pipes
);

proc_close($process);
// If process was successful, we'll return it's exit code to propagate
if ($process) {
return proc_close($process);
}

return true;
return false;
}

/**
* @param $options
* @return string
*/
private function getSshConnectionString($options)
{
$extraOptions = null;
Expand Down

0 comments on commit 590f356

Please sign in to comment.