From c0016c759ae78e6b5bd4c68b932d03e71899ac5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Tue, 7 Nov 2017 22:44:27 +0100 Subject: [PATCH] More refactoring --- libs/Daux.php | 111 ++++----------------- libs/DauxHelper.php | 38 +++++++ libs/Format/Confluence/Publisher.php | 2 +- libs/Format/Confluence/PublisherDelete.php | 2 +- libs/Processor.php | 4 +- libs/Tree/Builder.php | 40 ++++++++ 6 files changed, 102 insertions(+), 95 deletions(-) diff --git a/libs/Daux.php b/libs/Daux.php index e6f3edd..9e46dc2 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -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); } /** diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index afd6ba8..dc284fe 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -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); + } } diff --git a/libs/Format/Confluence/Publisher.php b/libs/Format/Confluence/Publisher.php index c6bfc2d..4f744d6 100644 --- a/libs/Format/Confluence/Publisher.php +++ b/libs/Format/Confluence/Publisher.php @@ -23,7 +23,7 @@ class Publisher public $width; /** - * @var \Symfony\Component\Console\Output\Output + * @var \Symfony\Component\Console\Output\OutputInterface */ public $output; diff --git a/libs/Format/Confluence/PublisherDelete.php b/libs/Format/Confluence/PublisherDelete.php index a41c3e3..5c5b593 100644 --- a/libs/Format/Confluence/PublisherDelete.php +++ b/libs/Format/Confluence/PublisherDelete.php @@ -3,7 +3,7 @@ class PublisherDelete { /** - * @var \Symfony\Component\Console\Output\Output + * @var \Symfony\Component\Console\Output\OutputInterface */ public $output; diff --git a/libs/Processor.php b/libs/Processor.php index 4bb43f8..122a732 100644 --- a/libs/Processor.php +++ b/libs/Processor.php @@ -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) { } diff --git a/libs/Tree/Builder.php b/libs/Tree/Builder.php index 6f986b4..60322e3 100644 --- a/libs/Tree/Builder.php +++ b/libs/Tree/Builder.php @@ -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; + } + }