2015-04-22 12:23:57 +02:00
|
|
|
<?php namespace Todaymade\Daux\Server;
|
|
|
|
|
2015-07-19 22:38:37 +02:00
|
|
|
use Symfony\Component\Console\Output\NullOutput;
|
2017-06-08 23:07:18 +02:00
|
|
|
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;
|
2015-05-18 14:26:29 +02:00
|
|
|
use Todaymade\Daux\DauxHelper;
|
2015-04-22 12:23:57 +02:00
|
|
|
use Todaymade\Daux\Exception;
|
2017-03-01 10:54:38 +01:00
|
|
|
use Todaymade\Daux\Format\Base\ComputedRawPage;
|
2015-07-19 22:38:37 +02:00
|
|
|
use Todaymade\Daux\Format\Base\LiveGenerator;
|
2017-06-08 23:07:18 +02:00
|
|
|
use Todaymade\Daux\Format\Base\Page;
|
2015-05-18 14:26:29 +02:00
|
|
|
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;
|
2015-05-18 14:26:29 +02:00
|
|
|
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()
|
|
|
|
{
|
2018-06-05 20:31:51 +02:00
|
|
|
$output = new NullOutput();
|
|
|
|
|
|
|
|
$daux = new Daux(Daux::LIVE_MODE, $output);
|
2015-07-28 17:26:35 +02:00
|
|
|
$daux->initializeConfiguration();
|
2015-07-19 22:38:37 +02:00
|
|
|
|
|
|
|
$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));
|
2015-07-19 22:38:37 +02:00
|
|
|
}
|
|
|
|
|
2015-07-19 23:17:57 +02:00
|
|
|
// Set this critical configuration
|
|
|
|
// for the tree generation
|
|
|
|
$daux->getParams()['index_key'] = 'index';
|
|
|
|
|
2015-07-19 22:38:37 +02:00
|
|
|
// Improve the tree with a processor
|
2015-07-19 23:17:57 +02:00
|
|
|
$daux->generateTree();
|
2015-07-19 22:38:37 +02:00
|
|
|
|
|
|
|
$server = new static($daux);
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
try {
|
2016-09-12 23:58:58 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-06-08 23:07:18 +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;
|
2015-05-18 14:26:29 +02:00
|
|
|
|
2017-06-08 23:07:18 +02:00
|
|
|
$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) {
|
2017-06-08 23:07:18 +02:00
|
|
|
$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;
|
|
|
|
}
|
2015-08-15 16:31:36 +02:00
|
|
|
|
2017-06-08 23:07:18 +02:00
|
|
|
throw new \RuntimeException("Could not create temporary file");
|
|
|
|
}
|
2015-08-15 16:31:36 +02:00
|
|
|
|
2017-06-08 23:07:18 +02:00
|
|
|
/**
|
|
|
|
* @param Page $page
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function createResponse(Page $page) {
|
2017-12-08 16:29:44 +01:00
|
|
|
|
2019-08-07 18:55:18 +02:00
|
|
|
$mimeTypes = MimeTypes::getDefault();
|
|
|
|
$mimeTypes->registerGuesser(new ExtensionMimeTypeGuesser());
|
2017-12-08 16:29:44 +01:00
|
|
|
|
2017-06-08 23:07:18 +02:00
|
|
|
if ($page instanceof RawPage) {
|
|
|
|
return new BinaryFileResponse($page->getFile());
|
2015-05-18 14:26:29 +02:00
|
|
|
}
|
2017-06-08 23:07:18 +02:00
|
|
|
|
|
|
|
if ($page instanceof ComputedRawPage) {
|
|
|
|
$file = $this->getTemporaryFile($page->getFilename());
|
|
|
|
file_put_contents($file, $page->getContent());
|
|
|
|
return new BinaryFileResponse($file);
|
2015-05-18 14:26:29 +02:00
|
|
|
}
|
2017-06-08 23:07:18 +02:00
|
|
|
|
|
|
|
return new Response($page->getContent(), $page instanceof ErrorPage ? 404 : 200);
|
2015-05-18 14:26:29 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* @return \Todaymade\Daux\Config
|
|
|
|
*/
|
2015-05-18 14:26:29 +02:00
|
|
|
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']) {
|
2015-05-18 14:26:29 +02:00
|
|
|
$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;
|
|
|
|
|
2015-05-18 14:26:29 +02:00
|
|
|
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
|
|
|
|
*/
|
2016-09-12 23:58:58 +02:00
|
|
|
public function handle()
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-05-18 14:26:29 +02:00
|
|
|
$this->params = $this->getParams();
|
2015-04-22 12:23:57 +02:00
|
|
|
|
2018-06-20 02:00:13 +02:00
|
|
|
$request = substr($this->request->getRequestUri(), strlen($this->request->getBaseUrl()) + 1);
|
2016-09-12 23:58:58 +02:00
|
|
|
|
|
|
|
if (substr($request, 0, 7) == 'themes/') {
|
|
|
|
return $this->serveTheme(substr($request, 6));
|
|
|
|
}
|
|
|
|
|
2017-06-08 23:07:18 +02:00
|
|
|
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-04-27 12:47:10 +02:00
|
|
|
|
2015-07-18 00:56:53 +02:00
|
|
|
return $this->getPage($request);
|
2015-04-22 12:23:57 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 23:58:58 +02:00
|
|
|
/**
|
|
|
|
* Handle a request on custom themes
|
|
|
|
*
|
2017-06-07 23:40:12 +02:00
|
|
|
* @param string $request
|
2016-09-12 23:58:58 +02:00
|
|
|
* @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)
|
|
|
|
{
|
2015-05-18 14:26:29 +02:00
|
|
|
$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.');
|
|
|
|
}
|
2015-04-27 12:47:10 +02:00
|
|
|
|
2017-01-09 18:29:52 +01:00
|
|
|
$this->daux->tree->setActiveNode($file);
|
|
|
|
|
2015-07-19 22:38:37 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|