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-07-29 22:31:41 +02:00
|
|
|
use Todaymade\Daux\ContentTypes\ContentTypeHandler;
|
2015-05-18 14:26:29 +02:00
|
|
|
use Todaymade\Daux\Tree\Builder;
|
2015-10-28 00:18:29 +01:00
|
|
|
use Todaymade\Daux\Tree\Content;
|
|
|
|
use Todaymade\Daux\Tree\Directory;
|
2015-07-18 20:52:14 +02:00
|
|
|
use Todaymade\Daux\Tree\Root;
|
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';
|
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
public $local_base;
|
2015-07-18 21:45:38 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2015-07-14 23:35:47 +02:00
|
|
|
public $internal_base;
|
2015-07-18 21:45:38 +02:00
|
|
|
|
2015-07-28 17:25:03 +02:00
|
|
|
/** @var \Todaymade\Daux\Format\Base\Generator */
|
|
|
|
protected $generator;
|
|
|
|
|
2015-07-29 22:31:41 +02:00
|
|
|
/** @var ContentTypeHandler */
|
2015-07-28 17:25:03 +02:00
|
|
|
protected $typeHandler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $validExtensions;
|
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
private $docs_path;
|
2015-04-27 12:47:10 +02:00
|
|
|
|
2015-07-31 17:54:02 +02:00
|
|
|
/** @var string */
|
|
|
|
private $themes_path;
|
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/** @var Processor */
|
2015-07-17 18:34:00 +02:00
|
|
|
protected $processor;
|
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/** @var Tree\Root */
|
2015-04-23 00:32:30 +02:00
|
|
|
public $tree;
|
2015-07-17 23:38:06 +02:00
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/** @var Config */
|
2015-04-23 00:32:30 +02:00
|
|
|
public $options;
|
2015-07-18 21:45:38 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2015-04-23 00:32:30 +02:00
|
|
|
private $mode;
|
|
|
|
|
2015-07-19 14:05:12 +02:00
|
|
|
/** @var bool */
|
|
|
|
private $merged_defaults = false;
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private $merged_tree = false;
|
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/**
|
|
|
|
* @param string $mode
|
|
|
|
*/
|
2015-04-23 00:32:30 +02:00
|
|
|
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__);
|
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
// In case we're inside the phar archive
|
|
|
|
// we save the path to the directory
|
|
|
|
// in which it is contained
|
2015-07-14 23:35:47 +02:00
|
|
|
if (defined('PHAR_DIR')) {
|
|
|
|
$this->local_base = PHAR_DIR;
|
|
|
|
}
|
2015-07-28 17:26:35 +02:00
|
|
|
|
|
|
|
// global.json
|
|
|
|
$this->loadBaseConfiguration();
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/**
|
|
|
|
* @param string $override_file
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-07-28 17:26:35 +02:00
|
|
|
public function initializeConfiguration($override_file = 'config.json')
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-28 17:26:35 +02:00
|
|
|
// Read documentation overrides
|
|
|
|
$this->loadConfiguration($this->docs_path . DIRECTORY_SEPARATOR . 'config.json');
|
2015-07-20 15:59:52 +02:00
|
|
|
|
2015-07-28 17:26:35 +02:00
|
|
|
// Read command line overrides
|
|
|
|
if (!is_null($override_file)) {
|
|
|
|
$this->loadConfiguration($this->local_base . DIRECTORY_SEPARATOR . $override_file);
|
2015-07-20 15:59:52 +02:00
|
|
|
}
|
2015-06-29 16:11:01 +02:00
|
|
|
|
2015-07-20 15:59:52 +02:00
|
|
|
// Set a valid default timezone
|
|
|
|
if (isset($this->options['timezone'])) {
|
|
|
|
date_default_timezone_set($this->options['timezone']);
|
|
|
|
} elseif (!ini_get('date.timezone')) {
|
|
|
|
date_default_timezone_set('GMT');
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2014-11-20 10:19:21 +01:00
|
|
|
|
2015-07-31 17:54:02 +02:00
|
|
|
public function setThemesPath($path)
|
|
|
|
{
|
|
|
|
$this->themes_path = $path;
|
|
|
|
if (!is_dir($this->themes_path) &&
|
|
|
|
!is_dir($this->themes_path = $this->local_base . DIRECTORY_SEPARATOR . $this->themes_path)
|
|
|
|
) {
|
|
|
|
throw new Exception('The Themes directory does not exist. Check the path again : ' . $this->themes_path);
|
|
|
|
}
|
|
|
|
$this->options['themes_path'] = $this->themes_path;
|
2015-08-06 22:47:38 +02:00
|
|
|
$this->options['templates'] = 'templates';
|
2015-07-31 17:54:02 +02:00
|
|
|
}
|
|
|
|
|
2015-07-28 17:26:35 +02:00
|
|
|
public function setDocumentationPath($path)
|
|
|
|
{
|
|
|
|
$this->docs_path = $path;
|
|
|
|
if (!is_dir($this->docs_path) &&
|
|
|
|
!is_dir($this->docs_path = $this->local_base . DIRECTORY_SEPARATOR . $this->docs_path)
|
|
|
|
) {
|
|
|
|
throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path);
|
|
|
|
}
|
2015-07-31 12:53:22 +02:00
|
|
|
|
|
|
|
$this->options['docs_path'] = $this->docs_path;
|
2015-07-28 17:26:35 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/**
|
|
|
|
* Load and validate the global configuration
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-07-20 15:59:52 +02:00
|
|
|
protected function loadBaseConfiguration()
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-20 15:59:52 +02:00
|
|
|
$this->options = new Config();
|
|
|
|
|
|
|
|
// Set the default configuration
|
|
|
|
$this->options->merge([
|
2015-06-29 16:11:01 +02:00
|
|
|
'docs_directory' => 'docs',
|
2015-07-19 16:36:34 +02:00
|
|
|
'valid_content_extensions' => ['md', 'markdown']
|
2015-07-20 15:59:52 +02:00
|
|
|
]);
|
2014-11-20 10:19:21 +01:00
|
|
|
|
2015-07-20 15:59:52 +02:00
|
|
|
// Load the global configuration
|
|
|
|
$this->loadConfiguration($this->local_base . DIRECTORY_SEPARATOR . 'global.json', false);
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 15:59:52 +02:00
|
|
|
/**
|
|
|
|
* @param string $config_file
|
|
|
|
* @param bool $optional
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function loadConfiguration($config_file, $optional = true)
|
|
|
|
{
|
|
|
|
if (!file_exists($config_file)) {
|
|
|
|
if ($optional) {
|
|
|
|
return;
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
2015-07-20 15:59:52 +02:00
|
|
|
|
|
|
|
throw new Exception('The configuration file is missing. Check path : ' . $config_file);
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2015-05-18 14:26:29 +02:00
|
|
|
|
2015-07-20 15:59:52 +02:00
|
|
|
$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 ?');
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
2015-07-20 15:59:52 +02:00
|
|
|
$this->options->merge($config);
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|
2014-07-12 12:31:57 +02:00
|
|
|
|
2015-07-18 21:45:38 +02:00
|
|
|
/**
|
|
|
|
* Generate the tree that will be used
|
|
|
|
*/
|
2015-07-19 23:17:57 +02:00
|
|
|
public function generateTree()
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-28 17:25:03 +02:00
|
|
|
$this->options['valid_content_extensions'] = $this->getContentExtensions();
|
|
|
|
|
2015-07-18 23:13:02 +02:00
|
|
|
$this->tree = new Root($this->getParams(), $this->docs_path);
|
|
|
|
Builder::build($this->tree, $this->options['ignore']);
|
2015-07-18 20:52:14 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
if (!empty($this->options['languages'])) {
|
|
|
|
foreach ($this->options['languages'] as $key => $node) {
|
2015-07-18 21:45:38 +02:00
|
|
|
$this->tree->getEntries()[$key]->setTitle($node);
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-28 00:18:29 +01:00
|
|
|
|
|
|
|
// Enhance the tree with processors
|
|
|
|
$this->getProcessor()->manipulateTree($this->tree);
|
|
|
|
|
|
|
|
// Sort the tree one last time before it is finalized
|
|
|
|
$this->sortTree($this->tree);
|
|
|
|
|
|
|
|
$this->finalizeTree($this->tree);
|
|
|
|
}
|
|
|
|
|
2015-11-06 22:44:34 +01:00
|
|
|
public function sortTree(Directory $current)
|
|
|
|
{
|
2015-10-28 00:18:29 +01:00
|
|
|
$current->sort();
|
|
|
|
foreach ($current->getEntries() as $entry) {
|
|
|
|
if ($entry instanceof Directory) {
|
|
|
|
$this->sortTree($entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function finalizeTree(Directory $current, $prev = null)
|
|
|
|
{
|
|
|
|
foreach ($current->getEntries() as $entry) {
|
|
|
|
if ($entry instanceof Directory) {
|
|
|
|
$prev = $this->finalizeTree($entry, $prev);
|
|
|
|
} elseif ($entry instanceof Content) {
|
|
|
|
if ($prev) {
|
|
|
|
$prev->setNext($entry);
|
|
|
|
$entry->setPrevious($prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
$prev = $entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $prev;
|
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-07-19 14:05:12 +02:00
|
|
|
if (!$this->merged_defaults) {
|
|
|
|
$default = [
|
|
|
|
//Features
|
|
|
|
'multilanguage' => !empty($this->options['languages']),
|
|
|
|
|
|
|
|
//Paths and tree
|
|
|
|
'mode' => $this->mode,
|
|
|
|
'local_base' => $this->local_base,
|
|
|
|
'docs_path' => $this->docs_path,
|
2015-07-31 17:54:02 +02:00
|
|
|
'themes_path' => $this->themes_path,
|
2015-08-06 22:47:38 +02:00
|
|
|
'templates' => 'templates'
|
2015-07-19 14:05:12 +02:00
|
|
|
];
|
|
|
|
$this->options->conservativeMerge($default);
|
|
|
|
|
|
|
|
$this->options['index_key'] = 'index.html';
|
|
|
|
$this->options['base_page'] = $this->options['base_url'] = '';
|
|
|
|
|
|
|
|
$this->merged_defaults = true;
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
|
2015-07-19 14:05:12 +02:00
|
|
|
if ($this->tree && !$this->merged_tree) {
|
2015-06-29 16:11:01 +02:00
|
|
|
$this->options['tree'] = $this->tree;
|
2015-07-18 23:13:02 +02:00
|
|
|
$this->options['index'] = $this->tree->getIndexPage() ?: $this->tree->getFirstPage();
|
2015-06-29 16:11:01 +02:00
|
|
|
if ($this->options['multilanguage']) {
|
2015-04-22 12:23:57 +02:00
|
|
|
foreach ($this->options['languages'] as $key => $name) {
|
2015-07-18 14:37:18 +02:00
|
|
|
$this->options['entry_page'][$key] = $this->tree->getEntries()[$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-07-19 14:05:12 +02:00
|
|
|
$this->merged_tree = true;
|
2015-04-22 18:24:10 +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-07-19 00:55:57 +02:00
|
|
|
|
|
|
|
// This is not the cleanest but it's
|
|
|
|
// the best i've found to use the
|
|
|
|
// processor in very remote places
|
|
|
|
$this->options['processor_instance'] = $processor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGenerators()
|
|
|
|
{
|
|
|
|
$default = [
|
|
|
|
'confluence' => '\Todaymade\Daux\Format\Confluence\Generator',
|
2015-11-06 22:44:34 +01:00
|
|
|
'html-file' => '\Todaymade\Daux\Format\HTMLFile\Generator',
|
2015-07-19 00:55:57 +02:00
|
|
|
'html' => '\Todaymade\Daux\Format\HTML\Generator',
|
|
|
|
];
|
|
|
|
|
|
|
|
$extended = $this->getProcessor()->addGenerators();
|
|
|
|
|
|
|
|
return array_replace($default, $extended);
|
2015-07-17 18:34:00 +02:00
|
|
|
}
|
2015-07-19 22:38:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
public function getProcessorClass()
|
|
|
|
{
|
|
|
|
$processor = $this->getParams()['processor'];
|
|
|
|
|
|
|
|
if (empty($processor)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = "\\Todaymade\\Daux\\Extension\\" . $processor;
|
|
|
|
if (!class_exists($class)) {
|
|
|
|
throw new \RuntimeException("Class '$class' not found. We cannot use it as a Processor");
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO :: check that it implements processor
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Todaymade\Daux\Format\Base\Generator
|
|
|
|
*/
|
|
|
|
public function getGenerator()
|
|
|
|
{
|
2015-07-28 17:25:03 +02:00
|
|
|
if ($this->generator) {
|
|
|
|
return $this->generator;
|
|
|
|
}
|
|
|
|
|
2015-07-19 22:38:37 +02:00
|
|
|
$generators = $this->getGenerators();
|
|
|
|
|
|
|
|
$format = $this->getParams()['format'];
|
|
|
|
|
|
|
|
if (!array_key_exists($format, $generators)) {
|
|
|
|
throw new \RuntimeException("The format '$format' doesn't exist, did you forget to set your processor ?");
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = $generators[$format];
|
|
|
|
if (!class_exists($class)) {
|
|
|
|
throw new \RuntimeException("Class '$class' not found. We cannot use it as a Generator");
|
|
|
|
}
|
|
|
|
|
|
|
|
$interface = 'Todaymade\Daux\Format\Base\Generator';
|
|
|
|
if (!in_array('Todaymade\Daux\Format\Base\Generator', class_implements($class))) {
|
|
|
|
throw new \RuntimeException("The class '$class' does not implement the '$interface' interface");
|
|
|
|
}
|
|
|
|
|
2015-07-28 17:25:03 +02:00
|
|
|
return $this->generator = new $class($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getContentTypeHandler()
|
|
|
|
{
|
|
|
|
if ($this->typeHandler) {
|
|
|
|
return $this->typeHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
$base_types = $this->getGenerator()->getContentTypes();
|
|
|
|
|
|
|
|
$extended = $this->getProcessor()->addContentType();
|
|
|
|
|
|
|
|
$types = array_merge($base_types, $extended);
|
|
|
|
|
|
|
|
return $this->typeHandler = new ContentTypeHandler($types);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all content file extensions
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getContentExtensions()
|
|
|
|
{
|
|
|
|
if ($this->validExtensions) {
|
|
|
|
return $this->validExtensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->validExtensions = $this->getContentTypeHandler()->getContentExtensions();
|
2015-07-19 22:38:37 +02:00
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|