2015-04-21 17:11:43 +02:00
|
|
|
<?php namespace Todaymade\Daux;
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2015-07-17 18:34:00 +02:00
|
|
|
use Symfony\Component\Console\Output\NullOutput;
|
2015-05-18 14:26:29 +02:00
|
|
|
use Todaymade\Daux\Tree\Builder;
|
2015-04-22 12:23:57 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
class Daux
|
|
|
|
{
|
|
|
|
const STATIC_MODE = 'DAUX_STATIC';
|
|
|
|
const LIVE_MODE = 'DAUX_LIVE';
|
|
|
|
|
|
|
|
public static $VALID_MARKDOWN_EXTENSIONS;
|
|
|
|
public $local_base;
|
2015-07-14 23:35:47 +02:00
|
|
|
public $internal_base;
|
2015-04-23 00:32:30 +02:00
|
|
|
private $docs_path;
|
2015-04-27 12:47:10 +02:00
|
|
|
|
2015-07-17 18:34:00 +02:00
|
|
|
/**
|
|
|
|
* @var Processor
|
|
|
|
*/
|
|
|
|
protected $processor;
|
|
|
|
|
2015-04-27 12:47:10 +02:00
|
|
|
/**
|
2015-07-17 23:38:06 +02:00
|
|
|
* @var Tree\Directory
|
2015-04-27 12:47:10 +02:00
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public $tree;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
2015-06-29 16:11:01 +02:00
|
|
|
/**
|
|
|
|
* @var Config
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public $options;
|
|
|
|
private $mode;
|
|
|
|
|
|
|
|
public function __construct($mode)
|
2014-07-12 12:31:57 +02:00
|
|
|
{
|
2015-04-23 00:32:30 +02:00
|
|
|
$this->mode = $mode;
|
|
|
|
|
2015-07-14 23:35:47 +02:00
|
|
|
$this->local_base = $this->internal_base = dirname(__DIR__);
|
|
|
|
|
|
|
|
if (defined('PHAR_DIR')) {
|
|
|
|
$this->local_base = PHAR_DIR;
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
public static function initConstants()
|
|
|
|
{
|
|
|
|
define("DS", DIRECTORY_SEPARATOR);
|
|
|
|
}
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2015-06-29 16:11:01 +02:00
|
|
|
public function initialize($override_file = 'config.json')
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-06-29 16:11:01 +02:00
|
|
|
//global.json (docs dir, markdown files)
|
|
|
|
$this->loadConfig();
|
|
|
|
|
|
|
|
//config.json
|
2015-05-18 14:26:29 +02:00
|
|
|
$this->loadConfigOverrides($override_file);
|
2015-06-29 16:11:01 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
$this->generateTree();
|
|
|
|
}
|
2014-11-20 10:19:21 +01:00
|
|
|
|
2015-06-29 16:11:01 +02:00
|
|
|
private function loadConfig()
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-06-29 16:11:01 +02:00
|
|
|
$default_config = [
|
|
|
|
'docs_directory' => 'docs',
|
|
|
|
'valid_markdown_extensions' => ['md', 'markdown']
|
|
|
|
];
|
|
|
|
|
|
|
|
$global_config_file = $this->local_base . DS . 'global.json';
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
if (!file_exists($global_config_file)) {
|
|
|
|
throw new Exception('The Global Config file is missing. Requested File : ' . $global_config_file);
|
|
|
|
}
|
2014-11-20 10:19:21 +01:00
|
|
|
|
2015-06-29 16:11:01 +02:00
|
|
|
$default_config = array_merge($default_config, json_decode(file_get_contents($global_config_file), true));
|
|
|
|
if (!isset($default_config)) {
|
2015-04-23 00:32:30 +02:00
|
|
|
throw new Exception('The Global Config file is corrupt. Check that the JSON encoding is correct');
|
|
|
|
}
|
2014-11-20 10:19:21 +01:00
|
|
|
|
2015-06-29 16:11:01 +02:00
|
|
|
$this->docs_path = $default_config['docs_directory'];
|
2015-04-23 00:32:30 +02:00
|
|
|
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);
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 16:11:01 +02:00
|
|
|
static::$VALID_MARKDOWN_EXTENSIONS = $default_config['valid_markdown_extensions'];
|
|
|
|
|
|
|
|
$this->options = new Config();
|
|
|
|
$this->options->merge($default_config);
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* Load the configuration files, first, "config.json"
|
|
|
|
* in the documentation and then the file specified
|
|
|
|
* when running the configuration
|
|
|
|
*
|
|
|
|
* @param string $override_file
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-05-21 18:04:57 +02:00
|
|
|
private function loadConfigOverrides($override_file)
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-05-21 18:04:57 +02:00
|
|
|
// Read documentation overrides
|
|
|
|
$config_file = $this->docs_path . DS . 'config.json';
|
|
|
|
if (file_exists($config_file)) {
|
|
|
|
$config = json_decode(file_get_contents($config_file), true);
|
|
|
|
if (!isset($config)) {
|
|
|
|
throw new Exception('The local config file is missing. Check path : ' . $config_file);
|
|
|
|
}
|
2015-06-29 16:11:01 +02:00
|
|
|
$this->options->merge($config);
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2015-05-18 14:26:29 +02:00
|
|
|
|
2015-05-21 18:04:57 +02:00
|
|
|
// Read command line overrides
|
|
|
|
$config_file = $this->local_base . DS . $override_file;
|
2015-05-22 14:48:09 +02:00
|
|
|
if (!is_null($override_file) && file_exists($config_file)) {
|
2015-04-23 00:32:30 +02:00
|
|
|
$config = json_decode(file_get_contents($config_file), true);
|
|
|
|
if (!isset($config)) {
|
2015-05-21 18:04:57 +02:00
|
|
|
throw new Exception('The local config file is missing. Check path : ' . $config_file);
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
2015-06-29 16:11:01 +02:00
|
|
|
$this->options->merge($config);
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2015-05-18 14:26:29 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
if (isset($this->options['timezone'])) {
|
|
|
|
date_default_timezone_set($this->options['timezone']);
|
|
|
|
} elseif (!ini_get('date.timezone')) {
|
|
|
|
date_default_timezone_set('GMT');
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
private function generateTree()
|
|
|
|
{
|
|
|
|
$this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->getParams());
|
|
|
|
if (!empty($this->options['languages'])) {
|
|
|
|
foreach ($this->options['languages'] as $key => $node) {
|
|
|
|
$this->tree->value[$key]->title = $node;
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2015-05-22 15:05:16 +02:00
|
|
|
/**
|
2015-07-14 23:35:47 +02:00
|
|
|
* @return Config
|
2015-05-22 15:05:16 +02:00
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
public function getParams()
|
|
|
|
{
|
2015-06-29 16:11:01 +02:00
|
|
|
$default = [
|
2015-04-23 00:32:30 +02:00
|
|
|
//Features
|
|
|
|
'multilanguage' => !empty($this->options['languages']),
|
|
|
|
|
|
|
|
//Paths and tree
|
2015-04-23 15:47:05 +02:00
|
|
|
'theme-name' => $this->options['theme'],
|
2015-04-23 00:32:30 +02:00
|
|
|
'mode' => $this->mode,
|
|
|
|
'local_base' => $this->local_base,
|
|
|
|
'docs_path' => $this->docs_path,
|
2015-07-14 23:35:47 +02:00
|
|
|
'templates' => $this->internal_base . DS . 'templates',
|
2015-06-29 16:11:01 +02:00
|
|
|
];
|
|
|
|
$this->options->conservativeMerge($default);
|
2015-04-23 00:32:30 +02:00
|
|
|
|
|
|
|
if ($this->tree) {
|
2015-06-29 16:11:01 +02:00
|
|
|
$this->options['tree'] = $this->tree;
|
|
|
|
$this->options['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage();
|
|
|
|
if ($this->options['multilanguage']) {
|
2015-04-22 12:23:57 +02:00
|
|
|
foreach ($this->options['languages'] as $key => $name) {
|
2015-06-29 16:11:01 +02:00
|
|
|
$this->options['entry_page'][$key] = $this->tree->value[$key]->getFirstPage();
|
2015-04-22 12:23:57 +02:00
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
} else {
|
2015-06-29 16:11:01 +02:00
|
|
|
$this->options['entry_page'] = $this->tree->getFirstPage();
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 16:11:01 +02:00
|
|
|
$this->options['index_key'] = 'index.html';
|
|
|
|
$this->options['base_page'] = $this->options['base_url'] = '';
|
2015-04-22 12:23:57 +02:00
|
|
|
|
2015-06-29 16:11:01 +02:00
|
|
|
return $this->options;
|
2015-04-22 12:23:57 +02:00
|
|
|
}
|
2015-07-17 18:34:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Processor
|
|
|
|
*/
|
|
|
|
public function getProcessor()
|
|
|
|
{
|
|
|
|
if (!$this->processor) {
|
|
|
|
$this->processor = new Processor($this, new NullOutput(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->processor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Processor $processor
|
|
|
|
*/
|
|
|
|
public function setProcessor(Processor $processor)
|
|
|
|
{
|
|
|
|
$this->processor = $processor;
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|