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;
|
2018-06-05 20:31:51 +02:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
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';
|
|
|
|
|
2018-06-05 20:31:51 +02:00
|
|
|
public static $output;
|
|
|
|
|
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;
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
/** @var string[] */
|
2015-07-28 17:25:03 +02:00
|
|
|
protected $validExtensions;
|
|
|
|
|
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 */
|
2019-12-05 21:25:58 +01:00
|
|
|
public $config;
|
2015-04-23 00:32:30 +02:00
|
|
|
|
2015-07-19 14:05:12 +02:00
|
|
|
/** @var bool */
|
|
|
|
private $merged_tree = false;
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
public function __construct(Config $config, OutputInterface $output)
|
2014-07-12 12:31:57 +02:00
|
|
|
{
|
2018-06-05 20:31:51 +02:00
|
|
|
Daux::$output = $output;
|
2016-09-06 23:11:32 +02:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
|
|
|
|
$this->config = $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
|
|
|
{
|
2019-12-05 21:25:58 +01:00
|
|
|
$this->config->setValidContentExtensions($this->getContentExtensions());
|
2015-07-28 17:25:03 +02:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$this->tree = new Root($this->getConfig());
|
|
|
|
Builder::build($this->tree, $this->config->getIgnore());
|
2015-07-18 20:52:14 +02:00
|
|
|
|
2017-01-10 10:28:58 +01:00
|
|
|
// Apply the language name as Section title
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($this->config->isMultilanguage()) {
|
|
|
|
foreach ($this->config->getLanguages() 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
|
2017-11-07 22:44:27 +01:00
|
|
|
Builder::sortTree($this->tree);
|
2015-10-28 00:18:29 +01:00
|
|
|
|
2017-11-07 22:44:27 +01:00
|
|
|
Builder::finalizeTree($this->tree);
|
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
|
|
|
*/
|
2019-12-05 21:25:58 +01:00
|
|
|
public function getConfig()
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-19 14:05:12 +02:00
|
|
|
if ($this->tree && !$this->merged_tree) {
|
2019-12-05 21:25:58 +01:00
|
|
|
$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();
|
2015-04-22 12:23:57 +02:00
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
} else {
|
2019-12-05 21:25:58 +01:00
|
|
|
$entry_page= $this->tree->getFirstPage();
|
2014-07-12 12:31:57 +02:00
|
|
|
}
|
2019-12-05 21:25:58 +01:00
|
|
|
$this->config->setEntryPage($entry_page);
|
2015-07-19 14:05:12 +02:00
|
|
|
$this->merged_tree = true;
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Config
|
|
|
|
* @deprecated Use getConfig instead
|
|
|
|
*/
|
|
|
|
public function getParams()
|
|
|
|
{
|
|
|
|
return $this->getConfig();
|
2015-04-22 12:23:57 +02:00
|
|
|
}
|
2015-07-17 18:34:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Processor
|
|
|
|
*/
|
|
|
|
public function getProcessor()
|
|
|
|
{
|
|
|
|
if (!$this->processor) {
|
2018-06-05 20:31:51 +02:00
|
|
|
$this->processor = new Processor($this, Daux::getOutput(), 0);
|
2015-07-17 18:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-12-05 21:25:58 +01:00
|
|
|
// the best I've found to use the
|
2015-07-19 00:55:57 +02:00
|
|
|
// processor in very remote places
|
2019-12-05 21:25:58 +01:00
|
|
|
$this->config->setProcessorInstance($processor);
|
2015-07-19 00:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2019-12-05 21:25:58 +01:00
|
|
|
$processor = $this->getConfig()->getProcessor();
|
2015-07-19 22:38:37 +02:00
|
|
|
|
|
|
|
if (empty($processor)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
$class = '\\Todaymade\\Daux\\Extension\\' . $processor;
|
2015-07-19 22:38:37 +02:00
|
|
|
if (!class_exists($class)) {
|
|
|
|
throw new \RuntimeException("Class '$class' not found. We cannot use it as a Processor");
|
|
|
|
}
|
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
if (!array_key_exists('Todaymade\\Daux\\Processor', class_parents($class))) {
|
2016-01-31 11:59:35 +01:00
|
|
|
throw new \RuntimeException("Class '$class' invalid, should extend '\\Todaymade\\Daux\\Processor'");
|
|
|
|
}
|
2015-07-19 22:38:37 +02:00
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:41:49 +02:00
|
|
|
protected function findAlternatives($input, $words) {
|
|
|
|
$alternatives = [];
|
|
|
|
|
|
|
|
foreach ($words as $word) {
|
|
|
|
$lev = levenshtein($input, $word);
|
|
|
|
|
|
|
|
if ($lev <= \strlen($word) / 3) {
|
|
|
|
$alternatives[] = $word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $alternatives;
|
|
|
|
}
|
|
|
|
|
2015-07-19 22:38:37 +02:00
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$format = $this->getConfig()->getFormat();
|
2015-07-19 22:38:37 +02:00
|
|
|
|
|
|
|
if (!array_key_exists($format, $generators)) {
|
2018-09-21 23:41:49 +02:00
|
|
|
$message = "The format '$format' doesn't exist, did you forget to set your processor ?";
|
|
|
|
|
|
|
|
$alternatives = $this->findAlternatives($format, array_keys($generators));
|
|
|
|
|
|
|
|
if (0 == \count($alternatives)) {
|
|
|
|
$message .= "\n\nAvailable formats are \n " . implode("\n ", array_keys($generators));
|
|
|
|
} elseif (1 == \count($alternatives)) {
|
|
|
|
$message .= "\n\nDid you mean this?\n " . implode("\n ", $alternatives);
|
|
|
|
} else {
|
|
|
|
$message .= "\n\nDid you mean one of these?\n " . implode("\n ", $alternatives);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \RuntimeException($message);
|
2015-07-19 22:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
2016-07-29 22:38:03 +02:00
|
|
|
if (!empty($this->validExtensions)) {
|
2015-07-28 17:25:03 +02:00
|
|
|
return $this->validExtensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->validExtensions = $this->getContentTypeHandler()->getContentExtensions();
|
2015-07-19 22:38:37 +02:00
|
|
|
}
|
2018-06-05 20:31:51 +02:00
|
|
|
|
|
|
|
public static function getOutput() {
|
|
|
|
if (!Daux::$output) {
|
2018-06-07 20:40:38 +02:00
|
|
|
Daux::$output = new NullOutput();
|
2018-06-05 20:31:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Daux::$output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a message to the output.
|
|
|
|
*
|
|
|
|
* @param string|array $messages The message as an array of lines or a single string
|
|
|
|
* @param bool $newline Whether to add a newline
|
|
|
|
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
|
|
|
|
*/
|
|
|
|
public static function write($messages, $newline = false, $options = 0) {
|
2018-06-07 20:40:38 +02:00
|
|
|
Daux::getOutput()->write($messages, $newline, $options);
|
2018-06-05 20:31:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a message to the output and adds a newline at the end.
|
|
|
|
*
|
|
|
|
* @param string|array $messages The message as an array of lines of a single string
|
|
|
|
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
|
|
|
|
*/
|
|
|
|
public static function writeln($messages, $options = 0) {
|
2018-06-07 20:40:38 +02:00
|
|
|
Daux::getOutput()->write($messages, true, $options);
|
2018-06-05 20:31:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getVerbosity() {
|
|
|
|
return Daux::getOutput()->getVerbosity();
|
|
|
|
}
|
2015-04-23 00:32:30 +02:00
|
|
|
}
|