#171 Redo the configuration layer to add a ConfigBuilder and group configuration parts
This commit is contained in:
200
libs/Daux.php
200
libs/Daux.php
@ -15,18 +15,13 @@ class Daux
|
||||
|
||||
public static $output;
|
||||
|
||||
/** @var string */
|
||||
public $local_base;
|
||||
|
||||
/** @var \Todaymade\Daux\Format\Base\Generator */
|
||||
protected $generator;
|
||||
|
||||
/** @var ContentTypeHandler */
|
||||
protected $typeHandler;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
/** @var string[] */
|
||||
protected $validExtensions;
|
||||
|
||||
/** @var Processor */
|
||||
@ -36,155 +31,17 @@ class Daux
|
||||
public $tree;
|
||||
|
||||
/** @var Config */
|
||||
public $options;
|
||||
|
||||
/** @var string */
|
||||
private $mode;
|
||||
public $config;
|
||||
|
||||
/** @var bool */
|
||||
private $merged_tree = false;
|
||||
|
||||
/**
|
||||
* @param string $mode
|
||||
*/
|
||||
public function __construct($mode, OutputInterface $output)
|
||||
public function __construct(Config $config, OutputInterface $output)
|
||||
{
|
||||
Daux::$output = $output;
|
||||
$this->mode = $mode;
|
||||
|
||||
$this->local_base = dirname(__DIR__);
|
||||
|
||||
// global.json
|
||||
$this->loadBaseConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $override_file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initializeConfiguration($override_file = null)
|
||||
{
|
||||
$params = $this->getParams();
|
||||
|
||||
// Validate and set theme path
|
||||
$params->setDocumentationDirectory(
|
||||
$docs_path = $this->normalizeDocumentationPath($this->getParams()->getDocumentationDirectory())
|
||||
);
|
||||
|
||||
// Read documentation overrides
|
||||
$this->loadConfiguration($docs_path . DIRECTORY_SEPARATOR . 'config.json');
|
||||
|
||||
// Read command line overrides
|
||||
$override_file = $this->getConfigurationOverride($override_file);
|
||||
if ($override_file !== null) {
|
||||
$params->setConfigurationOverrideFile($override_file);
|
||||
$this->loadConfiguration($override_file);
|
||||
}
|
||||
|
||||
// Validate and set theme path
|
||||
$params->setThemesPath($this->normalizeThemePath($params->getThemesDirectory()));
|
||||
|
||||
// Set a valid default timezone
|
||||
if ($params->hasTimezone()) {
|
||||
date_default_timezone_set($params->getTimezone());
|
||||
} elseif (!ini_get('date.timezone')) {
|
||||
date_default_timezone_set('GMT');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file requested for configuration overrides
|
||||
*
|
||||
* @param string|null $path
|
||||
* @return string|null the path to a file to load for configuration overrides
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConfigurationOverride($path)
|
||||
{
|
||||
$validPath = DauxHelper::findLocation($path, $this->local_base, 'DAUX_CONFIGURATION', 'file');
|
||||
|
||||
if ($validPath === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$validPath) {
|
||||
throw new Exception('The configuration override file does not exist. Check the path again : ' . $path);
|
||||
}
|
||||
|
||||
return $validPath;
|
||||
}
|
||||
|
||||
public function normalizeThemePath($path)
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
return $validPath;
|
||||
|
||||
}
|
||||
|
||||
public function normalizeDocumentationPath($path)
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
return $validPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load and validate the global configuration
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function loadBaseConfiguration()
|
||||
{
|
||||
$this->options = new Config();
|
||||
|
||||
// Set the default configuration
|
||||
$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
|
||||
$this->loadConfiguration($this->local_base . DIRECTORY_SEPARATOR . 'global.json', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $config_file
|
||||
* @param bool $optional
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function loadConfiguration($config_file, $optional = true)
|
||||
{
|
||||
if (!file_exists($config_file)) {
|
||||
if ($optional) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception('The configuration file is missing. Check path : ' . $config_file);
|
||||
}
|
||||
|
||||
$config = json_decode(file_get_contents($config_file), true);
|
||||
if (!isset($config)) {
|
||||
throw new Exception('The configuration file "' . $config_file . '" is corrupt. Is your JSON well-formed ?');
|
||||
}
|
||||
$this->options->merge($config);
|
||||
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -192,14 +49,14 @@ class Daux
|
||||
*/
|
||||
public function generateTree()
|
||||
{
|
||||
$this->options['valid_content_extensions'] = $this->getContentExtensions();
|
||||
$this->config->setValidContentExtensions($this->getContentExtensions());
|
||||
|
||||
$this->tree = new Root($this->getParams());
|
||||
Builder::build($this->tree, $this->options['ignore']);
|
||||
$this->tree = new Root($this->getConfig());
|
||||
Builder::build($this->tree, $this->config->getIgnore());
|
||||
|
||||
// Apply the language name as Section title
|
||||
if ($this->options->isMultilanguage()) {
|
||||
foreach ($this->options['languages'] as $key => $node) {
|
||||
if ($this->config->isMultilanguage()) {
|
||||
foreach ($this->config->getLanguages() as $key => $node) {
|
||||
$this->tree->getEntries()[$key]->setTitle($node);
|
||||
}
|
||||
}
|
||||
@ -216,22 +73,34 @@ class Daux
|
||||
/**
|
||||
* @return Config
|
||||
*/
|
||||
public function getParams()
|
||||
public function getConfig()
|
||||
{
|
||||
if ($this->tree && !$this->merged_tree) {
|
||||
$this->options['tree'] = $this->tree;
|
||||
$this->options['index'] = $this->tree->getIndexPage() ?: $this->tree->getFirstPage();
|
||||
if ($this->options->isMultilanguage()) {
|
||||
foreach ($this->options['languages'] as $key => $name) {
|
||||
$this->options['entry_page'][$key] = $this->tree->getEntries()[$key]->getFirstPage();
|
||||
$this->config->setTree($this->tree);
|
||||
$this->config->setIndex($this->tree->getIndexPage() ?: $this->tree->getFirstPage());
|
||||
$entry_page = null;
|
||||
if ($this->config->isMultilanguage()) {
|
||||
$entry_page = [];
|
||||
foreach ($this->config->getLanguages() as $key => $name) {
|
||||
$entry_page[$key] = $this->tree->getEntries()[$key]->getFirstPage();
|
||||
}
|
||||
} else {
|
||||
$this->options['entry_page'] = $this->tree->getFirstPage();
|
||||
$entry_page= $this->tree->getFirstPage();
|
||||
}
|
||||
$this->config->setEntryPage($entry_page);
|
||||
$this->merged_tree = true;
|
||||
}
|
||||
|
||||
return $this->options;
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Config
|
||||
* @deprecated Use getConfig instead
|
||||
*/
|
||||
public function getParams()
|
||||
{
|
||||
return $this->getConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -254,9 +123,9 @@ class Daux
|
||||
$this->processor = $processor;
|
||||
|
||||
// This is not the cleanest but it's
|
||||
// the best i've found to use the
|
||||
// the best I've found to use the
|
||||
// processor in very remote places
|
||||
$this->options['processor_instance'] = $processor;
|
||||
$this->config->setProcessorInstance($processor);
|
||||
}
|
||||
|
||||
public function getGenerators()
|
||||
@ -272,10 +141,9 @@ class Daux
|
||||
return array_replace($default, $extended);
|
||||
}
|
||||
|
||||
|
||||
public function getProcessorClass()
|
||||
{
|
||||
$processor = $this->getParams()['processor'];
|
||||
$processor = $this->getConfig()->getProcessor();
|
||||
|
||||
if (empty($processor)) {
|
||||
return null;
|
||||
@ -318,7 +186,7 @@ class Daux
|
||||
|
||||
$generators = $this->getGenerators();
|
||||
|
||||
$format = $this->getParams()->getFormat();
|
||||
$format = $this->getConfig()->getFormat();
|
||||
|
||||
if (!array_key_exists($format, $generators)) {
|
||||
$message = "The format '$format' doesn't exist, did you forget to set your processor ?";
|
||||
|
Reference in New Issue
Block a user