Move getIndexPage and getFirstPage to Directory
This commit is contained in:
parent
e87c86d9aa
commit
a13469047a
@ -146,8 +146,8 @@ class Daux
|
||||
*/
|
||||
private function generateTree()
|
||||
{
|
||||
$this->tree = new Root($this->docs_path);
|
||||
Builder::build($this->tree, $this->options['ignore'], $this->getParams());
|
||||
$this->tree = new Root($this->getParams(), $this->docs_path);
|
||||
Builder::build($this->tree, $this->options['ignore']);
|
||||
|
||||
if (!empty($this->options['languages'])) {
|
||||
foreach ($this->options['languages'] as $key => $node) {
|
||||
@ -176,7 +176,7 @@ class Daux
|
||||
|
||||
if ($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']) {
|
||||
foreach ($this->options['languages'] as $key => $name) {
|
||||
$this->options['entry_page'][$key] = $this->tree->getEntries()[$key]->getFirstPage();
|
||||
|
@ -133,8 +133,8 @@ class DauxHelper
|
||||
return $tree;
|
||||
}
|
||||
|
||||
if ($tree->getIndexPage()) {
|
||||
return $tree->getIndexPage();
|
||||
if ($index = $tree->getIndexPage()) {
|
||||
return $index;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -11,9 +11,8 @@ class Builder
|
||||
*
|
||||
* @param Directory $node
|
||||
* @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())) {
|
||||
return;
|
||||
@ -42,45 +41,44 @@ class Builder
|
||||
$new = new Directory($node, static::getUriFromFilename(static::getFilename($path)), $path);
|
||||
$new->setName(DauxHelper::pathinfo($path)['filename']);
|
||||
$new->setTitle(static::getTitleFromFilename($new->getName()));
|
||||
static::build($new, $ignore, $params);
|
||||
static::build($new, $ignore);
|
||||
} else {
|
||||
static::createContent($node, $path, $params);
|
||||
static::createContent($node, $path);
|
||||
}
|
||||
}
|
||||
|
||||
$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 string $path
|
||||
* @param Config $params
|
||||
* @return Content|Raw
|
||||
*/
|
||||
public static function createContent(Directory $parent, $path, Config $params)
|
||||
public static function createContent(Directory $parent, $path)
|
||||
{
|
||||
$name = DauxHelper::pathinfo($path)['filename'];
|
||||
|
||||
if (in_array(pathinfo($path, PATHINFO_EXTENSION), Daux::$VALID_MARKDOWN_EXTENSIONS)) {
|
||||
$uri = static::getUriFromFilename($name);
|
||||
if ($params['mode'] === Daux::STATIC_MODE) {
|
||||
$uri .= '.html';
|
||||
}
|
||||
|
||||
$entry = new Content($parent, $uri, $path, filemtime($path));
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
|
||||
if ($entry->getUri() == $params['index_key']) {
|
||||
$config = $parent->getConfig();
|
||||
|
||||
$uri = static::getUriFromFilename($name);
|
||||
if ($config['mode'] === Daux::STATIC_MODE) {
|
||||
$uri .= '.html';
|
||||
}
|
||||
|
||||
$entry = new Content($parent, $uri, $path, filemtime($path));
|
||||
|
||||
if ($entry->getUri() == $config['index_key']) {
|
||||
if ($parent instanceof Root) {
|
||||
$entry->setTitle($params['title']);
|
||||
$entry->setTitle($config['title']);
|
||||
} else {
|
||||
$entry->setTitle($parent->getTitle());
|
||||
}
|
||||
@ -88,7 +86,6 @@ class Builder
|
||||
$entry->setTitle(static::getTitleFromFilename($name));
|
||||
}
|
||||
|
||||
$entry->setIndexPage(false);
|
||||
$entry->setName($name);
|
||||
|
||||
return $entry;
|
||||
@ -181,7 +178,6 @@ class Builder
|
||||
if ($title == 'index') {
|
||||
$page->setName('_index');
|
||||
$page->setTitle($parent->getTitle());
|
||||
$parent->setIndexPage($page);
|
||||
} else {
|
||||
$page->setName($slug);
|
||||
$page->setTitle($title);
|
||||
|
@ -2,8 +2,12 @@
|
||||
|
||||
class Directory extends Entry
|
||||
{
|
||||
/** @var Entry[] */
|
||||
protected $children = [];
|
||||
|
||||
/** @var Content */
|
||||
protected $first_page;
|
||||
|
||||
public function sort()
|
||||
{
|
||||
uasort($this->children, array($this, 'compareEntries'));
|
||||
@ -27,6 +31,78 @@ class Directory extends Entry
|
||||
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)
|
||||
{
|
||||
$name_a = explode('_', $a->name);
|
||||
@ -76,6 +152,9 @@ class Directory extends Entry
|
||||
{
|
||||
$dump = parent::dump();
|
||||
|
||||
$dump['index'] = $this->getIndexPage() ? $this->getIndexPage()->getUrl() : '';
|
||||
$dump['first'] = $this->getFirstPage() ? $this->getFirstPage()->getUrl() : '';
|
||||
|
||||
foreach ($this->getEntries() as $entry) {
|
||||
$dump['children'][] = $entry->dump();
|
||||
}
|
||||
|
@ -8,12 +8,6 @@ abstract class Entry
|
||||
/** @var string */
|
||||
protected $name;
|
||||
|
||||
/** @var Content */
|
||||
protected $index_page;
|
||||
|
||||
/** @var Content */
|
||||
protected $first_page;
|
||||
|
||||
/** @var string */
|
||||
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
|
||||
*/
|
||||
@ -232,8 +164,6 @@ abstract class Entry
|
||||
'name' => $this->getName(),
|
||||
'uri' => $this->getUri(),
|
||||
'url' => $this->getUrl(),
|
||||
'index' => $this->getIndexPage() ? $this->getIndexPage()->getUrl() : '',
|
||||
'first' => $this->getFirstPage() ? $this->getFirstPage()->getUrl() : '',
|
||||
'path' => $this->path
|
||||
];
|
||||
}
|
||||
|
@ -1,15 +1,38 @@
|
||||
<?php namespace Todaymade\Daux\Tree;
|
||||
|
||||
use Todaymade\Daux\Config;
|
||||
|
||||
class Root extends Directory
|
||||
{
|
||||
/** @var Config */
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* The root doesn't have a parent
|
||||
*
|
||||
* @param string $uri
|
||||
*/
|
||||
public function __construct($uri)
|
||||
public function __construct(Config $config, $uri)
|
||||
{
|
||||
$this->setConfig($config);
|
||||
|
||||
$this->setUri($uri);
|
||||
$this->path = $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Config
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
*/
|
||||
public function setConfig($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user