daux.io/libs/ConfigBuilder.php

328 lignes
8.5 KiB
PHP
Brut Lien permanent Vue normale Historique

<?php namespace Todaymade\Daux;
class ConfigBuilder
{
/** @var Config */
private $config;
2020-04-24 22:22:57 +02:00
/** @var array */
private $overrideValues = [];
2020-04-22 22:24:52 +02:00
private $configuration_override_file;
2020-04-22 21:55:53 +02:00
private function __construct(string $mode)
{
$this->config = new Config();
$this->config['mode'] = $mode;
$this->config['local_base'] = dirname(__DIR__);
}
2020-04-22 21:55:53 +02:00
public static function fromFile($file): Config
{
return unserialize(file_get_contents($file));
}
2020-04-22 21:55:53 +02:00
public static function withMode($mode = Daux::STATIC_MODE): ConfigBuilder
{
$builder = new ConfigBuilder($mode);
$builder->loadBaseConfiguration();
2020-04-22 22:24:52 +02:00
return $builder;
}
2020-04-22 21:55:53 +02:00
public function with(array $values): ConfigBuilder
{
$this->config->merge($values);
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-24 22:22:57 +02:00
private function setValue(Config $array, $key, $value)
2020-04-22 21:55:53 +02:00
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
while (count($keys) > 1) {
$key = array_shift($keys);
if (!isset($array[$key]) || !is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$array[array_shift($keys)] = $value;
}
2020-04-22 21:55:53 +02:00
public function withValues(array $values): ConfigBuilder
{
2020-04-24 22:22:57 +02:00
$this->overrideValues = $values;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function withDocumentationDirectory($dir): ConfigBuilder
{
$this->config['docs_directory'] = $dir;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function withValidContentExtensions(array $value): ConfigBuilder
{
$this->config['valid_content_extensions'] = $value;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function withThemesPath($themePath): ConfigBuilder
{
$this->config['themes_path'] = $themePath;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function withThemesDirectory($directory): ConfigBuilder
{
$this->config['themes_directory'] = $directory;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function withCache(bool $value): ConfigBuilder
{
$this->config['cache'] = $value;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function withFormat($format): ConfigBuilder
{
$this->config['format'] = $format;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function withConfigurationOverride($file): ConfigBuilder
{
$this->configuration_override_file = $file;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function withProcessor($value): ConfigBuilder
{
$this->config['processor'] = $value;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function withConfluenceDelete($value): ConfigBuilder
{
$this->config['confluence']['delete'] = $value;
2020-04-22 22:24:52 +02:00
return $this;
}
2020-04-22 21:55:53 +02:00
public function build(): Config
{
$this->initializeConfiguration();
2020-04-24 22:22:57 +02:00
foreach ($this->overrideValues as $value) {
$this->setValue($this->config, $value[0], $value[1]);
}
return $this->config;
}
2020-04-22 21:55:53 +02:00
private function resolveThemeVariant()
{
$theme = $this->config->getHTML()->getTheme();
$themesPath = $this->config->getThemesPath() . DIRECTORY_SEPARATOR;
// If theme directory exists, we're good with that
if (is_dir(realpath(($themesPath . $theme)))) {
return [$theme, ''];
}
$themePieces = explode('-', $theme);
$variant = '';
// Do we have a variant or only a theme ?
if (count($themePieces) > 1) {
$variant = array_pop($themePieces);
$theme = implode('-', $themePieces);
}
if (!is_dir(realpath($themesPath . $theme))) {
throw new \RuntimeException("Theme '{$theme}' not found");
}
return [$theme, $variant];
}
/**
* @param string $override_file
2020-04-22 22:24:52 +02:00
*
* @throws Exception
*/
2020-04-22 21:55:53 +02:00
private function initializeConfiguration()
{
// Validate and set theme path
$docs_path = $this->normalizeDocumentationPath($this->config->getDocumentationDirectory());
$this->config['docs_directory'] = $docs_path;
// Read documentation overrides
$this->loadConfiguration($docs_path . DIRECTORY_SEPARATOR . 'config.json');
// Read command line overrides
$override_file = $this->getConfigurationOverride($this->configuration_override_file);
if ($override_file !== null) {
$this->loadConfiguration($override_file);
}
// Validate and set theme path
$this->withThemesPath($this->normalizeThemePath($this->config->getThemesDirectory()));
// Resolve variant once
$theme = $this->resolveThemeVariant();
$this->config['html']['theme'] = $theme[0];
$this->config['html']['theme-variant'] = $theme[1];
// Set a valid default timezone
if ($this->config->hasTimezone()) {
date_default_timezone_set($this->config->getTimezone());
} elseif (!ini_get('date.timezone')) {
date_default_timezone_set('GMT');
}
// Text search would be too slow on live server
if ($this->config->isLive()) {
$this->config['html']['search'] = false;
2020-04-22 21:55:53 +02:00
}
}
2020-04-22 21:55:53 +02:00
private function normalizeThemePath($path)
{
$validPath = $this->findLocation($path, $this->config->getLocalBase(), 'dir');
if (!$validPath) {
throw new Exception('The Themes directory does not exist. Check the path again : ' . $path);
}
return $validPath;
}
2020-04-22 21:55:53 +02:00
private function normalizeDocumentationPath($path)
{
$validPath = $this->findLocation($path, $this->config->getLocalBase(), 'dir');
if (!$validPath) {
throw new Exception('The Docs directory does not exist. Check the path again : ' . $path);
}
return $validPath;
}
/**
2020-04-22 22:24:52 +02:00
* Load and validate the global configuration.
*
* @throws Exception
*/
2020-04-22 21:55:53 +02:00
private function loadBaseConfiguration()
{
// Set the default configuration
$this->config->merge([
'docs_directory' => 'docs',
'valid_content_extensions' => ['md', 'markdown'],
//Paths and tree
'templates' => 'templates',
'base_url' => '',
]);
// Load the global configuration
$this->loadConfiguration($this->config->getLocalBase() . DIRECTORY_SEPARATOR . 'global.json', false);
}
/**
* @param string $config_file
* @param bool $optional
2020-04-22 22:24:52 +02:00
*
* @throws Exception
*/
2020-04-22 21:55:53 +02:00
private 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->config->merge($config);
}
/**
2020-04-22 22:24:52 +02:00
* Get the file requested for configuration overrides.
*
* @param null|string $path
*
* @throws Exception
2020-04-22 22:24:52 +02:00
*
* @return null|string the path to a file to load for configuration overrides
*/
2020-04-22 21:55:53 +02:00
private function getConfigurationOverride($path)
{
$validPath = $this->findLocation($path, $this->config->getLocalBase(), '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;
}
/**
2020-04-22 22:24:52 +02:00
* @param null|string $path
* @param string $basedir
2019-12-07 11:22:40 +01:00
* @param string $type
2020-04-22 22:24:52 +02:00
*
* @return null|false|string
*/
2020-04-22 21:55:53 +02:00
private function findLocation($path, $basedir, $type)
{
// If Path is explicitly null, it's useless to go further
2019-12-07 16:32:38 +01:00
if ($path === null) {
return null;
}
// VFS, used only in tests
2020-04-22 22:24:52 +02:00
if (substr($path, 0, 6) == 'vfs://') {
return $path;
}
// Check if it's relative to the current directory or an absolute path
if (DauxHelper::is($path, $type)) {
return DauxHelper::getAbsolutePath($path);
}
// Check if it exists relative to Daux's root
$newPath = $basedir . DIRECTORY_SEPARATOR . $path;
if (DauxHelper::is($newPath, $type)) {
return $newPath;
}
return false;
}
2020-04-22 21:55:53 +02:00
}