Skip to content

Commit

Permalink
4330 replace process builder (#4335)
Browse files Browse the repository at this point in the history
* Add new arguments key and target. Make it possible use not only default target for db connections

* Issue #4330: Replace ProcessBuilder class with Process in module install command.

* Issue #4330: Replace ProcessBuilder class with Process in server command.

* Issue #4330: Replace ProcessBuilder class with Process in edit config command.

* Issue #4330: Replace ProcessBuilder class with Process in db query command.

* Issue #4330: Replace ProcessBuilder class with Process in db client command.
  • Loading branch information
LOBsTerr authored Sep 15, 2022
1 parent 42e2dec commit 0ae58aa
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 95 deletions.
61 changes: 37 additions & 24 deletions src/Command/Config/EditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Drupal\Component\Serialization\Yaml;
Expand All @@ -22,6 +22,7 @@

class EditCommand extends Command
{

/**
* @var ConfigFactory
*/
Expand All @@ -40,20 +41,21 @@ class EditCommand extends Command
/**
* EditCommand constructor.
*
* @param ConfigFactory $configFactory
* @param CachedStorage $configStorage
* @param ConfigurationManager $configurationManager
* @param ConfigFactory $configFactory
* @param CachedStorage $configStorage
* @param ConfigurationManager $configurationManager
*/
public function __construct(
ConfigFactory $configFactory,
CachedStorage $configStorage,
ConfigurationManager $configurationManager
) {
$this->configFactory = $configFactory;
$this->configStorage = $configStorage;
$this->configFactory = $configFactory;
$this->configStorage = $configStorage;
$this->configurationManager = $configurationManager;
parent::__construct();
}

/**
* {@inheritdoc}
*/
Expand All @@ -80,34 +82,42 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$configName = $input->getArgument('config-name');
$editor = $input->getArgument('editor');
$config = $this->configFactory->getEditable($configName);
$configSystem = $this->configFactory->get('system.file');
$configName = $input->getArgument('config-name');
$editor = $input->getArgument('editor');
$config = $this->configFactory->getEditable($configName);
$configSystem = $this->configFactory->get('system.file');
$temporaryDirectory = $configSystem->get('path.temporary') ?: '/tmp';
$configFile = $temporaryDirectory.'/config-edit/'.$configName.'.yml';
$ymlFile = new Parser();
$fileSystem = new Filesystem();
$configFile = $temporaryDirectory.'/config-edit/'.$configName
.'.yml';
$ymlFile = new Parser();
$fileSystem = new Filesystem();

if (!$configName) {
$this->getIo()->error($this->trans('commands.config.edit.messages.no-config'));
$this->getIo()->error(
$this->trans('commands.config.edit.messages.no-config')
);

return 1;
}

try {
$fileSystem->mkdir($temporaryDirectory);
$fileSystem->dumpFile($configFile, $this->getYamlConfig($configName));
$fileSystem->dumpFile(
$configFile,
$this->getYamlConfig($configName)
);
} catch (IOExceptionInterface $e) {
$this->getIo()->error($this->trans('commands.config.edit.messages.no-directory').' '.$e->getPath());
$this->getIo()->error(
$this->trans('commands.config.edit.messages.no-directory').' '
.$e->getPath()
);

return 1;
}
if (!$editor) {
$editor = $this->getEditor();
}
$processBuilder = new ProcessBuilder([$editor, $configFile]);
$process = $processBuilder->getProcess();
$process = new Process([$editor, $configFile]);
$process->setTty('true');
$process->run();

Expand All @@ -120,6 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

if (!$process->isSuccessful()) {
$this->getIo()->error($process->getErrorOutput());

return 1;
}

Expand All @@ -131,8 +142,10 @@ protected function interact(InputInterface $input, OutputInterface $output)
$configName = $input->getArgument('config-name');
if (!$configName) {
$configNames = $this->configFactory->listAll();
$configName = $this->getIo()->choice(
$this->trans('commands.config.edit.messages.choose-configuration'),
$configName = $this->getIo()->choice(
$this->trans(
'commands.config.edit.messages.choose-configuration'
),
$configNames
);

Expand All @@ -148,7 +161,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
protected function getYamlConfig($config_name)
{
if ($this->configStorage->exists($config_name)) {
$configuration = $this->configStorage->read($config_name);
$configuration = $this->configStorage->read($config_name);
$configurationEncoded = Yaml::encode($configuration);
}

Expand All @@ -167,13 +180,13 @@ protected function getEditor()
return trim($editor);
}

$processBuilder = new ProcessBuilder(['bash']);
$process = $processBuilder->getProcess();
$process->setCommandLine('echo ${EDITOR:-${VISUAL:-vi}}');
$process = new Process(['bash']);
$process->setCommandLine('echo ${EDITOR:-${VISUAL:-nano}}');
$process->run();
$editor = $process->getOutput();
$process->stop();

return trim($editor);
}

}
20 changes: 12 additions & 8 deletions src/Command/Database/ClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\Process;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Command\Shared\ConnectTrait;

class ClientCommand extends Command
{

use ConnectTrait;

/**
Expand All @@ -25,7 +26,9 @@ protected function configure()
{
$this
->setName('database:client')
->setDescription($this->trans('commands.database.client.description'))
->setDescription(
$this->trans('commands.database.client.description')
)
->addArgument(
'database',
InputArgument::OPTIONAL,
Expand All @@ -49,23 +52,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$database = $input->getArgument('database');
$learning = $input->getOption('learning');
$target = $input->getArgument('target');
$target = $input->getArgument('target');

$databaseConnection = $this->resolveConnection($database, $target);
$connection = $this->getConnectionString($databaseConnection);
$connection = $this->getConnectionString($databaseConnection);

if ($learning) {
$this->getIo()->commentBlock(
sprintf(
$this->trans('commands.database.client.messages.connection'),
$this->trans(
'commands.database.client.messages.connection'
),
$connection
)
);
}

$processBuilder = new ProcessBuilder([]);
$processBuilder->setArguments(explode(' ', $connection));
$process = $processBuilder->getProcess();
$process = new Process(explode(' ', $connection));
$process->setTty('true');
$process->run();

Expand All @@ -75,4 +78,5 @@ protected function execute(InputInterface $input, OutputInterface $output)

return 0;
}

}
74 changes: 55 additions & 19 deletions src/Command/Database/QueryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\Process;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Command\Shared\ConnectTrait;

class QueryCommand extends Command
{

use ConnectTrait;

/**
Expand All @@ -31,7 +32,9 @@ protected function configure()
{
$this
->setName('database:query')
->setDescription($this->trans('commands.database.query.description'))
->setDescription(
$this->trans('commands.database.query.description')
)
->addArgument(
'query',
InputArgument::REQUIRED,
Expand All @@ -49,14 +52,48 @@ protected function configure()
$this->trans('commands.database.connect.arguments.target'),
'default'
)
->addOption('quick', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.quick'))
->addOption('debug', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.debug'))
->addOption('html', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.html'))
->addOption('xml', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.xml'))
->addOption('raw', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.raw'))
->addOption('vertical', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.vertical'))
->addOption('batch', null, InputOption::VALUE_NONE, $this->trans('commands.database.query.options.batch'))

->addOption(
'quick',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.query.options.quick')
)
->addOption(
'debug',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.query.options.debug')
)
->addOption(
'html',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.query.options.html')
)
->addOption(
'xml',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.query.options.xml')
)
->addOption(
'raw',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.query.options.raw')
)
->addOption(
'vertical',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.query.options.vertical')
)
->addOption(
'batch',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.query.options.batch')
)
->setHelp($this->trans('commands.database.query.help'))
->setAliases(['dbq']);
}
Expand All @@ -66,9 +103,9 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$query = $input->getArgument('query');
$query = $input->getArgument('query');
$database = $input->getArgument('database');
$target = $input->getArgument('target');
$target = $input->getArgument('target');
$learning = $input->getOption('learning');

$databaseConnection = $this->resolveConnection($database, $target);
Expand All @@ -83,14 +120,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$databaseConnection['port']
);

$args = explode(' ', $connection);
$args = explode(' ', $connection);
$args[] = sprintf('--execute=%s', $query);

$opts = ['quick', 'debug', 'html', 'xml', 'raw', 'vertical', 'batch'];
array_walk(
$opts, function ($opt) use ($input, &$args) {
if ($input->getOption($opt)) {
switch ($opt) {
if ($input->getOption($opt)) {
switch ($opt) {
case 'quick':
$args[] = '--quick';
break;
Expand All @@ -112,9 +149,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
case 'batch':
$args[] = '--batch';
break;
}
}
}
}
);

if ($learning) {
Expand All @@ -123,9 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
}

$processBuilder = new ProcessBuilder();
$processBuilder->setArguments($args);
$process = $processBuilder->getProcess();
$process = new Process($args);
$process->setTty('true');
$process->run();

Expand All @@ -135,4 +170,5 @@ protected function execute(InputInterface $input, OutputInterface $output)

return 0;
}

}
Loading

0 comments on commit 0ae58aa

Please sign in to comment.