2015-04-21 17:11:43 +02:00
|
|
|
<?php namespace Todaymade\Daux;
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2015-04-22 18:24:10 +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;
|
|
|
|
public $base_url = '';
|
|
|
|
public $host;
|
|
|
|
private $docs_path;
|
|
|
|
public $tree;
|
|
|
|
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;
|
|
|
|
|
|
|
|
$this->local_base = dirname(__DIR__);
|
|
|
|
$this->base_url = '';
|
|
|
|
|
|
|
|
if ($this->mode == Daux::LIVE_MODE) {
|
|
|
|
$this->host = $_SERVER['HTTP_HOST'];
|
|
|
|
$this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
|
|
|
|
$t = strrpos($this->base_url, '/index.php');
|
|
|
|
if ($t != false) {
|
|
|
|
$this->base_url = substr($this->base_url, 0, $t);
|
|
|
|
}
|
|
|
|
if (substr($this->base_url, -1) !== '/') {
|
|
|
|
$this->base_url .= '/';
|
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
|
|
|
public static function initConstants()
|
|
|
|
{
|
|
|
|
define("DS", DIRECTORY_SEPARATOR);
|
|
|
|
}
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
public function initialize($global_config_file = null, $config_file = 'config.json')
|
|
|
|
{
|
|
|
|
$this->loadConfig($global_config_file);
|
|
|
|
$this->loadConfigOverrides($config_file);
|
|
|
|
$this->generateTree();
|
|
|
|
}
|
2014-11-20 10:19:21 +01:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
private function loadConfig($global_config_file)
|
|
|
|
{
|
|
|
|
if (is_null($global_config_file)) {
|
|
|
|
$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);
|
|
|
|
}
|
2014-11-20 10:19:21 +01:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
$global_config = json_decode(file_get_contents($global_config_file), true);
|
|
|
|
if (!isset($global_config)) {
|
|
|
|
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-04-23 00:32:30 +02:00
|
|
|
if (!isset($global_config['docs_directory'])) {
|
|
|
|
throw new Exception('The Global Config file does not have the docs directory set.');
|
|
|
|
}
|
2014-11-20 10:19:21 +01:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
$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)) {
|
|
|
|
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-04-23 00:32:30 +02:00
|
|
|
if (!isset($global_config['valid_markdown_extensions'])) {
|
|
|
|
static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown');
|
|
|
|
} else {
|
|
|
|
static::$VALID_MARKDOWN_EXTENSIONS = $global_config['valid_markdown_extensions'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadConfigOverrides($config_file)
|
|
|
|
{
|
|
|
|
$config_file = $this->docs_path . DS . $config_file;
|
|
|
|
if (!file_exists($config_file)) {
|
|
|
|
throw new Exception('The local config file is missing. Check path : ' . $config_file);
|
|
|
|
}
|
|
|
|
$this->options = json_decode(file_get_contents($this->local_base . DS . 'default.json'), true);
|
|
|
|
if (is_file($config_file)) {
|
|
|
|
$config = json_decode(file_get_contents($config_file), true);
|
|
|
|
if (!isset($config)) {
|
|
|
|
throw new Exception('There was an error parsing the Config file. Please review');
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
$this->options = array_merge($this->options, $config);
|
|
|
|
}
|
|
|
|
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-04-23 00:32:30 +02:00
|
|
|
public function getParams()
|
|
|
|
{
|
2015-04-23 10:24:50 +02:00
|
|
|
$params = $this->options += array(
|
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-04-23 10:24:50 +02:00
|
|
|
'templates' => $this->local_base . DS . 'templates',
|
2015-04-23 00:32:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->tree) {
|
|
|
|
$params['tree'] = $this->tree;
|
|
|
|
$params['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage();
|
|
|
|
if ($params['multilanguage']) {
|
2015-04-22 12:23:57 +02:00
|
|
|
foreach ($this->options['languages'] as $key => $name) {
|
2015-04-23 00:32:30 +02:00
|
|
|
$params['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-04-23 00:32:30 +02:00
|
|
|
$params['entry_page'] = $this->tree->getFirstPage();
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
if ($this->mode == self::LIVE_MODE) {
|
2015-04-22 12:23:57 +02:00
|
|
|
$params['index_key'] = 'index';
|
|
|
|
$params['host'] = $this->host;
|
2015-04-22 18:24:10 +02:00
|
|
|
$params['base_page'] = $params['base_url'] = '//' . $this->base_url;
|
2015-04-23 00:32:30 +02:00
|
|
|
if (!$this->options['clean_urls']) {
|
|
|
|
$params['base_page'] .= 'index.php/';
|
|
|
|
}
|
2015-04-22 12:23:57 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
if ($params['image'] !== '') {
|
|
|
|
$params['image'] = str_replace('<base_url>', $params['base_url'], $params['image']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$params['index_key'] = 'index.html';
|
|
|
|
$params['base_page'] = $params['base_url'] = '';
|
|
|
|
}
|
2015-04-22 12:23:57 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
$params['theme'] = DauxHelper::getTheme(
|
2015-04-23 21:04:00 +02:00
|
|
|
$this->options['theme-name'],
|
2015-04-23 00:32:30 +02:00
|
|
|
$params['base_url'],
|
|
|
|
$this->local_base,
|
2015-04-23 21:04:00 +02:00
|
|
|
$params['base_url']
|
2015-04-23 00:32:30 +02:00
|
|
|
);
|
2015-04-22 12:23:57 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
return $params;
|
2015-04-22 12:23:57 +02:00
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|