daux.io/libs/Format/Confluence/Generator.php

136 lines
4.1 KiB
PHP
Raw Normal View History

<?php namespace Todaymade\Daux\Format\Confluence;
use Symfony\Component\Console\Input\InputInterface;
2015-07-14 22:06:01 +02:00
use Symfony\Component\Console\Output\OutputInterface;
2019-12-07 11:22:40 +01:00
use Todaymade\Daux\Config as GlobalConfig;
2015-07-23 17:44:24 +02:00
use Todaymade\Daux\Console\RunAction;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Tree\Content;
use Todaymade\Daux\Tree\Directory;
class Generator implements \Todaymade\Daux\Format\Base\Generator
{
2015-07-14 22:06:01 +02:00
use RunAction;
/** @var string */
protected $prefix;
/** @var Daux */
protected $daux;
public function __construct(Daux $daux)
{
$this->daux = $daux;
$this->checkConfiguration();
}
public function checkConfiguration()
{
$config = $this->daux->getConfig();
$confluence = $config->getConfluenceConfiguration();
if ($confluence == null) {
2016-09-26 20:54:06 +02:00
throw new \RuntimeException('You must specify your Confluence configuration');
}
$mandatory = ['space_id', 'base_url', 'user', 'pass', 'prefix'];
$errors = [];
foreach ($mandatory as $key) {
2019-12-07 11:22:40 +01:00
if (!$confluence->hasValue($key)) {
$errors[] = $key;
}
}
if (count($errors)) {
2016-09-26 20:54:06 +02:00
throw new \RuntimeException("The following options are mandatory for confluence : '" . implode("', '", $errors) . "'");
}
2019-12-07 16:32:38 +01:00
if (!$confluence->hasAncestorId() && !$confluence->hasRootId()) {
throw new \RuntimeException("You must specify an 'ancestor_id' or a 'root_id' for confluence.");
}
}
/**
* @return array
*/
public function getContentTypes()
{
return [
new ContentTypes\Markdown\ContentType($this->daux->getConfig()),
];
}
2015-07-20 15:59:52 +02:00
/**
* {@inheritdoc}
*/
public function generateAll(InputInterface $input, OutputInterface $output, $width)
{
$config = $this->daux->getConfig();
$confluence = $config->getConfluenceConfiguration();
$this->prefix = trim($confluence->getPrefix()) . ' ';
if ($this->prefix == ' ') {
$this->prefix = '';
}
2015-07-14 22:06:01 +02:00
$tree = $this->runAction(
2016-07-27 21:32:51 +02:00
'Generating Tree ...',
2015-07-14 22:06:01 +02:00
$width,
2020-04-22 21:55:53 +02:00
function () use ($config) {
$tree = $this->generateRecursive($this->daux->tree, $config);
$tree['title'] = $this->prefix . $config->getTitle();
2015-07-14 22:06:01 +02:00
return $tree;
}
);
2016-07-27 21:32:51 +02:00
$output->writeln('Start Publishing...');
$publisher = new Publisher($confluence);
2015-07-14 22:06:01 +02:00
$publisher->output = $output;
$publisher->width = $width;
$publisher->publish($tree);
}
2019-12-07 11:22:40 +01:00
private function generateRecursive(Directory $tree, GlobalConfig $config, $base_url = '')
{
$final = ['title' => $this->prefix . $tree->getTitle()];
$config['base_url'] = $base_url;
$config->setImage(str_replace('<base_url>', $base_url, $config->getImage()));
if ($base_url !== '') {
$config->setEntryPage($tree->getFirstPage());
}
foreach ($tree->getEntries() as $key => $node) {
if ($node instanceof Directory) {
$final['children'][$this->prefix . $node->getTitle()] = $this->generateRecursive(
$node,
$config,
'../' . $base_url
);
} elseif ($node instanceof Content) {
$config->setRequest($node->getUrl());
2015-08-02 15:42:23 +02:00
$contentType = $this->daux->getContentTypeHandler()->getType($node);
$data = [
'title' => $this->prefix . $node->getTitle(),
'file' => $node,
'page' => ContentPage::fromFile($node, $config, $contentType),
];
if ($key == 'index.html') {
$final['title'] = $this->prefix . $tree->getTitle();
$final['file'] = $node;
$final['page'] = $data['page'];
} else {
$final['children'][$data['title']] = $data;
}
}
}
return $final;
}
}