Remove "default.json" concentrate all configurations in "global.json"

Set configurations in an Object instead of an array
This commit is contained in:
Stéphane Goetz 2015-06-29 16:11:01 +02:00 committed by Stéphane Goetz
parent 100568bfbb
commit 60b50919b4
7 changed files with 92 additions and 68 deletions

View File

@ -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"
}

View File

@ -1,4 +1,32 @@
{ {
"docs_directory": "docs", "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"
} }

20
libs/Config.php Normal file
View File

@ -0,0 +1,20 @@
<?php namespace Todaymade\Daux;
use ArrayObject;
class Config extends ArrayObject {
public function merge($newValues, $override = true) {
foreach ($newValues as $key => $value) {
if (array_key_exists($key, $this) && $override == false) {
continue;
}
$this[$key] = $value;
}
}
public function conservativeMerge($newValues) {
$this->merge($newValues, false);
}
}

View File

@ -15,6 +15,9 @@ class Daux
* @var Tree\Entry * @var Tree\Entry
*/ */
public $tree; public $tree;
/**
* @var Config
*/
public $options; public $options;
private $mode; private $mode;
@ -30,48 +33,48 @@ class Daux
define("DS", DIRECTORY_SEPARATOR); 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->loadConfigOverrides($override_file);
$this->generateTree(); $this->generateTree();
} }
private function loadConfig($global_config_file) private function loadConfig()
{ {
if (is_null($global_config_file)) { $default_config = [
$global_config_file = $this->local_base . DS . 'global.json'; 'docs_directory' => 'docs',
} 'valid_markdown_extensions' => ['md', 'markdown']
];
$global_config_file = $this->local_base . DS . 'global.json';
if (!file_exists($global_config_file)) { if (!file_exists($global_config_file)) {
throw new Exception('The Global Config file is missing. Requested File : ' . $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); $default_config = array_merge($default_config, json_decode(file_get_contents($global_config_file), true));
if (!isset($global_config)) { if (!isset($default_config)) {
throw new Exception('The Global Config file is corrupt. Check that the JSON encoding is correct'); throw new Exception('The Global Config file is corrupt. Check that the JSON encoding is correct');
} }
if (!isset($global_config['docs_directory'])) { $this->docs_path = $default_config['docs_directory'];
throw new Exception('The Global Config file does not have the docs directory set.');
}
$this->docs_path = $global_config['docs_directory'];
if (!is_dir($this->docs_path) && !is_dir($this->docs_path = $this->local_base . DS . $this->docs_path)) { 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); 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 = $default_config['valid_markdown_extensions'];
static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown');
} else { $this->options = new Config();
static::$VALID_MARKDOWN_EXTENSIONS = $global_config['valid_markdown_extensions']; $this->options->merge($default_config);
}
} }
private function loadConfigOverrides($override_file) 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 // Read documentation overrides
$config_file = $this->docs_path . DS . 'config.json'; $config_file = $this->docs_path . DS . 'config.json';
if (file_exists($config_file)) { if (file_exists($config_file)) {
@ -79,7 +82,7 @@ class Daux
if (!isset($config)) { if (!isset($config)) {
throw new Exception('The local config file is missing. Check path : ' . $config_file); 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 // Read command line overrides
@ -89,7 +92,7 @@ class Daux
if (!isset($config)) { if (!isset($config)) {
throw new Exception('The local config file is missing. Check path : ' . $config_file); 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'])) { if (isset($this->options['timezone'])) {
@ -115,7 +118,7 @@ class Daux
*/ */
public function getParams() public function getParams()
{ {
$params = $this->options += array( $default = [
//Features //Features
'multilanguage' => !empty($this->options['languages']), 'multilanguage' => !empty($this->options['languages']),
@ -125,23 +128,24 @@ class Daux
'local_base' => $this->local_base, 'local_base' => $this->local_base,
'docs_path' => $this->docs_path, 'docs_path' => $this->docs_path,
'templates' => $this->local_base . DS . 'templates', 'templates' => $this->local_base . DS . 'templates',
); ];
$this->options->conservativeMerge($default);
if ($this->tree) { if ($this->tree) {
$params['tree'] = $this->tree; $this->options['tree'] = $this->tree;
$params['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage(); $this->options['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage();
if ($params['multilanguage']) { if ($this->options['multilanguage']) {
foreach ($this->options['languages'] as $key => $name) { 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 { } else {
$params['entry_page'] = $this->tree->getFirstPage(); $this->options['entry_page'] = $this->tree->getFirstPage();
} }
} }
$params['index_key'] = 'index.html'; $this->options['index_key'] = 'index.html';
$params['base_page'] = $params['base_url'] = ''; $this->options['base_page'] = $this->options['base_url'] = '';
return $params; return $this->options;
} }
} }

View File

@ -1,6 +1,7 @@
<?php namespace Todaymade\Daux\Format\Base; <?php namespace Todaymade\Daux\Format\Base;
use League\CommonMark\CommonMarkConverter; use League\CommonMark\CommonMarkConverter;
use Todaymade\Daux\Config;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
abstract class MarkdownPage extends SimplePage abstract class MarkdownPage extends SimplePage
@ -11,7 +12,7 @@ abstract class MarkdownPage extends SimplePage
protected $file; protected $file;
/** /**
* @var array * @var Config
*/ */
protected $params; protected $params;
@ -25,7 +26,7 @@ abstract class MarkdownPage extends SimplePage
$this->file = $file; $this->file = $file;
} }
public function setParams(array $params) public function setParams(Config $params)
{ {
$this->params = $params; $this->params = $params;
} }

View File

@ -1,5 +1,6 @@
<?php namespace Todaymade\Daux\Format\Confluence; <?php namespace Todaymade\Daux\Format\Confluence;
use Todaymade\Daux\Config;
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
use Todaymade\Daux\Tree\Directory; use Todaymade\Daux\Tree\Directory;
@ -31,7 +32,7 @@ class Generator
echo "Done !\n"; echo "Done !\n";
} }
private function generateRecursive(Entry $tree, array $params, $base_url = '') private function generateRecursive(Entry $tree, Config $params, $base_url = '')
{ {
$final = ['title' => $this->prefix . $tree->getTitle()]; $final = ['title' => $this->prefix . $tree->getTitle()];
$params['base_url'] = $params['base_page'] = $base_url; $params['base_url'] = $params['base_page'] = $base_url;

View File

@ -9,7 +9,7 @@ class Generator
public function generate($options) public function generate($options)
{ {
$daux = new Daux(Daux::STATIC_MODE); $daux = new Daux(Daux::STATIC_MODE);
$daux->initialize(null, $options['config']); $daux->initialize($options['config']);
switch(strtolower($options['format'])) { switch(strtolower($options['format'])) {
case 'confluence': case 'confluence':
@ -19,6 +19,5 @@ class Generator
default: default:
(new HTMLGenerator())->generate($daux, $options['destination']); (new HTMLGenerator())->generate($daux, $options['destination']);
} }
} }
} }