8
0
daux.io/libs/Server/Server.php

202 Zeilen
5.6 KiB
PHP

2015-04-22 12:23:57 +02:00
<?php namespace Todaymade\Daux\Server;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
2019-08-07 18:55:18 +02:00
use Symfony\Component\Mime\MimeTypes;
2015-04-22 12:23:57 +02:00
use Todaymade\Daux\Daux;
use Todaymade\Daux\ConfigBuilder;
use Todaymade\Daux\DauxHelper;
2015-04-22 12:23:57 +02:00
use Todaymade\Daux\Exception;
use Todaymade\Daux\Format\Base\ComputedRawPage;
use Todaymade\Daux\Format\Base\LiveGenerator;
use Todaymade\Daux\Format\Base\Page;
use Todaymade\Daux\Format\HTML\RawPage;
2015-04-22 12:23:57 +02:00
2015-04-23 00:32:30 +02:00
class Server
{
2015-04-22 12:23:57 +02:00
private $daux;
private $config;
private $base_url;
2015-04-22 12:23:57 +02:00
2017-11-07 22:54:31 +01:00
/**
* @var Request
*/
private $request;
2015-07-17 23:38:06 +02:00
/**
* Serve the documentation
*
* @throws Exception
*/
2015-04-23 00:32:30 +02:00
public static function serve()
{
$verbosity = getenv('DAUX_VERBOSITY');
$output = new ConsoleOutput($verbosity);
2018-06-05 20:31:51 +02:00
$configFile = getenv('DAUX_CONFIG');
if ($configFile) {
$config = ConfigBuilder::fromFile($configFile);
} else {
$config = ConfigBuilder::withMode(Daux::LIVE_MODE)->build();
}
$daux = new Daux($config, $output);
$class = $daux->getProcessorClass();
2015-07-20 15:59:52 +02:00
if (!empty($class)) {
2018-06-05 20:31:51 +02:00
$daux->setProcessor(new $class($daux, $output, 0));
}
// Improve the tree with a processor
2015-07-19 23:17:57 +02:00
$daux->generateTree();
$server = new static($daux);
2015-04-23 00:32:30 +02:00
try {
$page = $server->handle();
2015-04-23 00:32:30 +02:00
} catch (NotFoundException $e) {
$page = new ErrorPage('An error occured', $e->getMessage(), $daux->getConfig());
2015-04-22 12:23:57 +02:00
}
$server->createResponse($page)->prepare($server->request)->send();
2015-04-22 12:23:57 +02:00
}
2015-04-23 00:32:30 +02:00
public function __construct(Daux $daux)
{
2015-04-22 12:23:57 +02:00
$this->daux = $daux;
$this->request = Request::createFromGlobals();
$this->base_url = $this->request->getHttpHost() . $this->request->getBaseUrl() . "/";
}
/**
* Create a temporary file with the file suffix, for mime type detection.
*
* @param string $postfix
* @return string
*/
2017-11-07 22:54:31 +01:00
private function getTemporaryFile($postfix) {
$sysFileName = tempnam(sys_get_temp_dir(), 'daux');
if ($sysFileName === false) {
throw new \RuntimeException("Could not create temporary file");
}
$newFileName = $sysFileName . $postfix;
if ($sysFileName == $newFileName) {
return $sysFileName;
}
if (DIRECTORY_SEPARATOR == '\\' ? rename($sysFileName, $newFileName) : link($sysFileName, $newFileName)) {
return $newFileName;
}
throw new \RuntimeException("Could not create temporary file");
}
/**
* @param Page $page
* @return Response
*/
public function createResponse(Page $page) {
2019-11-28 22:36:26 +01:00
// Add a custom MimeType guesser in case the default ones are not available
// This makes sure that at least CSS and JS work fine.
2019-08-07 18:55:18 +02:00
$mimeTypes = MimeTypes::getDefault();
$mimeTypes->registerGuesser(new ExtensionMimeTypeGuesser());
2017-12-08 16:29:44 +01:00
if ($page instanceof RawPage) {
return new BinaryFileResponse($page->getFile());
}
if ($page instanceof ComputedRawPage) {
$file = $this->getTemporaryFile($page->getFilename());
file_put_contents($file, $page->getContent());
return new BinaryFileResponse($file);
}
return new Response($page->getContent(), $page instanceof ErrorPage ? 404 : 200);
}
2015-07-17 23:38:06 +02:00
/**
* @return \Todaymade\Daux\Config
*/
public function getConfig()
{
$config = $this->daux->getConfig();
DauxHelper::rebaseConfiguration($config, '//' . $this->base_url);
2016-03-13 21:51:58 +01:00
return $config;
2015-04-22 12:23:57 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* Handle an incoming request
*
2015-07-20 15:59:52 +02:00
* @return \Todaymade\Daux\Format\Base\Page
2015-07-17 23:38:06 +02:00
* @throws Exception
* @throws NotFoundException
*/
public function handle()
2015-04-23 00:32:30 +02:00
{
$this->config = $this->getConfig();
2015-04-22 12:23:57 +02:00
$request = substr($this->request->getRequestUri(), strlen($this->request->getBaseUrl()) + 1);
if (substr($request, 0, 7) == 'themes/') {
return $this->serveTheme(substr($request, 6));
}
if ($request == '') {
2015-07-19 23:17:57 +02:00
$request = $this->daux->tree->getIndexPage()->getUri();
2015-04-22 12:23:57 +02:00
}
2015-07-18 00:56:53 +02:00
return $this->getPage($request);
2015-04-22 12:23:57 +02:00
}
/**
* Handle a request on custom themes
*
2017-06-07 23:40:12 +02:00
* @param string $request
* @return \Todaymade\Daux\Format\Base\Page
* @throws NotFoundException
*/
public function serveTheme($request)
{
$file = $this->getConfig()->getThemesPath() . $request;
if (file_exists($file)) {
return new RawPage($file);
}
throw new NotFoundException;
}
2015-07-17 23:38:06 +02:00
/**
* @param string $request
2015-07-20 15:59:52 +02:00
* @return \Todaymade\Daux\Format\Base\Page
2015-07-17 23:38:06 +02:00
* @throws NotFoundException
*/
2015-04-23 00:32:30 +02:00
private function getPage($request)
{
$file = DauxHelper::getFile($this->daux->tree, $request);
2015-04-23 00:32:30 +02:00
if ($file === false) {
throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
}
$this->daux->tree->setActiveNode($file);
$generator = $this->daux->getGenerator();
if (!$generator instanceof LiveGenerator) {
throw new \RuntimeException(
"The generator '" . get_class($generator) . "' does not implement the interface " .
"'Todaymade\\Daux\\Format\\Base\\LiveGenerator' and thus doesn't support live rendering."
);
}
return $this->daux->getGenerator()->generateOne($file, $this->config);
2015-04-22 12:23:57 +02:00
}
}