Handle absolute paths, fixes #407

This commit is contained in:
Stéphane Goetz 2016-09-21 23:10:10 +02:00
parent f8806b18b7
commit 14b50c7c45
2 changed files with 48 additions and 5 deletions

View File

@ -73,7 +73,9 @@ class Daux
$params = $this->getParams();
// Validate and set theme path
$params->setDocumentationDirectory($docs_path = $this->normalizeDocumentationPath());
$params->setDocumentationDirectory(
$docs_path = $this->normalizeDocumentationPath($this->getParams()->getDocumentationDirectory())
);
// Read documentation overrides
$this->loadConfiguration($docs_path . DIRECTORY_SEPARATOR . 'config.json');
@ -109,6 +111,10 @@ class Daux
}
if (file_exists($override_file)) {
if (DauxHelper::isAbsolutePath($override_file)) {
return $override_file;
}
return getcwd() . '/' . $override_file;
}
@ -129,6 +135,10 @@ class Daux
}
if (is_dir($path)) {
if (DauxHelper::isAbsolutePath($path)) {
return $path;
}
return getcwd() . '/' . $path;
}
@ -140,7 +150,7 @@ class Daux
throw new Exception('The Themes directory does not exist. Check the path again : ' . $path);
}
public function normalizeDocumentationPath()
public function normalizeDocumentationPath($path)
{
// When running through `daux --serve` we set an environment variable to know where we started from
$env = getenv('DAUX_SOURCE');
@ -148,8 +158,11 @@ class Daux
return $env;
}
$path = $this->getParams()->getDocumentationDirectory();
if (is_dir($path)) {
if (DauxHelper::isAbsolutePath($path)) {
return $path;
}
return getcwd() . '/' . $path;
}

View File

@ -24,7 +24,7 @@ class DauxHelper
$config['image'] = str_replace('<base_url>', $base_url, $config['image']);
}
public static function resolveVariant(Config $params)
protected static function resolveVariant(Config $params)
{
if (array_key_exists('theme-variant', $params['html'])) {
return;
@ -54,7 +54,7 @@ class DauxHelper
* @param string $current_url
* @return array
*/
public static function getTheme(Config $params, $current_url)
protected static function getTheme(Config $params, $current_url)
{
self::resolveVariant($params);
@ -438,4 +438,34 @@ class DauxHelper
return implode('/', $relPath);
}
public static function isAbsolutePath($path)
{
if (!is_string($path)) {
$mess = sprintf('String expected but was given %s', gettype($path));
throw new \InvalidArgumentException($mess);
}
if (!ctype_print($path)) {
$mess = 'Path can NOT have non-printable characters or be empty';
throw new \DomainException($mess);
}
// Optional wrapper(s).
$regExp = '%^(?<wrappers>(?:[[:print:]]{2,}://)*)';
// Optional root prefix.
$regExp .= '(?<root>(?:[[:alpha:]]:/|/)?)';
// Actual path.
$regExp .= '(?<path>(?:[[:print:]]*))$%';
$parts = [];
if (!preg_match($regExp, $path, $parts)) {
$mess = sprintf('Path is NOT valid, was given %s', $path);
throw new \DomainException($mess);
}
return '' !== $parts['root'];
}
}