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

196 Zeilen
5.4 KiB
PHP

2015-04-22 12:23:57 +02:00
<?php namespace Todaymade\Daux\Server;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
2015-04-22 12:23:57 +02:00
use Todaymade\Daux\Daux;
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 $params;
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()
{
2015-04-22 12:23:57 +02:00
$daux = new Daux(Daux::LIVE_MODE);
$daux->initializeConfiguration();
$class = $daux->getProcessorClass();
2015-07-20 15:59:52 +02:00
if (!empty($class)) {
$daux->setProcessor(new $class($daux, new NullOutput(), 0));
}
2015-07-19 23:17:57 +02:00
// Set this critical configuration
// for the tree generation
$daux->getParams()['index_key'] = 'index';
// 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) {
2016-07-27 21:32:51 +02:00
$page = new ErrorPage('An error occured', $e->getMessage(), $daux->getParams());
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) {
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 getParams()
{
$params = $this->daux->getParams();
2015-07-19 14:05:12 +02:00
DauxHelper::rebaseConfiguration($params, '//' . $this->base_url);
$params['base_page'] = '//' . $this->base_url;
2015-07-19 16:36:34 +02:00
if (!$this->daux->options['live']['clean_urls']) {
$params['base_page'] .= 'index.php/';
}
2016-03-13 21:51:58 +01:00
// Text search would be too slow on live server
$params['html']['search'] = false;
return $params;
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->params = $this->getParams();
2015-04-22 12:23:57 +02:00
$request = substr($this->request->getRequestUri(), 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->getParams()->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."
);
}
2016-03-13 21:51:58 +01:00
return $this->daux->getGenerator()->generateOne($file, $this->params);
2015-04-22 12:23:57 +02:00
}
}