Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / Command / Exec / ExecCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Exec\ExecCommand.
6  */
7
8 namespace Drupal\Console\Core\Command\Exec;
9
10 use Symfony\Component\Console\Command\Command;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Process\ExecutableFinder;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Utils\ShellProcess;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 /**
21  * Class ExecCommand
22  *
23  * @package Drupal\Console\Core\Command\Exec
24  */
25 class ExecCommand extends Command
26 {
27     use CommandTrait;
28
29     /**
30      * @var ShellProcess
31      */
32     protected $shellProcess;
33
34     /**
35      * ExecCommand constructor.
36      *
37      * @param ShellProcess $shellProcess
38      */
39     public function __construct(ShellProcess $shellProcess)
40     {
41         $this->shellProcess = $shellProcess;
42         parent::__construct();
43     }
44
45     /**
46      * {@inheritdoc}
47      */
48     protected function configure()
49     {
50         $this
51             ->setName('exec')
52             ->setDescription($this->trans('commands.exec.description'))
53             ->addArgument(
54                 'bin',
55                 InputArgument::REQUIRED,
56                 $this->trans('commands.exec.arguments.bin')
57             )->addOption(
58                 'working-directory',
59                 null,
60                 InputOption::VALUE_OPTIONAL,
61                 $this->trans('commands.exec.options.working-directory')
62             );
63     }
64
65     /**
66      * {@inheritdoc}
67      */
68     protected function execute(InputInterface $input, OutputInterface $output)
69     {
70         $io = new DrupalStyle($input, $output);
71         $bin = $input->getArgument('bin');
72         $workingDirectory = $input->getOption('working-directory');
73
74         if (!$bin) {
75             $io->error(
76                 $this->trans('commands.exec.messages.missing-bin')
77             );
78
79             return 1;
80         }
81
82         $name = $bin;
83         if ($index = stripos($name, " ")) {
84             $name = substr($name, 0, $index);
85         }
86
87         $finder = new ExecutableFinder();
88         if (!$finder->find($name)) {
89             $io->error(
90                 sprintf(
91                     $this->trans('commands.exec.messages.binary-not-found'),
92                     $name
93                 )
94             );
95
96             return 1;
97         }
98
99         if (!$this->shellProcess->exec($bin, $workingDirectory)) {
100             $io->error(
101                 sprintf(
102                     $this->trans('commands.exec.messages.invalid-bin')
103                 )
104             );
105
106             $io->writeln($this->shellProcess->getOutput());
107
108             return 1;
109         }
110
111         $io->success(
112             sprintf(
113                 $this->trans('commands.exec.messages.success'),
114                 $bin
115             )
116         );
117
118         return 0;
119     }
120 }