Move getIndexPage and getFirstPage to Directory

This commit is contained in:
Stéphane Goetz 2015-07-18 23:13:02 +02:00
parent e87c86d9aa
commit a13469047a
7 changed files with 128 additions and 100 deletions

BIN
daux.phar Executable file → Normal file

Binary file not shown.

View File

@ -146,8 +146,8 @@ class Daux
*/ */
private function generateTree() private function generateTree()
{ {
$this->tree = new Root($this->docs_path); $this->tree = new Root($this->getParams(), $this->docs_path);
Builder::build($this->tree, $this->options['ignore'], $this->getParams()); Builder::build($this->tree, $this->options['ignore']);
if (!empty($this->options['languages'])) { if (!empty($this->options['languages'])) {
foreach ($this->options['languages'] as $key => $node) { foreach ($this->options['languages'] as $key => $node) {
@ -176,7 +176,7 @@ class Daux
if ($this->tree) { if ($this->tree) {
$this->options['tree'] = $this->tree; $this->options['tree'] = $this->tree;
$this->options['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage(); $this->options['index'] = $this->tree->getIndexPage() ?: $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->getEntries()[$key]->getFirstPage(); $this->options['entry_page'][$key] = $this->tree->getEntries()[$key]->getFirstPage();

View File

@ -133,8 +133,8 @@ class DauxHelper
return $tree; return $tree;
} }
if ($tree->getIndexPage()) { if ($index = $tree->getIndexPage()) {
return $tree->getIndexPage(); return $index;
} }
return false; return false;

View File

@ -11,9 +11,8 @@ class Builder
* *
* @param Directory $node * @param Directory $node
* @param array $ignore * @param array $ignore
* @param Config $params
*/ */
public static function build($node, $ignore, Config $params) public static function build($node, $ignore)
{ {
if (!$dh = opendir($node->getPath())) { if (!$dh = opendir($node->getPath())) {
return; return;
@ -42,45 +41,44 @@ class Builder
$new = new Directory($node, static::getUriFromFilename(static::getFilename($path)), $path); $new = new Directory($node, static::getUriFromFilename(static::getFilename($path)), $path);
$new->setName(DauxHelper::pathinfo($path)['filename']); $new->setName(DauxHelper::pathinfo($path)['filename']);
$new->setTitle(static::getTitleFromFilename($new->getName())); $new->setTitle(static::getTitleFromFilename($new->getName()));
static::build($new, $ignore, $params); static::build($new, $ignore);
} else { } else {
static::createContent($node, $path, $params); static::createContent($node, $path);
} }
} }
$node->sort(); $node->sort();
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);
}
} }
/** /**
* @param Directory $parent * @param Directory $parent
* @param string $path * @param string $path
* @param Config $params
* @return Content|Raw * @return Content|Raw
*/ */
public static function createContent(Directory $parent, $path, Config $params) public static function createContent(Directory $parent, $path)
{ {
$name = DauxHelper::pathinfo($path)['filename']; $name = DauxHelper::pathinfo($path)['filename'];
if (in_array(pathinfo($path, PATHINFO_EXTENSION), Daux::$VALID_MARKDOWN_EXTENSIONS)) { if (!in_array(pathinfo($path, PATHINFO_EXTENSION), Daux::$VALID_MARKDOWN_EXTENSIONS)) {
$entry = new Raw($parent, static::getUriFromFilename(static::getFilename($path)), $path, filemtime($path));
$entry->setTitle(static::getTitleFromFilename($name));
$entry->setName($name);
return $entry;
}
$config = $parent->getConfig();
$uri = static::getUriFromFilename($name); $uri = static::getUriFromFilename($name);
if ($params['mode'] === Daux::STATIC_MODE) { if ($config['mode'] === Daux::STATIC_MODE) {
$uri .= '.html'; $uri .= '.html';
} }
$entry = new Content($parent, $uri, $path, filemtime($path)); $entry = new Content($parent, $uri, $path, filemtime($path));
} else {
$entry = new Raw($parent, static::getUriFromFilename(static::getFilename($path)), $path, filemtime($path));
}
if ($entry->getUri() == $params['index_key']) { if ($entry->getUri() == $config['index_key']) {
if ($parent instanceof Root) { if ($parent instanceof Root) {
$entry->setTitle($params['title']); $entry->setTitle($config['title']);
} else { } else {
$entry->setTitle($parent->getTitle()); $entry->setTitle($parent->getTitle());
} }
@ -88,7 +86,6 @@ class Builder
$entry->setTitle(static::getTitleFromFilename($name)); $entry->setTitle(static::getTitleFromFilename($name));
} }
$entry->setIndexPage(false);
$entry->setName($name); $entry->setName($name);
return $entry; return $entry;
@ -181,7 +178,6 @@ class Builder
if ($title == 'index') { if ($title == 'index') {
$page->setName('_index'); $page->setName('_index');
$page->setTitle($parent->getTitle()); $page->setTitle($parent->getTitle());
$parent->setIndexPage($page);
} else { } else {
$page->setName($slug); $page->setName($slug);
$page->setTitle($title); $page->setTitle($title);

View File

@ -2,8 +2,12 @@
class Directory extends Entry class Directory extends Entry
{ {
/** @var Entry[] */
protected $children = []; protected $children = [];
/** @var Content */
protected $first_page;
public function sort() public function sort()
{ {
uasort($this->children, array($this, 'compareEntries')); uasort($this->children, array($this, 'compareEntries'));
@ -27,6 +31,78 @@ class Directory extends Entry
unset($this->children[$entry->getUri()]); unset($this->children[$entry->getUri()]);
} }
/**
* @return \Todaymade\Daux\Config
*/
public function getConfig()
{
if (!$this->parent) {
throw new \RuntimeException("Could not retrieve configuration. Are you sure that your tree has a Root ?");
}
return $this->parent->getConfig();
}
/**
* @return Content|null
*/
public function getIndexPage()
{
$index_key = $this->getConfig()['index_key'];
if (isset($this->children[$index_key])) {
return $this->children[$index_key];
}
return null;
}
/**
* @return Content|false
*/
public function getFirstPage()
{
if ($this->first_page) {
return $this->first_page;
}
if (!$this instanceof Directory) {
return false;
}
// First we try to find a real page
foreach ($this->getEntries() as $node) {
if ($node instanceof Content) {
// TODO :: this condition looks weird ...
if (!$node->getParent() && $node->getTitle() == 'index') {
//the homepage should not count as first page
continue;
}
$this->setFirstPage($node);
return $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->setFirstPage($page);
return $page;
}
}
return false;
}
/**
* @param Content $first_page
*/
public function setFirstPage($first_page)
{
$this->first_page = $first_page;
}
private function compareEntries($a, $b) private function compareEntries($a, $b)
{ {
$name_a = explode('_', $a->name); $name_a = explode('_', $a->name);
@ -76,6 +152,9 @@ class Directory extends Entry
{ {
$dump = parent::dump(); $dump = parent::dump();
$dump['index'] = $this->getIndexPage() ? $this->getIndexPage()->getUrl() : '';
$dump['first'] = $this->getFirstPage() ? $this->getFirstPage()->getUrl() : '';
foreach ($this->getEntries() as $entry) { foreach ($this->getEntries() as $entry) {
$dump['children'][] = $entry->dump(); $dump['children'][] = $entry->dump();
} }

View File

@ -8,12 +8,6 @@ abstract class Entry
/** @var string */ /** @var string */
protected $name; protected $name;
/** @var Content */
protected $index_page;
/** @var Content */
protected $first_page;
/** @var string */ /** @var string */
protected $uri; protected $uri;
@ -86,68 +80,6 @@ abstract class Entry
} }
} }
/**
* @return Content
*/
public function getIndexPage()
{
return $this->index_page;
}
/**
* @param Content $index_page
*/
public function setIndexPage($index_page)
{
$this->index_page = $index_page;
}
/**
* @return Content|false
*/
public function getFirstPage()
{
if ($this->first_page) {
return $this->first_page;
}
if (!$this instanceof Directory) {
return false;
}
// First we try to find a real page
foreach ($this->getEntries() as $node) {
if ($node instanceof Content) {
// TODO :: this condition looks weird ...
if (!$node->getParent() && $node->getTitle() == 'index') {
//the homepage should not count as first page
continue;
}
$this->setFirstPage($node);
return $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->setFirstPage($page);
return $page;
}
}
return false;
}
/**
* @param Content $first_page
*/
public function setFirstPage($first_page)
{
$this->first_page = $first_page;
}
/** /**
* @return string * @return string
*/ */
@ -232,8 +164,6 @@ abstract class Entry
'name' => $this->getName(), 'name' => $this->getName(),
'uri' => $this->getUri(), 'uri' => $this->getUri(),
'url' => $this->getUrl(), 'url' => $this->getUrl(),
'index' => $this->getIndexPage() ? $this->getIndexPage()->getUrl() : '',
'first' => $this->getFirstPage() ? $this->getFirstPage()->getUrl() : '',
'path' => $this->path 'path' => $this->path
]; ];
} }

View File

@ -1,15 +1,38 @@
<?php namespace Todaymade\Daux\Tree; <?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\Config;
class Root extends Directory class Root extends Directory
{ {
/** @var Config */
protected $config;
/** /**
* The root doesn't have a parent * The root doesn't have a parent
* *
* @param string $uri * @param string $uri
*/ */
public function __construct($uri) public function __construct(Config $config, $uri)
{ {
$this->setConfig($config);
$this->setUri($uri); $this->setUri($uri);
$this->path = $uri; $this->path = $uri;
} }
/**
* @return Config
*/
public function getConfig()
{
return $this->config;
}
/**
* @param Config $config
*/
public function setConfig($config)
{
$this->config = $config;
}
} }