From 3375f8ff2bbfc55b669d94517d81f8fd4772ea3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Wed, 27 Jul 2016 23:27:51 +0200 Subject: [PATCH] Create a "daux" binary Refactored Daux a bit to allow generating documentation from working directory --- bin/daux | 77 ++++++++++++++++++ composer.json | 1 + libs/Config.php | 16 ++++ libs/Console/Generate.php | 6 +- libs/Daux.php | 78 ++++++++----------- libs/DauxHelper.php | 2 +- libs/Format/HTML/ContentPage.php | 8 +- libs/Format/HTML/Template.php | 9 ++- libs/Server/Server.php | 2 - libs/Tree/Root.php | 8 +- .../Markdown/LinkRendererTest.php | 3 +- tests/Tree/BuilderIntegrationTest.php | 3 +- tests/Tree/BuilderTest.php | 11 ++- tests/Tree/ContentTest.php | 5 +- tests/Tree/DirectoryTest.php | 6 +- 15 files changed, 164 insertions(+), 71 deletions(-) create mode 100755 bin/daux diff --git a/bin/daux b/bin/daux new file mode 100755 index 0000000..8e94bd3 --- /dev/null +++ b/bin/daux @@ -0,0 +1,77 @@ +#!/usr/bin/env php +run(); diff --git a/composer.json b/composer.json index d7d4d64..0425862 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "homepage": "http://todaymade.com/" } ], + "bin": ["bin/daux"], "require": { "php": ">=5.5", "league/plates": "~3.1", diff --git a/libs/Config.php b/libs/Config.php index 3bd64f4..9de2374 100644 --- a/libs/Config.php +++ b/libs/Config.php @@ -57,4 +57,20 @@ class Config extends ArrayObject { $this['current_page'] = $entry; } + + public function getDocumentationDirectory() { + return $this['docs_directory']; + } + + public function setDocumentationDirectory($documentationPath) { + $this['docs_directory'] = $documentationPath; + } + + public function getThemesDirectory() { + return $this['themes_directory']; + } + + public function isMultilanguage() { + return array_key_exists('languages', $this) && !empty($this['languages']); + } } diff --git a/libs/Console/Generate.php b/libs/Console/Generate.php index 7ca1c1c..347d217 100755 --- a/libs/Console/Generate.php +++ b/libs/Console/Generate.php @@ -51,13 +51,9 @@ class Generate extends SymfonyCommand // Set the source directory if ($input->getOption('source')) { - $daux->getParams()['docs_directory'] = $input->getOption('source'); + $daux->getParams()->setDocumentationDirectory($input->getOption('source')); } - $daux->setDocumentationPath($daux->getParams()['docs_directory']); - - $daux->setThemesPath($daux->getParams()['themes_directory']); - $daux->initializeConfiguration($input->getOption('configuration')); if ($input->getOption('delete')) { diff --git a/libs/Daux.php b/libs/Daux.php index f4b3d9a..e2aa236 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -29,12 +29,6 @@ class Daux */ protected $validExtensions; - /** @var string */ - private $docs_path; - - /** @var string */ - private $themes_path; - /** @var Processor */ protected $processor; @@ -79,14 +73,20 @@ class Daux */ public function initializeConfiguration($override_file = 'config.json') { + // Validate and set theme path + $this->getParams()->setDocumentationDirectory($docs_path = $this->normalizeDocumentationPath()); + // Read documentation overrides - $this->loadConfiguration($this->docs_path . DIRECTORY_SEPARATOR . 'config.json'); + $this->loadConfiguration($docs_path . DIRECTORY_SEPARATOR . 'config.json'); // Read command line overrides if (!is_null($override_file)) { $this->loadConfiguration($this->local_base . DIRECTORY_SEPARATOR . $override_file); } + // Validate and set theme path + $this->options['themes_path'] = $this->normalizeThemePath($this->getParams()->getThemesDirectory()); + // Set a valid default timezone if (isset($this->options['timezone'])) { date_default_timezone_set($this->options['timezone']); @@ -95,28 +95,27 @@ class Daux } } - public function setThemesPath($path) - { - $this->themes_path = $path; - if (!is_dir($this->themes_path) && - !is_dir($this->themes_path = $this->local_base . DIRECTORY_SEPARATOR . $this->themes_path) - ) { - throw new Exception('The Themes directory does not exist. Check the path again : ' . $this->themes_path); + public function normalizeThemePath($path) { + if (is_dir($path)) { + return $path; } - $this->options['themes_path'] = $this->themes_path; - $this->options['templates'] = 'templates'; + + $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 setDocumentationPath($path) + public function normalizeDocumentationPath() { - $this->docs_path = $path; - if (!is_dir($this->docs_path) && - !is_dir($this->docs_path = $this->local_base . DIRECTORY_SEPARATOR . $this->docs_path) - ) { - throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path); + $path = $this->getParams()->getDocumentationDirectory(); + if (is_dir($path)) { + return $path; } - $this->options['docs_path'] = $this->docs_path; + throw new Exception('The Docs directory does not exist. Check the path again : ' . $path); } /** @@ -132,6 +131,15 @@ class Daux $this->options->merge([ 'docs_directory' => 'docs', 'valid_content_extensions' => ['md', 'markdown'], + + //Paths and tree + 'mode' => $this->mode, + 'local_base' => $this->local_base, + 'templates' => 'templates', + + 'index_key' => 'index.html', + 'base_page' => '', + 'base_url' => '', ]); // Load the global configuration @@ -167,7 +175,7 @@ class Daux { $this->options['valid_content_extensions'] = $this->getContentExtensions(); - $this->tree = new Root($this->getParams(), $this->docs_path); + $this->tree = new Root($this->getParams()); Builder::build($this->tree, $this->options['ignore']); if (!empty($this->options['languages'])) { @@ -218,30 +226,10 @@ class Daux */ public function getParams() { - if (!$this->merged_defaults) { - $default = [ - //Features - 'multilanguage' => !empty($this->options['languages']), - - //Paths and tree - 'mode' => $this->mode, - 'local_base' => $this->local_base, - 'docs_path' => $this->docs_path, - 'themes_path' => $this->themes_path, - 'templates' => 'templates', - ]; - $this->options->conservativeMerge($default); - - $this->options['index_key'] = 'index.html'; - $this->options['base_page'] = $this->options['base_url'] = ''; - - $this->merged_defaults = true; - } - if ($this->tree && !$this->merged_tree) { $this->options['tree'] = $this->tree; $this->options['index'] = $this->tree->getIndexPage() ?: $this->tree->getFirstPage(); - if ($this->options['multilanguage']) { + if ($this->options->isMultilanguage()) { foreach ($this->options['languages'] as $key => $name) { $this->options['entry_page'][$key] = $this->tree->getEntries()[$key]->getFirstPage(); } diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index 303b19e..653f720 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -58,7 +58,7 @@ class DauxHelper self::resolveVariant($params); $theme_folder = $params['themes_path'] . DIRECTORY_SEPARATOR . $params['html']['theme']; - $theme_url = $params['base_url'] . $params['themes_directory'] . '/' . $params['html']['theme'] . '/'; + $theme_url = $params['base_url'] . $params->getThemesDirectory() . '/' . $params['html']['theme'] . '/'; $theme = []; if (is_file($theme_folder . DIRECTORY_SEPARATOR . 'config.json')) { diff --git a/libs/Format/HTML/ContentPage.php b/libs/Format/HTML/ContentPage.php index bf9e5c1..2113a23 100644 --- a/libs/Format/HTML/ContentPage.php +++ b/libs/Format/HTML/ContentPage.php @@ -17,7 +17,7 @@ class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage return false; } - if ($this->params['multilanguage']) { + if ($this->params->isMultilanguage()) { return $this->file->getParent()->getParent() instanceof Root; } @@ -29,7 +29,7 @@ class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage $this->homepage = $this->isHomepage(); $this->language = ''; - if ($this->params['multilanguage'] && count($this->file->getParents())) { + if ($this->params->isMultilanguage() && count($this->file->getParents())) { $language_dir = $this->file->getParents()[0]; $this->language = $language_dir->getName(); } @@ -63,7 +63,7 @@ class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage $entry_page = []; if ($this->homepage) { - if ($params['multilanguage']) { + if ($params->isMultilanguage()) { foreach ($params['languages'] as $key => $name) { $entry_page[$name] = $params['base_page'] . $params['entry_page'][$key]->getUrl(); } @@ -89,7 +89,7 @@ class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage ]; if ($page['breadcrumbs']) { - $page['breadcrumb_trail'] = $this->getBreadcrumbTrail($this->file->getParents(), $params['multilanguage']); + $page['breadcrumb_trail'] = $this->getBreadcrumbTrail($this->file->getParents(), $params->isMultilanguage()); $page['breadcrumb_separator'] = $params['html']['breadcrumb_separator']; } diff --git a/libs/Format/HTML/Template.php b/libs/Format/HTML/Template.php index eb1de68..e845401 100644 --- a/libs/Format/HTML/Template.php +++ b/libs/Format/HTML/Template.php @@ -15,9 +15,14 @@ class Template */ public function __construct($base, $theme) { - // Use templates from the phar archive if the templates dir doesn't exist. + // Use internal templates or the ones in the phar + // archive if no templates dir exists in the working directory if (!is_dir($base)) { - $base = 'phar://daux.phar/templates'; + if (is_dir(__DIR__ . '/../../../templates')) { + $base = __DIR__ . '/../../../templates'; + } else { + $base = 'phar://daux.phar/templates'; + } } // Create new Plates instance diff --git a/libs/Server/Server.php b/libs/Server/Server.php index 5ae3b93..f1ed4a2 100755 --- a/libs/Server/Server.php +++ b/libs/Server/Server.php @@ -22,8 +22,6 @@ class Server public static function serve() { $daux = new Daux(Daux::LIVE_MODE); - $daux->setThemesPath($daux->getParams()['themes_directory']); - $daux->setDocumentationPath($daux->getParams()['docs_directory']); $daux->initializeConfiguration(); $class = $daux->getProcessorClass(); diff --git a/libs/Tree/Root.php b/libs/Tree/Root.php index 3e4d49b..d579584 100644 --- a/libs/Tree/Root.php +++ b/libs/Tree/Root.php @@ -9,15 +9,13 @@ class Root extends Directory /** * The root doesn't have a parent - * - * @param string $uri */ - public function __construct(Config $config, $uri) + public function __construct(Config $config) { $this->setConfig($config); - $this->setUri($uri); - $this->path = $uri; + $this->setUri($config->getDocumentationDirectory()); + $this->path = $config->getDocumentationDirectory(); } /** diff --git a/tests/ContentTypes/Markdown/LinkRendererTest.php b/tests/ContentTypes/Markdown/LinkRendererTest.php index bf0cbf0..9941cfa 100644 --- a/tests/ContentTypes/Markdown/LinkRendererTest.php +++ b/tests/ContentTypes/Markdown/LinkRendererTest.php @@ -22,11 +22,12 @@ class LinkRendererTest extends \PHPUnit_Framework_TestCase ]; $root = vfsStream::setup('root', null, $structure); + $config->setDocumentationDirectory($root->url()); $config['valid_content_extensions'] = ['md']; $config['mode'] = Daux::STATIC_MODE; $config['index_key'] = 'index.html'; - $tree = new Root($config, $root->url()); + $tree = new Root($config); Builder::build($tree, []); return $tree; diff --git a/tests/Tree/BuilderIntegrationTest.php b/tests/Tree/BuilderIntegrationTest.php index 400c746..e2815d6 100644 --- a/tests/Tree/BuilderIntegrationTest.php +++ b/tests/Tree/BuilderIntegrationTest.php @@ -29,11 +29,12 @@ class BuilderIntegrationTest extends \PHPUnit_Framework_TestCase public function testCreateHierarchy() { $config = new Config(); + $config->setDocumentationDirectory($this->root->url()); $config['valid_content_extensions'] = ['md']; $config['mode'] = Daux::STATIC_MODE; $config['index_key'] = 'index.html'; - $tree = new Root($config, $this->root->url()); + $tree = new Root($config); Builder::build($tree, []); $this->assertCount(2, $tree); diff --git a/tests/Tree/BuilderTest.php b/tests/Tree/BuilderTest.php index 5c29bf5..f94e24e 100644 --- a/tests/Tree/BuilderTest.php +++ b/tests/Tree/BuilderTest.php @@ -39,7 +39,9 @@ class BuilderTest extends \PHPUnit_Framework_TestCase public function testGetOrCreateDirNew() { - $root = new Root(new Config(), ''); + $config = new Config; + $config->setDocumentationDirectory(''); + $root = new Root($config); $dir = Builder::getOrCreateDir($root, 'directory'); @@ -51,7 +53,9 @@ class BuilderTest extends \PHPUnit_Framework_TestCase public function testGetOrCreateDirExisting() { - $root = new Root(new Config(), ''); + $config = new Config; + $config->setDocumentationDirectory(''); + $root = new Root($config); $directory = new Directory($root, 'directory'); $directory->setTitle('directory'); @@ -66,11 +70,12 @@ class BuilderTest extends \PHPUnit_Framework_TestCase public function getStaticRoot() { $config = new Config(); + $config->setDocumentationDirectory(''); $config['mode'] = Daux::STATIC_MODE; $config['index_key'] = 'index.html'; $config['valid_content_extensions'] = ['md']; - return new Root($config, ''); + return new Root($config); } public function testGetOrCreatePage() diff --git a/tests/Tree/ContentTest.php b/tests/Tree/ContentTest.php index 765f780..c42608c 100644 --- a/tests/Tree/ContentTest.php +++ b/tests/Tree/ContentTest.php @@ -6,7 +6,10 @@ class ContentTest extends \PHPUnit_Framework_TestCase { protected function createContent($content) { - $dir = new Directory(new Root(new Config, ''), ''); + $config = new Config; + $config->setDocumentationDirectory(''); + + $dir = new Directory(new Root($config), ''); $obj = new Content($dir, ''); $obj->setContent($content); diff --git a/tests/Tree/DirectoryTest.php b/tests/Tree/DirectoryTest.php index a836276..9612407 100644 --- a/tests/Tree/DirectoryTest.php +++ b/tests/Tree/DirectoryTest.php @@ -28,7 +28,11 @@ class DirectoryTest extends \PHPUnit_Framework_TestCase public function testSort($list, $expected) { shuffle($list); - $directory = new Directory(new Root(new Config(), ''), 'dir'); + + $config = new Config; + $config->setDocumentationDirectory(''); + + $directory = new Directory(new Root($config), 'dir'); foreach ($list as $value) { $entry = new Content($directory, $value);