X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fdrupal%2Fconsole%2Fsrc%2FCommand%2FServerCommand.php;fp=vendor%2Fdrupal%2Fconsole%2Fsrc%2FCommand%2FServerCommand.php;h=1d7fcb5e2e92a2ca0c72f008de2a32bf255c8711;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/drupal/console/src/Command/ServerCommand.php b/vendor/drupal/console/src/Command/ServerCommand.php new file mode 100644 index 000000000..1d7fcb5e2 --- /dev/null +++ b/vendor/drupal/console/src/Command/ServerCommand.php @@ -0,0 +1,158 @@ +appRoot = $appRoot; + $this->configurationManager = $configurationManager; + + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('server') + ->setDescription($this->trans('commands.server.description')) + ->addArgument( + 'address', + InputArgument::OPTIONAL, + $this->trans('commands.server.arguments.address'), + '127.0.0.1:8088' + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new DrupalStyle($input, $output); + $learning = $input->hasOption('learning')?$input->getOption('learning'):false; + $address = $this->validatePort($input->getArgument('address')); + + $finder = new PhpExecutableFinder(); + if (false === $binary = $finder->find()) { + $io->error($this->trans('commands.server.errors.binary')); + return; + } + + $router = $this->getRouterPath(); + $processBuilder = new ProcessBuilder([$binary, '-S', $address, $router]); + $processBuilder->setTimeout(null); + $processBuilder->setWorkingDirectory($this->appRoot); + $process = $processBuilder->getProcess(); + + $io->success( + sprintf( + $this->trans('commands.server.messages.executing'), + $binary + ) + ); + + $io->commentBlock( + sprintf( + $this->trans('commands.server.messages.listening'), + $address + ) + ); + + // Use the process helper to copy process output to console output. + $this->getHelper('process')->run($output, $process, null, null); + + if (!$process->isSuccessful()) { + $io->error($process->getErrorOutput()); + } + } + + /** + * @return null|string + */ + private function getRouterPath() + { + $router = sprintf( + '%s/.console/router.php', + $this->configurationManager->getHomeDirectory() + ); + + if (file_exists($router)) { + return $router; + } + + $router = sprintf( + '%s/config/dist/router.php', + $this->configurationManager->getApplicationDirectory() + ); + + if (file_exists($router)) { + return $router; + } + + return null; + } + + /** + * @param string $address + * @return string + */ + private function validatePort($address) + { + if (false === strpos($address, ':')) { + $host = $address; + $port = '8088'; + } else { + $host = explode(':', $address)[0]; + $port = explode(':', $address)[1]; + } + + if (fsockopen($host, $port)) { + $port = rand(8888, 9999); + $address = sprintf( + '%s:%s', + $host, + $port + ); + + $address = $this->validatePort($address); + } + + return $address; + } +}