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