Refactor to not use ->value in the tree

This commit is contained in:
Stéphane Goetz 2015-07-18 14:37:18 +02:00
parent 9cdc9815ea
commit 3235c49acd
11 changed files with 55 additions and 45 deletions

View File

@ -127,7 +127,7 @@ class Daux
$this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->getParams()); $this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->getParams());
if (!empty($this->options['languages'])) { if (!empty($this->options['languages'])) {
foreach ($this->options['languages'] as $key => $node) { 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(); $this->options['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage();
if ($this->options['multilanguage']) { if ($this->options['multilanguage']) {
foreach ($this->options['languages'] as $key => $name) { 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 { } else {
$this->options['entry_page'] = $this->tree->getFirstPage(); $this->options['entry_page'] = $this->tree->getFirstPage();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,11 +2,29 @@
class Directory extends Entry class Directory extends Entry
{ {
public $value = []; protected $children = [];
public function sort() 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) private function compareEntries($a, $b)

View File

@ -99,21 +99,23 @@ abstract class Entry
return false; 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 ($node instanceof Content) {
if (!count($node->getParents()) && $node->title == 'index') { if (!count($node->getParents()) && $node->title == 'index') {
//the homepage should not count as first page //the homepage should not count as first page
continue; continue;
} }
$this->first_page = $node; $this->setFirstPage($node);
return $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()) { if ($node instanceof Directory && $page = $node->getFirstPage()) {
$this->first_page = $page; $this->setFirstPage($page);
return $page; return $page;
} }
} }

View File

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