4172f5d9cd826ec32c8a909ef9123c7779b658b1
[yaffs-website] / vendor / drush / drush / src / Psysh / DrushCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drush\Psysh\DrushCommand.
5  *
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.
8  */
9
10 namespace Drush\Psysh;
11
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;
17
18 /**
19  * Main Drush command.
20  */
21 class DrushCommand extends BaseCommand
22 {
23
24     /**
25      * @var \Consolidation\AnnotatedCommand\AnnotatedCommand
26      */
27     private $command;
28
29     /**
30      * DrushCommand constructor.
31      *
32      * @param \Consolidation\AnnotatedCommand\AnnotatedCommand $command
33      *   Original (annotated) Drush command.
34      */
35     public function __construct(AnnotatedCommand $command)
36     {
37         $this->command = $command;
38         parent::__construct();
39     }
40
41     /**
42      * Get the namespace of this command.
43      */
44     public function getNamespace()
45     {
46         $parts = explode('-', $this->getName());
47         return count($parts) >= 2 ? array_shift($parts) : 'global';
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     protected function configure()
54     {
55         $this
56         ->setName($this->command->getName())
57         ->setAliases($this->command->getAliases())
58         ->setDefinition($this->command->getDefinition())
59         ->setDescription($this->command->getDescription())
60         ->setHelp($this->buildHelpFromCommand());
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     protected function execute(InputInterface $input, OutputInterface $output)
67     {
68         $args = $input->getArguments();
69         $first = array_shift($args);
70
71         // If the first argument is an alias, assign the next argument as the
72         // command.
73         if (strpos($first, '@') === 0) {
74             $alias = $first;
75             $command = array_shift($args);
76         } // Otherwise, default the alias to '@self' and use the first argument as the
77         // command.
78         else {
79             $alias = '@self';
80             $command = $first;
81         }
82
83         $options = $input->getOptions();
84         // Force the 'backend' option to TRUE.
85         $options['backend'] = true;
86
87         $return = drush_invoke_process($alias, $command, array_values($args), $options, ['interactive' => true]);
88
89         if ($return['error_status'] > 0) {
90             foreach ($return['error_log'] as $error_type => $errors) {
91                 $output->write($errors);
92             }
93             // Add a newline after so the shell returns on a new line.
94             $output->writeln('');
95         } else {
96             $output->page(drush_backend_get_result());
97         }
98     }
99
100     /**
101      * Build a command help from the Drush configuration array.
102      *
103      * Currently it's a word-wrapped description, plus any examples provided.
104      *
105      * @return string
106      *   The help string.
107      */
108     protected function buildHelpFromCommand()
109     {
110         $help = wordwrap($this->command->getDescription());
111
112         $examples = [];
113         foreach ($this->command->getExampleUsages() as $ex => $def) {
114             // Skip empty examples and things with obvious pipes...
115             if (($ex === '') || (strpos($ex, '|') !== false)) {
116                 continue;
117             }
118
119             $ex = preg_replace('/^drush\s+/', '', $ex);
120             $examples[$ex] = $def;
121         }
122
123         if (!empty($examples)) {
124             $help .= "\n\ne.g.";
125
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));
129             }
130         }
131
132         return $help;
133     }
134 }