92b17d58b9c58429967caecc7df8c765ee566c6b
[yaffs-website] / vendor / drupal / console / src / Command / ServerCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\ServerCommand.
6  */
7
8 namespace Drupal\Console\Command;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Input\InputArgument;
13 use Symfony\Component\Process\ProcessBuilder;
14 use Symfony\Component\Process\PhpExecutableFinder;
15 use Drupal\Console\Core\Command\Command;
16 use \Drupal\Console\Core\Utils\ConfigurationManager;
17
18 /**
19  * Class ServerCommand
20  *
21  * @package Drupal\Console\Command
22  */
23 class ServerCommand extends Command
24 {
25     /**
26      * @var string
27      */
28     protected $appRoot;
29
30     /**
31      * @var ConfigurationManager
32      */
33     protected $configurationManager;
34
35     /**
36      * ServerCommand constructor.
37      *
38      * @param $appRoot
39      * @param $configurationManager
40      */
41     public function __construct($appRoot, $configurationManager)
42     {
43         $this->appRoot = $appRoot;
44         $this->configurationManager = $configurationManager;
45
46         parent::__construct();
47     }
48
49     /**
50      * {@inheritdoc}
51      */
52     protected function configure()
53     {
54         $this
55             ->setName('server')
56             ->setDescription($this->trans('commands.server.description'))
57             ->addArgument(
58                 'address',
59                 InputArgument::OPTIONAL,
60                 $this->trans('commands.server.arguments.address'),
61                 '127.0.0.1:8088'
62             )->setAliases(['serve']);
63     }
64
65     /**
66      * {@inheritdoc}
67      */
68     protected function execute(InputInterface $input, OutputInterface $output)
69     {
70         $address = $this->validatePort($input->getArgument('address'));
71
72         $finder = new PhpExecutableFinder();
73         if (false === $binary = $finder->find()) {
74             $this->getIo()->error($this->trans('commands.server.errors.binary'));
75             return 1;
76         }
77
78         $router = $this->configurationManager
79             ->getVendorCoreDirectory() . 'router.php';
80
81         $processBuilder = new ProcessBuilder([$binary, '-S', $address, $router]);
82         $processBuilder->setTimeout(null);
83         $processBuilder->setWorkingDirectory($this->appRoot);
84         $process = $processBuilder->getProcess();
85
86         $this->getIo()->success(
87             sprintf(
88                 $this->trans('commands.server.messages.executing'),
89                 $binary
90             )
91         );
92
93         $this->getIo()->commentBlock(
94             sprintf(
95                 $this->trans('commands.server.messages.listening'),
96                 'http://'.$address
97             )
98         );
99
100         if ($this->getIo()->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
101             $callback = [$this, 'outputCallback'];
102         } else {
103             $callback = null;
104         }
105
106         // Use the process helper to copy process output to console output.
107         $this->getHelper('process')->run($output, $process, null, $callback);
108
109         if (!$process->isSuccessful()) {
110             $this->getIo()->error($process->getErrorOutput());
111             return 1;
112         }
113
114         return 0;
115     }
116
117     /**
118      * @param string $address
119      * @return string
120      */
121     private function validatePort($address)
122     {
123         if (false === strpos($address, ':')) {
124             $host = $address;
125             $port = '8088';
126         } else {
127             $host = explode(':', $address)[0];
128             $port = explode(':', $address)[1];
129         }
130
131         if (fsockopen($host, $port)) {
132             $port = rand(8888, 9999);
133             $address = sprintf(
134                 '%s:%s',
135                 $host,
136                 $port
137             );
138
139             $address = $this->validatePort($address);
140         }
141
142         return $address;
143     }
144
145     public function outputCallback($type, $buffer)
146     {
147         // TODO: seems like $type is Process::ERR always
148         echo $buffer;
149     }
150 }