diff --git a/libs/Config.php b/libs/Config.php index 0a70754..05f9f20 100644 --- a/libs/Config.php +++ b/libs/Config.php @@ -73,11 +73,13 @@ class Config extends ArrayObject return $this['themes_directory']; } - public function setThemesPath($themePath) { + public function setThemesPath($themePath) + { $this['themes_path'] = $themePath; } - public function getThemesPath() { + public function getThemesPath() + { return $this['themes_path']; } diff --git a/libs/Console/Serve.php b/libs/Console/Serve.php index cd09cc9..f9ad7d7 100755 --- a/libs/Console/Serve.php +++ b/libs/Console/Serve.php @@ -41,6 +41,7 @@ class Serve extends DauxCommand chdir(__DIR__ . '/../../'); putenv('DAUX_SOURCE=' . $daux->getParams()->getDocumentationDirectory()); + putenv('DAUX_THEME=' . $daux->getParams()->getThemesPath()); //TODO :: support configuration and processor //putenv('DAUX_CONFIGURATION=' . $daux->getParams()->getConfigurationFile()); diff --git a/libs/Daux.php b/libs/Daux.php index 13852e3..273f61f 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -86,7 +86,6 @@ class Daux // Validate and set theme path $params->setThemesPath($this->normalizeThemePath($params->getThemesDirectory())); - // Set a valid default timezone if ($params->hasTimezone()) { date_default_timezone_set($params->getTimezone()); @@ -97,8 +96,14 @@ class Daux 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)) { - return $path; + return getcwd() . '/' . $path; } $newPath = $this->local_base . DIRECTORY_SEPARATOR . $path; diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index 5ddfd31..e6864d2 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -44,7 +44,7 @@ class DauxHelper $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"); } } diff --git a/libs/Server/Server.php b/libs/Server/Server.php index f1ed4a2..0d6d653 100755 --- a/libs/Server/Server.php +++ b/libs/Server/Server.php @@ -39,7 +39,7 @@ class Server $server = new static($daux); try { - $page = $server->handle($_REQUEST); + $page = $server->handle(); } catch (NotFoundException $e) { http_response_code(404); $page = new ErrorPage('An error occured', $e->getMessage(), $daux->getParams()); @@ -106,17 +106,21 @@ class Server /** * Handle an incoming request * - * @param array $query * @return \Todaymade\Daux\Format\Base\Page * @throws Exception * @throws NotFoundException */ - public function handle($query = []) + public function handle() { $this->params = $this->getParams(); $request = $this->getRequest(); $request = urldecode($request); + + if (substr($request, 0, 7) == 'themes/') { + return $this->serveTheme(substr($request, 6)); + } + if ($request == 'index_page') { $request = $this->daux->tree->getIndexPage()->getUri(); } @@ -124,6 +128,23 @@ class Server 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 * @return \Todaymade\Daux\Format\Base\Page