From 60b50919b4a6f698c168ea277d5875c1c93769f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Mon, 29 Jun 2015 16:11:01 +0200 Subject: [PATCH] Remove "default.json" concentrate all configurations in "global.json" Set configurations in an Object instead of an array --- default.json | 29 ------------ global.json | 30 +++++++++++- libs/Config.php | 20 ++++++++ libs/Daux.php | 70 +++++++++++++++------------- libs/Format/Base/MarkdownPage.php | 5 +- libs/Format/Confluence/Generator.php | 3 +- libs/Generator/Generator.php | 3 +- 7 files changed, 92 insertions(+), 68 deletions(-) delete mode 100755 default.json create mode 100644 libs/Config.php diff --git a/default.json b/default.json deleted file mode 100755 index 999a593..0000000 --- a/default.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "title": "My Project", - "tagline": "My Stylish Documentation", - "author": "I, Me & Myself", - "image": "", - "repo": "", - "twitter": [], - "links": { - }, - - "languages": {}, - "theme": "daux-blue", - "ignore": { - "files": [], - "folders": [] - }, - - "breadcrumbs": false, - "clean_urls": false, - "toggle_code": false, - "date_modified": false, - "float": false, - "file_editor": false, - "timezone": "America/Los_Angeles", - - "google_analytics": false, - "piwik_analytics": false, - "piwik_analytics_id": "0" -} diff --git a/global.json b/global.json index 30b6e2e..d1b9c10 100644 --- a/global.json +++ b/global.json @@ -1,4 +1,32 @@ { "docs_directory": "docs", - "valid_markdown_extensions": ["md", "markdown"] + "valid_markdown_extensions": ["md", "markdown"], + + "title": "My Project", + "tagline": "My Stylish Documentation", + "author": "I, Me & Myself", + "image": "", + "repo": "", + "twitter": [], + "links": { + }, + + "languages": {}, + "theme": "daux-blue", + "ignore": { + "files": [], + "folders": [] + }, + + "breadcrumbs": false, + "clean_urls": false, + "toggle_code": false, + "date_modified": false, + "float": false, + "file_editor": false, + "timezone": "America/Los_Angeles", + + "google_analytics": false, + "piwik_analytics": false, + "piwik_analytics_id": "0" } diff --git a/libs/Config.php b/libs/Config.php new file mode 100644 index 0000000..a32aabb --- /dev/null +++ b/libs/Config.php @@ -0,0 +1,20 @@ + $value) { + if (array_key_exists($key, $this) && $override == false) { + continue; + } + + $this[$key] = $value; + } + } + + public function conservativeMerge($newValues) { + $this->merge($newValues, false); + } +} diff --git a/libs/Daux.php b/libs/Daux.php index 4b885b2..02ca792 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -15,6 +15,9 @@ class Daux * @var Tree\Entry */ public $tree; + /** + * @var Config + */ public $options; private $mode; @@ -30,48 +33,48 @@ class Daux define("DS", DIRECTORY_SEPARATOR); } - public function initialize($global_config_file = null, $override_file = 'config.json') + public function initialize($override_file = 'config.json') { - $this->loadConfig($global_config_file); + //global.json (docs dir, markdown files) + $this->loadConfig(); + + //config.json $this->loadConfigOverrides($override_file); + $this->generateTree(); } - private function loadConfig($global_config_file) + private function loadConfig() { - if (is_null($global_config_file)) { - $global_config_file = $this->local_base . DS . 'global.json'; - } + $default_config = [ + 'docs_directory' => 'docs', + 'valid_markdown_extensions' => ['md', 'markdown'] + ]; + + $global_config_file = $this->local_base . DS . 'global.json'; + if (!file_exists($global_config_file)) { throw new Exception('The Global Config file is missing. Requested File : ' . $global_config_file); } - $global_config = json_decode(file_get_contents($global_config_file), true); - if (!isset($global_config)) { + $default_config = array_merge($default_config, json_decode(file_get_contents($global_config_file), true)); + if (!isset($default_config)) { throw new Exception('The Global Config file is corrupt. Check that the JSON encoding is correct'); } - if (!isset($global_config['docs_directory'])) { - throw new Exception('The Global Config file does not have the docs directory set.'); - } - - $this->docs_path = $global_config['docs_directory']; + $this->docs_path = $default_config['docs_directory']; if (!is_dir($this->docs_path) && !is_dir($this->docs_path = $this->local_base . DS . $this->docs_path)) { throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path); } - if (!isset($global_config['valid_markdown_extensions'])) { - static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown'); - } else { - static::$VALID_MARKDOWN_EXTENSIONS = $global_config['valid_markdown_extensions']; - } + static::$VALID_MARKDOWN_EXTENSIONS = $default_config['valid_markdown_extensions']; + + $this->options = new Config(); + $this->options->merge($default_config); } private function loadConfigOverrides($override_file) { - // Read main configuration - $this->options = json_decode(file_get_contents($this->local_base . DS . 'default.json'), true); - // Read documentation overrides $config_file = $this->docs_path . DS . 'config.json'; if (file_exists($config_file)) { @@ -79,7 +82,7 @@ class Daux if (!isset($config)) { throw new Exception('The local config file is missing. Check path : ' . $config_file); } - $this->options = array_merge($this->options, $config); + $this->options->merge($config); } // Read command line overrides @@ -89,7 +92,7 @@ class Daux if (!isset($config)) { throw new Exception('The local config file is missing. Check path : ' . $config_file); } - $this->options = array_merge($this->options, $config); + $this->options->merge($config); } if (isset($this->options['timezone'])) { @@ -115,7 +118,7 @@ class Daux */ public function getParams() { - $params = $this->options += array( + $default = [ //Features 'multilanguage' => !empty($this->options['languages']), @@ -125,23 +128,24 @@ class Daux 'local_base' => $this->local_base, 'docs_path' => $this->docs_path, 'templates' => $this->local_base . DS . 'templates', - ); + ]; + $this->options->conservativeMerge($default); if ($this->tree) { - $params['tree'] = $this->tree; - $params['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage(); - if ($params['multilanguage']) { + $this->options['tree'] = $this->tree; + $this->options['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage(); + if ($this->options['multilanguage']) { foreach ($this->options['languages'] as $key => $name) { - $params['entry_page'][$key] = $this->tree->value[$key]->getFirstPage(); + $this->options['entry_page'][$key] = $this->tree->value[$key]->getFirstPage(); } } else { - $params['entry_page'] = $this->tree->getFirstPage(); + $this->options['entry_page'] = $this->tree->getFirstPage(); } } - $params['index_key'] = 'index.html'; - $params['base_page'] = $params['base_url'] = ''; + $this->options['index_key'] = 'index.html'; + $this->options['base_page'] = $this->options['base_url'] = ''; - return $params; + return $this->options; } } diff --git a/libs/Format/Base/MarkdownPage.php b/libs/Format/Base/MarkdownPage.php index 0481651..e844b19 100644 --- a/libs/Format/Base/MarkdownPage.php +++ b/libs/Format/Base/MarkdownPage.php @@ -1,6 +1,7 @@ file = $file; } - public function setParams(array $params) + public function setParams(Config $params) { $this->params = $params; } diff --git a/libs/Format/Confluence/Generator.php b/libs/Format/Confluence/Generator.php index ff839c7..d690ce6 100644 --- a/libs/Format/Confluence/Generator.php +++ b/libs/Format/Confluence/Generator.php @@ -1,5 +1,6 @@ $this->prefix . $tree->getTitle()]; $params['base_url'] = $params['base_page'] = $base_url; diff --git a/libs/Generator/Generator.php b/libs/Generator/Generator.php index 859a719..46dd8c8 100644 --- a/libs/Generator/Generator.php +++ b/libs/Generator/Generator.php @@ -9,7 +9,7 @@ class Generator public function generate($options) { $daux = new Daux(Daux::STATIC_MODE); - $daux->initialize(null, $options['config']); + $daux->initialize($options['config']); switch(strtolower($options['format'])) { case 'confluence': @@ -19,6 +19,5 @@ class Generator default: (new HTMLGenerator())->generate($daux, $options['destination']); } - } }