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 Drupal\Console\Core\Command\Command;
16 use \Drupal\Console\Core\Utils\ConfigurationManager;
21 * @package Drupal\Console\Command
23 class ServerCommand extends Command
31 * @var ConfigurationManager
33 protected $configurationManager;
36 * ServerCommand constructor.
39 * @param $configurationManager
41 public function __construct($appRoot, $configurationManager)
43 $this->appRoot = $appRoot;
44 $this->configurationManager = $configurationManager;
46 parent::__construct();
52 protected function configure()
56 ->setDescription($this->trans('commands.server.description'))
59 InputArgument::OPTIONAL,
60 $this->trans('commands.server.arguments.address'),
62 )->setAliases(['serve']);
68 protected function execute(InputInterface $input, OutputInterface $output)
70 $address = $this->validatePort($input->getArgument('address'));
72 $finder = new PhpExecutableFinder();
73 if (false === $binary = $finder->find()) {
74 $this->getIo()->error($this->trans('commands.server.errors.binary'));
78 $router = $this->configurationManager
79 ->getVendorCoreDirectory() . 'router.php';
81 $processBuilder = new ProcessBuilder([$binary, '-S', $address, $router]);
82 $processBuilder->setTimeout(null);
83 $processBuilder->setWorkingDirectory($this->appRoot);
84 $process = $processBuilder->getProcess();
86 $this->getIo()->success(
88 $this->trans('commands.server.messages.executing'),
93 $this->getIo()->commentBlock(
95 $this->trans('commands.server.messages.listening'),
100 if ($this->getIo()->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
101 $callback = [$this, 'outputCallback'];
106 // Use the process helper to copy process output to console output.
107 $this->getHelper('process')->run($output, $process, null, $callback);
109 if (!$process->isSuccessful()) {
110 $this->getIo()->error($process->getErrorOutput());
118 * @param string $address
121 private function validatePort($address)
123 if (false === strpos($address, ':')) {
127 $host = explode(':', $address)[0];
128 $port = explode(':', $address)[1];
131 if (fsockopen($host, $port)) {
132 $port = rand(8888, 9999);
139 $address = $this->validatePort($address);
145 public function outputCallback($type, $buffer)
147 // TODO: seems like $type is Process::ERR always