Handle custom themes on serve fixes #396

This commit is contained in:
Stéphane Goetz 2016-09-12 23:58:58 +02:00
parent 7ee564c667
commit aa4ad02b46
5 changed files with 37 additions and 8 deletions

View File

@ -73,11 +73,13 @@ class Config extends ArrayObject
return $this['themes_directory']; return $this['themes_directory'];
} }
public function setThemesPath($themePath) { public function setThemesPath($themePath)
{
$this['themes_path'] = $themePath; $this['themes_path'] = $themePath;
} }
public function getThemesPath() { public function getThemesPath()
{
return $this['themes_path']; return $this['themes_path'];
} }

View File

@ -41,6 +41,7 @@ class Serve extends DauxCommand
chdir(__DIR__ . '/../../'); chdir(__DIR__ . '/../../');
putenv('DAUX_SOURCE=' . $daux->getParams()->getDocumentationDirectory()); putenv('DAUX_SOURCE=' . $daux->getParams()->getDocumentationDirectory());
putenv('DAUX_THEME=' . $daux->getParams()->getThemesPath());
//TODO :: support configuration and processor //TODO :: support configuration and processor
//putenv('DAUX_CONFIGURATION=' . $daux->getParams()->getConfigurationFile()); //putenv('DAUX_CONFIGURATION=' . $daux->getParams()->getConfigurationFile());

View File

@ -86,7 +86,6 @@ class Daux
// Validate and set theme path // Validate and set theme path
$params->setThemesPath($this->normalizeThemePath($params->getThemesDirectory())); $params->setThemesPath($this->normalizeThemePath($params->getThemesDirectory()));
// Set a valid default timezone // Set a valid default timezone
if ($params->hasTimezone()) { if ($params->hasTimezone()) {
date_default_timezone_set($params->getTimezone()); date_default_timezone_set($params->getTimezone());
@ -97,8 +96,14 @@ class Daux
public function normalizeThemePath($path) public function normalizeThemePath($path)
{ {
// When running through `daux --serve` we set an environment variable to know where we started from
$env = getenv('DAUX_THEME');
if ($env && is_dir($env)) {
return $env;
}
if (is_dir($path)) { if (is_dir($path)) {
return $path; return getcwd() . '/' . $path;
} }
$newPath = $this->local_base . DIRECTORY_SEPARATOR . $path; $newPath = $this->local_base . DIRECTORY_SEPARATOR . $path;

View File

@ -44,7 +44,7 @@ class DauxHelper
$params['html']['theme'] = array_pop($theme); $params['html']['theme'] = array_pop($theme);
} }
if (!is_dir(realpath(($params->getThemesPath() . DIRECTORY_SEPARATOR . $params['html']['theme'])))) { if (!is_dir(realpath($params->getThemesPath() . DIRECTORY_SEPARATOR . $params['html']['theme']))) {
throw new \RuntimeException("Theme '{$params['html']['theme']}' not found"); throw new \RuntimeException("Theme '{$params['html']['theme']}' not found");
} }
} }

View File

@ -39,7 +39,7 @@ class Server
$server = new static($daux); $server = new static($daux);
try { try {
$page = $server->handle($_REQUEST); $page = $server->handle();
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
http_response_code(404); http_response_code(404);
$page = new ErrorPage('An error occured', $e->getMessage(), $daux->getParams()); $page = new ErrorPage('An error occured', $e->getMessage(), $daux->getParams());
@ -106,17 +106,21 @@ class Server
/** /**
* Handle an incoming request * Handle an incoming request
* *
* @param array $query
* @return \Todaymade\Daux\Format\Base\Page * @return \Todaymade\Daux\Format\Base\Page
* @throws Exception * @throws Exception
* @throws NotFoundException * @throws NotFoundException
*/ */
public function handle($query = []) public function handle()
{ {
$this->params = $this->getParams(); $this->params = $this->getParams();
$request = $this->getRequest(); $request = $this->getRequest();
$request = urldecode($request); $request = urldecode($request);
if (substr($request, 0, 7) == 'themes/') {
return $this->serveTheme(substr($request, 6));
}
if ($request == 'index_page') { if ($request == 'index_page') {
$request = $this->daux->tree->getIndexPage()->getUri(); $request = $this->daux->tree->getIndexPage()->getUri();
} }
@ -124,6 +128,23 @@ class Server
return $this->getPage($request); return $this->getPage($request);
} }
/**
* Handle a request on custom themes
*
* @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;
}
/** /**
* @param string $request * @param string $request
* @return \Todaymade\Daux\Format\Base\Page * @return \Todaymade\Daux\Format\Base\Page