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