More refactoring

This commit is contained in:
Stéphane Goetz 2017-11-07 22:44:27 +01:00
parent f281169871
commit c0016c759a
6 changed files with 102 additions and 95 deletions

View File

@ -15,9 +15,6 @@ class Daux
/** @var string */
public $local_base;
/** @var string */
public $internal_base;
/** @var \Todaymade\Daux\Format\Base\Generator */
protected $generator;
@ -51,14 +48,7 @@ class Daux
{
$this->mode = $mode;
$this->local_base = $this->internal_base = dirname(__DIR__);
// In case we're inside the phar archive
// we save the path to the directory
// in which it is contained
if (defined('PHAR_DIR')) {
$this->local_base = PHAR_DIR;
}
$this->local_base = dirname(__DIR__);
// global.json
$this->loadBaseConfiguration();
@ -101,79 +91,46 @@ class Daux
/**
* Get the file requested for configuration overrides
*
* @param string|null $override_file
* @param string|null $path
* @return string|null the path to a file to load for configuration overrides
* @throws Exception
*/
public function getConfigurationOverride($override_file)
public function getConfigurationOverride($path)
{
// When running through `daux --serve` we set an environment variable to know where we started from
$env = getenv('DAUX_CONFIGURATION');
if ($env && file_exists($env)) {
return $env;
}
$validPath = DauxHelper::findLocation($path, $this->local_base, 'DAUX_CONFIGURATION', 'file');
if ($override_file == null) {
if ($validPath === null) {
return null;
}
if (file_exists($override_file)) {
if (DauxHelper::isAbsolutePath($override_file)) {
return $override_file;
}
return getcwd() . '/' . $override_file;
if (!$validPath) {
throw new Exception('The configuration override file does not exist. Check the path again : ' . $path);
}
$newPath = $this->local_base . DIRECTORY_SEPARATOR . $override_file;
if (file_exists($newPath)) {
return $newPath;
}
throw new Exception('The configuration override file does not exist. Check the path again : ' . $override_file);
return $validPath;
}
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;
$validPath = DauxHelper::findLocation($path, $this->local_base, 'DAUX_THEME', 'dir');
if (!$validPath) {
throw new Exception('The Themes directory does not exist. Check the path again : ' . $path);
}
if (is_dir($path)) {
if (DauxHelper::isAbsolutePath($path)) {
return $path;
}
return $validPath;
return getcwd() . '/' . $path;
}
$newPath = $this->local_base . DIRECTORY_SEPARATOR . $path;
if (is_dir($newPath)) {
return $newPath;
}
throw new Exception('The Themes directory does not exist. Check the path again : ' . $path);
}
public function normalizeDocumentationPath($path)
{
// When running through `daux --serve` we set an environment variable to know where we started from
$env = getenv('DAUX_SOURCE');
if ($env && is_dir($env)) {
return $env;
$validPath = DauxHelper::findLocation($path, $this->local_base, 'DAUX_SOURCE', 'dir');
if (!$validPath) {
throw new Exception('The Docs directory does not exist. Check the path again : ' . $path);
}
if (is_dir($path)) {
if (DauxHelper::isAbsolutePath($path)) {
return $path;
}
return getcwd() . '/' . $path;
}
throw new Exception('The Docs directory does not exist. Check the path again : ' . $path);
return $validPath;
}
/**
@ -247,37 +204,9 @@ class Daux
$this->getProcessor()->manipulateTree($this->tree);
// Sort the tree one last time before it is finalized
$this->sortTree($this->tree);
Builder::sortTree($this->tree);
$this->finalizeTree($this->tree);
}
public function sortTree(Directory $current)
{
$current->sort();
foreach ($current->getEntries() as $entry) {
if ($entry instanceof Directory) {
$this->sortTree($entry);
}
}
}
public function finalizeTree(Directory $current, $prev = null)
{
foreach ($current->getEntries() as $entry) {
if ($entry instanceof Directory) {
$prev = $this->finalizeTree($entry, $prev);
} elseif ($entry instanceof Content) {
if ($prev) {
$prev->setNext($entry);
$entry->setPrevious($prev);
}
$prev = $entry;
}
}
return $prev;
Builder::finalizeTree($this->tree);
}
/**

View File

@ -468,4 +468,42 @@ class DauxHelper
return '' !== $parts['root'];
}
/**
* @param $path
* @param $basedir
* @param $var
* @param $type
* @return false|null|string
*/
public static function findLocation($path, $basedir, $var, $type) {
// When running through `daux --serve` we set an environment variable to know where we started from
$env = getenv($var);
if ($env && DauxHelper::is($env, $type)) {
return $env;
}
if ($path == null) {
return null;
}
if (DauxHelper::is($path, $type)) {
if (DauxHelper::isAbsolutePath($path)) {
return $path;
}
return getcwd() . '/' . $path;
}
$newPath = $basedir . DIRECTORY_SEPARATOR . $path;
if (DauxHelper::is($newPath, $type)) {
return $newPath;
}
return false;
}
public static function is($path, $type) {
return ($type == 'dir') ? is_dir($path) : file_exists($path);
}
}

View File

@ -23,7 +23,7 @@ class Publisher
public $width;
/**
* @var \Symfony\Component\Console\Output\Output
* @var \Symfony\Component\Console\Output\OutputInterface
*/
public $output;

View File

@ -3,7 +3,7 @@
class PublisherDelete
{
/**
* @var \Symfony\Component\Console\Output\Output
* @var \Symfony\Component\Console\Output\OutputInterface
*/
public $output;

View File

@ -40,7 +40,7 @@ class Processor
*
* @param Root $root
*/
public function manipulateTree(Root $root)
public function manipulateTree(/** @scrutinizer ignore-unused */ Root $root)
{
}
@ -51,7 +51,7 @@ class Processor
*
* @param Environment $environment
*/
public function extendCommonMarkEnvironment(Environment $environment)
public function extendCommonMarkEnvironment(/** @scrutinizer ignore-unused */ Environment $environment)
{
}

View File

@ -203,4 +203,44 @@ class Builder
return $page;
}
/**
* Sort the tree recursively
*
* @param Directory $current
*/
public static function sortTree(Directory $current) {
$current->sort();
foreach ($current->getEntries() as $entry) {
if ($entry instanceof Directory) {
Builder::sortTree($entry);
}
}
}
/**
* Calculate next and previous for all pages
*
* @param Directory $current
* @param null|Content $prev
* @return null|Content
*/
public static function finalizeTree(Directory $current, $prev = null)
{
foreach ($current->getEntries() as $entry) {
if ($entry instanceof Directory) {
$prev = Builder::finalizeTree($entry, $prev);
} elseif ($entry instanceof Content) {
if ($prev) {
$prev->setNext($entry);
$entry->setPrevious($prev);
}
$prev = $entry;
}
}
return $prev;
}
}