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;
18 use \Drupal\Console\Core\Utils\ConfigurationManager;
23 * @package Drupal\Console\Command
25 class ServerCommand extends Command
35 * @var ConfigurationManager
37 protected $configurationManager;
40 * ServerCommand constructor.
43 * @param $configurationManager
45 public function __construct($appRoot, $configurationManager)
47 $this->appRoot = $appRoot;
48 $this->configurationManager = $configurationManager;
50 parent::__construct();
56 protected function configure()
60 ->setDescription($this->trans('commands.server.description'))
63 InputArgument::OPTIONAL,
64 $this->trans('commands.server.arguments.address'),
72 protected function execute(InputInterface $input, OutputInterface $output)
74 $io = new DrupalStyle($input, $output);
75 $address = $this->validatePort($input->getArgument('address'));
77 $finder = new PhpExecutableFinder();
78 if (false === $binary = $finder->find()) {
79 $io->error($this->trans('commands.server.errors.binary'));
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();
91 $this->trans('commands.server.messages.executing'),
98 $this->trans('commands.server.messages.listening'),
103 // Use the process helper to copy process output to console output.
104 $this->getHelper('process')->run($output, $process, null, null);
106 if (!$process->isSuccessful()) {
107 $io->error($process->getErrorOutput());
115 * @return null|string
117 private function getRouterPath()
121 '%s/.console/router.php',
122 $this->configurationManager->getHomeDirectory()
125 '%s/console/router.php',
126 $this->configurationManager->getApplicationDirectory()
129 '%s/%s/config/dist/router.php',
130 $this->configurationManager->getApplicationDirectory(),
135 foreach ($routerPath as $router) {
136 if (file_exists($router)) {
145 * @param string $address
148 private function validatePort($address)
150 if (false === strpos($address, ':')) {
154 $host = explode(':', $address)[0];
155 $port = explode(':', $address)[1];
158 if (fsockopen($host, $port)) {
159 $port = rand(8888, 9999);
166 $address = $this->validatePort($address);