Refactor to not use ->value in the tree

This commit is contained in:
Stéphane Goetz 2015-07-18 14:37:18 +02:00
bovenliggende 9cdc9815ea
commit 3235c49acd
11 gewijzigde bestanden met toevoegingen van 55 en 45 verwijderingen

Bestand weergeven

@ -127,7 +127,7 @@ class Daux
$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;
$this->tree->getEntries()[$key]->title = $node;
}
}
}
@ -155,7 +155,7 @@ class Daux
$this->options['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage();
if ($this->options['multilanguage']) {
foreach ($this->options['languages'] as $key => $name) {
$this->options['entry_page'][$key] = $this->tree->value[$key]->getFirstPage();
$this->options['entry_page'][$key] = $this->tree->getEntries()[$key]->getFirstPage();
}
} else {
$this->options['entry_page'] = $this->tree->getFirstPage();

Bestand weergeven

@ -113,8 +113,8 @@ class DauxHelper
// if the node exists in the current request tree,
// change the $tree variable to reference the new
// node and proceed to the next url part
if (isset($tree->value[$node])) {
$tree = $tree->value[$node];
if (isset($tree->getEntries()[$node])) {
$tree = $tree->getEntries()[$node];
continue;
}

Bestand weergeven

@ -53,7 +53,7 @@ class Generator
if ($base_url !== '') {
$params['entry_page'] = $tree->getFirstPage();
}
foreach ($tree->value as $key => $node) {
foreach ($tree->getEntries() as $key => $node) {
if ($node instanceof Directory) {
$final['children'][$this->prefix . $node->getTitle()] = $this->generateRecursive(
$node,

Bestand weergeven

@ -54,7 +54,7 @@ class Generator
if ($base_url !== '' && empty($params['entry_page'])) {
$params['entry_page'] = $tree->getFirstPage();
}
foreach ($tree->value as $key => $node) {
foreach ($tree->getEntries() as $key => $node) {
if ($node instanceof Directory) {
$new_output_dir = $output_dir . DS . $key;
mkdir($new_output_dir);

Bestand weergeven

@ -2,6 +2,8 @@
use League\Plates\Engine;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Tree\Content;
use Todaymade\Daux\Tree\Directory;
class Template
{
@ -88,13 +90,13 @@ class Template
return "<ul class='nav nav-list'>$nav</ul>";
}
private function buildNavigation($tree, $path, $current_url, $base_page, $mode)
private function buildNavigation(Directory $tree, $path, $current_url, $base_page, $mode)
{
$nav = [];
foreach ($tree->value as $node) {
foreach ($tree->getEntries() as $node) {
$url = $node->getUri();
if ($node instanceof \Todaymade\Daux\Tree\Content) {
if ($node->value === 'index') {
if ($node instanceof Content) {
if ($node->getName() === '_index') {
continue;
}
@ -105,8 +107,7 @@ class Template
'href' => $base_page . $link,
'class' => ($current_url === $link) ? 'active' : ''
];
}
if ($node instanceof \Todaymade\Daux\Tree\Directory) {
} else if ($node instanceof Directory) {
$link = ($path === '') ? $url : $path . '/' . $url;
$folder = [

Bestand weergeven

@ -130,7 +130,7 @@ class Server
$params = $this->params;
$params['request'] = $request;
$params['file_uri'] = $file->value;
$params['file_uri'] = $file->getUri();
if ($request !== 'index') {
$params['entry_page'] = $file->getFirstPage();
}

Bestand weergeven

@ -57,14 +57,14 @@ class Builder
}
if ($entry instanceof Entry) {
$node->value[$entry->getUri()] = $entry;
$node->addChild($entry);
}
}
$node->sort();
if (isset($node->value[$params['index_key']])) {
$node->value[$params['index_key']]->setFirstPage($node->getFirstPage());
$node->setIndexPage($node->value[$params['index_key']]);
if (isset($node->getEntries()[$params['index_key']])) {
$node->getEntries()[$params['index_key']]->setFirstPage($node->getFirstPage());
$node->setIndexPage($node->getEntries()[$params['index_key']]);
} else {
$node->setIndexPage(false);
}
@ -72,22 +72,22 @@ class Builder
}
/**
* @param Entry $parent
* @param Directory $parent
* @param String $title
* @return Directory
*/
public static function getOrCreateDir($parent, $title)
public static function getOrCreateDir(Directory $parent, $title)
{
$slug = DauxHelper::slug($title);
if (array_key_exists($slug, $parent->value)) {
return $parent->value[$slug];
if (array_key_exists($slug, $parent->getEntries())) {
return $parent->getEntries()[$slug];
}
$dir = new Directory();
$dir->setTitle($title);
$dir->setUri($slug);
$parent->value[$slug] = $dir;
$parent->addChild($dir);
return $dir;
}
@ -102,10 +102,13 @@ class Builder
$slug = DauxHelper::slug($title);
$uri = $slug . ".html";
/**
* @var Directory $nearestParent
*/
$nearestParent = end($parents);
if (array_key_exists($uri, $nearestParent->value)) {
return $nearestParent->value[$uri];
if (array_key_exists($uri, $nearestParent->getEntries())) {
return $nearestParent->getEntries()[$uri];
}
$page = new Content('', $parents);
@ -115,14 +118,13 @@ class Builder
if ($title == 'index') {
$page->setName('_index');
$page->setTitle($nearestParent->getTitle());
$page->value = 'index';
$nearestParent->setIndexPage($page);
} else {
$page->setName($slug);
$page->setTitle($title);
}
$nearestParent->value[$uri] = $page;
$nearestParent->addChild($page);
return $page;
}

Bestand weergeven

@ -9,13 +9,6 @@ class Content extends Entry
*/
protected $content;
public function __construct($path = '', $parents = array())
{
parent::__construct($path, $parents);
$this->value = $this->uri;
}
/**
* @return string
*/

Bestand weergeven

@ -2,11 +2,29 @@
class Directory extends Entry
{
public $value = [];
protected $children = [];
public function sort()
{
uasort($this->value, array($this, 'compareEntries'));
uasort($this->children, array($this, 'compareEntries'));
}
public function getEntries()
{
return $this->children;
}
public function addChild(Entry $entry)
{
//TODO :: set parent in the entry
//TODO :: remove child from previous parent
$this->children[$entry->getUri()] = $entry;
}
public function removeChild(Entry $entry)
{
unset($this->children[$entry->getUri()]);
}
private function compareEntries($a, $b)

Bestand weergeven

@ -99,21 +99,23 @@ abstract class Entry
return false;
}
foreach ($this->value as $node) {
// First we try to find a real page
foreach ($this->getEntries() as $node) {
if ($node instanceof Content) {
if (!count($node->getParents()) && $node->title == 'index') {
//the homepage should not count as first page
continue;
}
$this->first_page = $node;
$this->setFirstPage($node);
return $node;
}
}
foreach ($this->value as $node) {
// If we can't find one we check in the sub-directories
foreach ($this->getEntries() as $node) {
if ($node instanceof Directory && $page = $node->getFirstPage()) {
$this->first_page = $page;
$this->setFirstPage($page);
return $page;
}
}

Bestand weergeven

@ -2,10 +2,4 @@
class Raw extends Entry
{
public function __construct($path = '', $parents = array())
{
parent::__construct($path, $parents);
$this->value = $this->uri;
}
}