Version 1
[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 Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18
19 /**
20  * Class ServerCommand
21  *
22  * @package Drupal\Console\Command
23  */
24 class ServerCommand extends Command
25 {
26     use CommandTrait;
27
28     protected $appRoot;
29
30     protected $configurationManager;
31
32     /**
33      * ServerCommand constructor.
34      *
35      * @param $appRoot
36      * @param $configurationManager
37      */
38     public function __construct($appRoot, $configurationManager)
39     {
40         $this->appRoot = $appRoot;
41         $this->configurationManager = $configurationManager;
42
43         parent::__construct();
44     }
45
46     /**
47      * {@inheritdoc}
48      */
49     protected function configure()
50     {
51         $this
52             ->setName('server')
53             ->setDescription($this->trans('commands.server.description'))
54             ->addArgument(
55                 'address',
56                 InputArgument::OPTIONAL,
57                 $this->trans('commands.server.arguments.address'),
58                 '127.0.0.1:8088'
59             );
60     }
61
62     /**
63      * {@inheritdoc}
64      */
65     protected function execute(InputInterface $input, OutputInterface $output)
66     {
67         $io = new DrupalStyle($input, $output);
68         $learning = $input->hasOption('learning')?$input->getOption('learning'):false;
69         $address = $this->validatePort($input->getArgument('address'));
70
71         $finder = new PhpExecutableFinder();
72         if (false === $binary = $finder->find()) {
73             $io->error($this->trans('commands.server.errors.binary'));
74             return;
75         }
76
77         $router = $this->getRouterPath();
78         $processBuilder = new ProcessBuilder([$binary, '-S', $address, $router]);
79         $processBuilder->setTimeout(null);
80         $processBuilder->setWorkingDirectory($this->appRoot);
81         $process = $processBuilder->getProcess();
82
83         $io->success(
84             sprintf(
85                 $this->trans('commands.server.messages.executing'),
86                 $binary
87             )
88         );
89
90         $io->commentBlock(
91             sprintf(
92                 $this->trans('commands.server.messages.listening'),
93                 $address
94             )
95         );
96
97         // Use the process helper to copy process output to console output.
98         $this->getHelper('process')->run($output, $process, null, null);
99
100         if (!$process->isSuccessful()) {
101             $io->error($process->getErrorOutput());
102         }
103     }
104
105     /**
106      * @return null|string
107      */
108     private function getRouterPath()
109     {
110         $router = sprintf(
111             '%s/.console/router.php',
112             $this->configurationManager->getHomeDirectory()
113         );
114
115         if (file_exists($router)) {
116             return $router;
117         }
118
119         $router = sprintf(
120             '%s/config/dist/router.php',
121             $this->configurationManager->getApplicationDirectory()
122         );
123
124         if (file_exists($router)) {
125             return $router;
126         }
127
128         return null;
129     }
130
131     /**
132      * @param string $address
133      * @return string
134      */
135     private function validatePort($address)
136     {
137         if (false === strpos($address, ':')) {
138             $host = $address;
139             $port = '8088';
140         } else {
141             $host = explode(':', $address)[0];
142             $port = explode(':', $address)[1];
143         }
144
145         if (fsockopen($host, $port)) {
146             $port = rand(8888, 9999);
147             $address = sprintf(
148                 '%s:%s',
149                 $host,
150                 $port
151             );
152
153             $address = $this->validatePort($address);
154         }
155
156         return $address;
157     }
158 }