1c54ef49336425f5e84f070bd3f338fb6638d4d7
[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  * @package Drupal\Console\Core\Command\Exec
23  */
24 class ExecCommand extends Command
25 {
26     use CommandTrait;
27
28     /**
29      * @var ShellProcess
30      */
31     protected $shellProcess;
32
33     /**
34      * ExecCommand constructor.
35      * @param ShellProcess $shellProcess
36      */
37     public function __construct(ShellProcess $shellProcess)
38     {
39         $this->shellProcess = $shellProcess;
40         parent::__construct();
41     }
42
43     /**
44      * {@inheritdoc}
45      */
46     protected function configure()
47     {
48         $this
49             ->setName('exec')
50             ->setDescription($this->trans('commands.exec.description'))
51             ->addArgument(
52                 'bin',
53                 InputArgument::REQUIRED,
54                 $this->trans('commands.exec.arguments.bin')
55             )->addOption(
56                 'working-directory',
57                 null,
58                 InputOption::VALUE_OPTIONAL,
59                 $this->trans('commands.exec.options.working-directory')
60             );
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     protected function execute(InputInterface $input, OutputInterface $output)
67     {
68         $io = new DrupalStyle($input, $output);
69         $bin = $input->getArgument('bin');
70         $workingDirectory = $input->getOption('working-directory');
71
72         if (!$bin) {
73             $io->error(
74                 $this->trans('commands.exec.messages.missing-bin')
75             );
76
77             return 1;
78         }
79
80         $name = $bin;
81         if ($index = stripos($name, " ")) {
82             $name = substr($name, 0, $index);
83         }
84
85         $finder = new ExecutableFinder();
86         if (!$finder->find($name)) {
87             $io->error(
88                 sprintf(
89                     $this->trans('commands.exec.messages.binary-not-found'),
90                     $name
91                 )
92             );
93
94             return 1;
95         }
96
97         if (!$this->shellProcess->exec($bin, $workingDirectory)) {
98             $io->error(
99                 sprintf(
100                     $this->trans('commands.exec.messages.invalid-bin')
101                 )
102             );
103
104             $io->writeln($this->shellProcess->getOutput());
105
106             return 1;
107         }
108
109         $io->success(
110             sprintf(
111                 $this->trans('commands.exec.messages.success'),
112                 $bin
113             )
114         );
115
116         return 0;
117     }
118 }