2015-04-22 18:24:10 +02:00
|
|
|
<?php namespace Todaymade\Daux\Tree;
|
|
|
|
|
|
|
|
use Todaymade\Daux\Daux;
|
2015-07-17 18:28:54 +02:00
|
|
|
use Todaymade\Daux\DauxHelper;
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
class Builder
|
|
|
|
{
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* Build the initial tree
|
|
|
|
*
|
2015-07-18 20:52:14 +02:00
|
|
|
* @param Directory $node
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param array $ignore
|
|
|
|
*/
|
2015-07-18 23:13:02 +02:00
|
|
|
public static function build($node, $ignore)
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-07-18 20:52:14 +02:00
|
|
|
if (!$dh = opendir($node->getPath())) {
|
2015-04-22 18:24:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
if ($node instanceof Root) {
|
|
|
|
// Ignore config.json in the root directory
|
|
|
|
$ignore['files'][] = 'config.json';
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
if ($file == '.' || $file == '..') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-20 15:59:52 +02:00
|
|
|
$path = $node->getPath() . DIRECTORY_SEPARATOR . $file;
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
if (is_dir($path) && in_array($file, $ignore['folders'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!is_dir($path) && in_array($file, $ignore['files'])) {
|
2015-04-22 18:24:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_dir($path)) {
|
2015-07-20 15:59:52 +02:00
|
|
|
$new = new Directory($node, static::removeSortingInformations(static::getFilename($path)), $path);
|
2015-07-18 20:52:14 +02:00
|
|
|
$new->setName(DauxHelper::pathinfo($path)['filename']);
|
2015-07-20 15:59:52 +02:00
|
|
|
$new->setTitle(static::removeSortingInformations($new->getName(), ' '));
|
2015-07-18 23:13:02 +02:00
|
|
|
static::build($new, $ignore);
|
2015-04-22 18:24:10 +02:00
|
|
|
} else {
|
2015-07-18 23:13:02 +02:00
|
|
|
static::createContent($node, $path);
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->sort();
|
2015-07-18 20:52:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Directory $parent
|
|
|
|
* @param string $path
|
|
|
|
* @return Content|Raw
|
|
|
|
*/
|
2015-07-18 23:13:02 +02:00
|
|
|
public static function createContent(Directory $parent, $path)
|
2015-07-18 20:52:14 +02:00
|
|
|
{
|
|
|
|
$name = DauxHelper::pathinfo($path)['filename'];
|
|
|
|
|
2015-07-19 16:36:34 +02:00
|
|
|
$config = $parent->getConfig();
|
|
|
|
|
|
|
|
if (!in_array(pathinfo($path, PATHINFO_EXTENSION), $config['valid_content_extensions'])) {
|
2015-07-20 15:59:52 +02:00
|
|
|
$entry = new Raw($parent, static::removeSortingInformations(static::getFilename($path)), $path, filemtime($path));
|
|
|
|
$entry->setTitle(static::removeSortingInformations($name, ' '));
|
2015-07-18 23:13:02 +02:00
|
|
|
$entry->setName($name);
|
|
|
|
|
|
|
|
return $entry;
|
2015-07-18 20:52:14 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 15:59:52 +02:00
|
|
|
$uri = static::removeSortingInformations($name);
|
2015-07-18 23:13:02 +02:00
|
|
|
if ($config['mode'] === Daux::STATIC_MODE) {
|
|
|
|
$uri .= '.html';
|
|
|
|
}
|
|
|
|
|
|
|
|
$entry = new Content($parent, $uri, $path, filemtime($path));
|
|
|
|
|
|
|
|
if ($entry->getUri() == $config['index_key']) {
|
2015-07-18 20:52:14 +02:00
|
|
|
if ($parent instanceof Root) {
|
2015-07-18 23:13:02 +02:00
|
|
|
$entry->setTitle($config['title']);
|
2015-07-18 20:52:14 +02:00
|
|
|
} else {
|
|
|
|
$entry->setTitle($parent->getTitle());
|
|
|
|
}
|
|
|
|
} else {
|
2015-07-20 15:59:52 +02:00
|
|
|
$entry->setTitle(static::removeSortingInformations($name, ' '));
|
2015-07-18 20:52:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$entry->setName($name);
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getFilename($file)
|
|
|
|
{
|
|
|
|
$parts = explode('/', $file);
|
|
|
|
return end($parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-07-20 15:59:52 +02:00
|
|
|
protected static function removeSortingInformations($filename, $separator = '_')
|
2015-07-18 20:52:14 +02:00
|
|
|
{
|
|
|
|
$filename = explode('_', $filename);
|
|
|
|
if ($filename[0] == '' || is_numeric($filename[0])) {
|
|
|
|
unset($filename[0]);
|
|
|
|
} else {
|
|
|
|
$t = $filename[0];
|
|
|
|
if ($t[0] == '-') {
|
|
|
|
$filename[0] = substr($t, 1);
|
|
|
|
}
|
|
|
|
}
|
2015-07-20 15:59:52 +02:00
|
|
|
$filename = implode($separator, $filename);
|
2015-07-18 20:52:14 +02:00
|
|
|
return $filename;
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
2015-07-17 18:28:54 +02:00
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
2015-07-18 14:37:18 +02:00
|
|
|
* @param Directory $parent
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param String $title
|
|
|
|
* @return Directory
|
|
|
|
*/
|
2015-07-18 14:37:18 +02:00
|
|
|
public static function getOrCreateDir(Directory $parent, $title)
|
2015-07-17 23:38:06 +02:00
|
|
|
{
|
2015-07-17 18:28:54 +02:00
|
|
|
$slug = DauxHelper::slug($title);
|
|
|
|
|
2015-07-18 14:37:18 +02:00
|
|
|
if (array_key_exists($slug, $parent->getEntries())) {
|
|
|
|
return $parent->getEntries()[$slug];
|
2015-07-17 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
$dir = new Directory($parent, $slug);
|
2015-07-17 18:28:54 +02:00
|
|
|
$dir->setTitle($title);
|
|
|
|
|
|
|
|
return $dir;
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
2015-07-18 20:52:14 +02:00
|
|
|
* @param Directory $parent
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param string $title
|
|
|
|
* @return Content
|
|
|
|
*/
|
2015-07-18 20:52:14 +02:00
|
|
|
public static function getOrCreatePage(Directory $parent, $title)
|
2015-07-17 23:38:06 +02:00
|
|
|
{
|
2015-07-17 18:28:54 +02:00
|
|
|
$slug = DauxHelper::slug($title);
|
|
|
|
$uri = $slug . ".html";
|
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
if (array_key_exists($uri, $parent->getEntries())) {
|
|
|
|
return $parent->getEntries()[$uri];
|
2015-07-17 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
$page = new Content($parent, $uri);
|
2015-07-17 18:28:54 +02:00
|
|
|
$page->setContent("-"); //set an almost empty content to avoid problems
|
|
|
|
|
|
|
|
if ($title == 'index') {
|
|
|
|
$page->setName('_index');
|
2015-07-18 20:52:14 +02:00
|
|
|
$page->setTitle($parent->getTitle());
|
2015-07-17 18:28:54 +02:00
|
|
|
} else {
|
|
|
|
$page->setName($slug);
|
|
|
|
$page->setTitle($title);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|