daux.io/libs/Console/Serve.php

70 lines
2.1 KiB
PHP
Raw Normal View History

<?php namespace Todaymade\Daux\Console;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
2020-04-22 22:24:52 +02:00
use Symfony\Component\Process\PhpExecutableFinder;
use Todaymade\Daux\Daux;
class Serve extends DauxCommand
{
protected function configure()
{
parent::configure();
$this
->setName('serve')
->setDescription('Serve documentation')
->addOption('host', null, InputOption::VALUE_REQUIRED, 'The host to serve on', 'localhost')
->addOption('port', null, InputOption::VALUE_REQUIRED, 'The port to serve on', 8085);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$host = $input->getOption('host');
$port = $input->getOption('port');
$builder = $this->prepareConfig(Daux::LIVE_MODE, $input, $output);
// Daux can only serve HTML
$builder->withFormat('html');
$daux = new Daux($builder->build(), $output);
2020-04-22 22:24:52 +02:00
$width = (new Terminal())->getWidth();
// Instiantiate the processor if one is defined
$this->prepareProcessor($daux, $width);
// Write the current configuration to a file to read it from the other serving side
$file = tmpfile();
2019-12-07 11:22:40 +01:00
2019-12-07 16:32:38 +01:00
if ($file === false) {
2020-04-22 22:24:52 +02:00
$output->writeln('<fg=red>Failed to create temporary file for configuration</fg=red>');
2019-12-07 11:22:40 +01:00
return 1;
}
2020-04-22 21:55:53 +02:00
$path = stream_get_meta_data($file)['uri'];
fwrite($file, serialize($daux->getConfig()));
chdir(__DIR__ . '/../../');
2019-12-07 16:32:38 +01:00
putenv('DAUX_CONFIG=' . $path);
putenv('DAUX_VERBOSITY=' . $output->getVerbosity());
2018-05-05 18:16:24 +02:00
putenv('DAUX_EXTENSION=' . DAUX_EXTENSION);
$base = escapeshellarg(__DIR__ . '/../../');
2020-04-22 22:24:52 +02:00
$binary = escapeshellarg((new PhpExecutableFinder())->find(false));
echo "Daux development server started on http://{$host}:{$port}/\n";
2018-09-20 20:17:51 +02:00
passthru("{$binary} -S {$host}:{$port} {$base}/index.php");
fclose($file);
2019-11-28 22:41:53 +01:00
return 0;
}
}