4 * Contains \Drush\Psysh\DrushCommand.
6 * DrushCommand is a PsySH proxy command which accepts a Drush command config
7 * array and tries to build an appropriate PsySH command for it.
10 namespace Drush\Psysh;
12 use Consolidation\AnnotatedCommand\AnnotatedCommand;
13 use Psy\Command\Command as BaseCommand;
14 use Symfony\Component\Console\Formatter\OutputFormatter;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
21 class DrushCommand extends BaseCommand
25 * @var \Consolidation\AnnotatedCommand\AnnotatedCommand
30 * DrushCommand constructor.
32 * @param \Consolidation\AnnotatedCommand\AnnotatedCommand $command
33 * Original (annotated) Drush command.
35 public function __construct(AnnotatedCommand $command)
37 $this->command = $command;
38 parent::__construct();
42 * Get the namespace of this command.
44 public function getNamespace()
46 $parts = explode('-', $this->getName());
47 return count($parts) >= 2 ? array_shift($parts) : 'global';
53 protected function configure()
56 ->setName($this->command->getName())
57 ->setAliases($this->command->getAliases())
58 ->setDefinition($this->command->getDefinition())
59 ->setDescription($this->command->getDescription())
60 ->setHelp($this->buildHelpFromCommand());
66 protected function execute(InputInterface $input, OutputInterface $output)
68 $args = $input->getArguments();
69 $first = array_shift($args);
71 // If the first argument is an alias, assign the next argument as the
73 if (strpos($first, '@') === 0) {
75 $command = array_shift($args);
76 } // Otherwise, default the alias to '@self' and use the first argument as the
83 $options = $input->getOptions();
84 // Force the 'backend' option to TRUE.
85 $options['backend'] = true;
87 $return = drush_invoke_process($alias, $command, array_values($args), $options, ['interactive' => true]);
89 if ($return['error_status'] > 0) {
90 foreach ($return['error_log'] as $error_type => $errors) {
91 $output->write($errors);
93 // Add a newline after so the shell returns on a new line.
96 $output->page(drush_backend_get_result());
101 * Build a command help from the Drush configuration array.
103 * Currently it's a word-wrapped description, plus any examples provided.
108 protected function buildHelpFromCommand()
110 $help = wordwrap($this->command->getDescription());
113 foreach ($this->command->getExampleUsages() as $ex => $def) {
114 // Skip empty examples and things with obvious pipes...
115 if (($ex === '') || (strpos($ex, '|') !== false)) {
119 $ex = preg_replace('/^drush\s+/', '', $ex);
120 $examples[$ex] = $def;
123 if (!empty($examples)) {
126 foreach ($examples as $ex => $def) {
127 $help .= sprintf("\n<return>// %s</return>\n", wordwrap(OutputFormatter::escape($def), 75, "</return>\n<return>// "));
128 $help .= sprintf("<return>>>> %s</return>\n", OutputFormatter::escape($ex));