5 * Contains \Drupal\Console\Command\ServerCommand.
8 namespace Drupal\Console\Command;
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;
22 * @package Drupal\Console\Command
24 class ServerCommand extends Command
30 protected $configurationManager;
33 * ServerCommand constructor.
36 * @param $configurationManager
38 public function __construct($appRoot, $configurationManager)
40 $this->appRoot = $appRoot;
41 $this->configurationManager = $configurationManager;
43 parent::__construct();
49 protected function configure()
53 ->setDescription($this->trans('commands.server.description'))
56 InputArgument::OPTIONAL,
57 $this->trans('commands.server.arguments.address'),
65 protected function execute(InputInterface $input, OutputInterface $output)
67 $io = new DrupalStyle($input, $output);
68 $learning = $input->hasOption('learning')?$input->getOption('learning'):false;
69 $address = $this->validatePort($input->getArgument('address'));
71 $finder = new PhpExecutableFinder();
72 if (false === $binary = $finder->find()) {
73 $io->error($this->trans('commands.server.errors.binary'));
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();
85 $this->trans('commands.server.messages.executing'),
92 $this->trans('commands.server.messages.listening'),
97 // Use the process helper to copy process output to console output.
98 $this->getHelper('process')->run($output, $process, null, null);
100 if (!$process->isSuccessful()) {
101 $io->error($process->getErrorOutput());
106 * @return null|string
108 private function getRouterPath()
111 '%s/.console/router.php',
112 $this->configurationManager->getHomeDirectory()
115 if (file_exists($router)) {
120 '%s/config/dist/router.php',
121 $this->configurationManager->getApplicationDirectory()
124 if (file_exists($router)) {
132 * @param string $address
135 private function validatePort($address)
137 if (false === strpos($address, ':')) {
141 $host = explode(':', $address)[0];
142 $port = explode(':', $address)[1];
145 if (fsockopen($host, $port)) {
146 $port = rand(8888, 9999);
153 $address = $this->validatePort($address);