Use DirectoryIterator to build the tree

This commit is contained in:
Stéphane Goetz 2015-08-16 21:19:55 +02:00
bovenliggende 26be012128
commit db520e36e6
4 gewijzigde bestanden met toevoegingen van 80 en 77 verwijderingen

Bestand weergeven

@ -136,24 +136,6 @@ class DauxHelper
return implode(DIRECTORY_SEPARATOR, $absolutes);
}
/**
* Get pathinfo for a file
*
* @param string $path
* @return array
*/
public static function pathinfo($path)
{
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);
$ret = [];
foreach (['dir' => 1, 'basename' => 2, 'filename' => 3, 'extension' => 5] as $key => $group) {
if (isset($m[$group])) {
$ret[$key] = $m[$group];
}
}
return $ret;
}
/**
* Locate a file in the tree. Returns the file if found or false
*

Bestand weergeven

@ -1,10 +1,54 @@
<?php namespace Todaymade\Daux\Tree;
use RuntimeException;
use SplFileInfo;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
class Builder
{
protected static $IGNORED = [
// Popular VCS Systems
'.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg',
// Operating system files
'.DS_Store', 'Thumbs.db',
];
protected static function isIgnored(\SplFileInfo $file, $ignore) {
if (in_array($file->getFilename(), static::$IGNORED)) {
return true;
}
if ($file->isDir() && in_array($file->getFilename(), $ignore['folders'])) {
return true;
}
if (!$file->isDir() && in_array($file->getFilename(), $ignore['files'])) {
return true;
}
return false;
}
/**
* Get name for a file
*
* @param string $path
* @return string
*/
protected static function getName($path)
{
// ['dir' => 1, 'basename' => 2, 'filename' => 3, 'extension' => 5]
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);
if (!isset($m[3])) {
throw new RuntimeException("Name not found");
}
return $m[3];
}
/**
* Build the initial tree
*
@ -13,7 +57,7 @@ class Builder
*/
public static function build($node, $ignore)
{
if (!$dh = opendir($node->getPath())) {
if (!$it = new \FilesystemIterator($node->getPath())) {
return;
}
@ -22,27 +66,19 @@ class Builder
$ignore['files'][] = 'config.json';
}
while (($file = readdir($dh)) !== false) {
if ($file == '.' || $file == '..') {
/** @var \SplFileInfo $file */
foreach ($it as $file) {
if (static::isIgnored($file, $ignore)) {
continue;
}
$path = $node->getPath() . DIRECTORY_SEPARATOR . $file;
if (is_dir($path) && in_array($file, $ignore['folders'])) {
continue;
}
if (!is_dir($path) && in_array($file, $ignore['files'])) {
continue;
}
if (is_dir($path)) {
$new = new Directory($node, static::removeSortingInformations(static::getFilename($path)), $path);
$new->setName(DauxHelper::pathinfo($path)['filename']);
if ($file->isDir()) {
$new = new Directory($node, static::removeSortingInformations($file->getFilename()), $file);
$new->setName(static::getName($file->getPathName()));
$new->setTitle(static::removeSortingInformations($new->getName(), ' '));
static::build($new, $ignore);
} else {
static::createContent($node, $path);
static::createContent($node, $file);
}
}
@ -51,19 +87,19 @@ class Builder
/**
* @param Directory $parent
* @param string $path
* @param SplFileInfo $file
* @return Content|Raw
*/
public static function createContent(Directory $parent, $path)
public static function createContent(Directory $parent, SplFileInfo $file)
{
$name = DauxHelper::pathinfo($path)['filename'];
$name = static::getName($file->getPathname());
$config = $parent->getConfig();
if (!in_array(pathinfo($path, PATHINFO_EXTENSION), $config['valid_content_extensions'])) {
$uri = static::removeSortingInformations(static::getFilename($path));
if (!in_array($file->getExtension(), $config['valid_content_extensions'])) {
$uri = static::removeSortingInformations($file->getFilename());
$entry = new Raw($parent, $uri, $path, filemtime($path));
$entry = new Raw($parent, $uri, $file);
$entry->setTitle(static::removeSortingInformations($name, ' '));
$entry->setName($name);
@ -75,7 +111,7 @@ class Builder
$uri .= '.html';
}
$entry = new Content($parent, $uri, $path, filemtime($path));
$entry = new Content($parent, $uri, $file);
if ($entry->getUri() == $config['index_key']) {
if ($parent instanceof Root) {
@ -92,16 +128,6 @@ class Builder
return $entry;
}
/**
* @param string $file
* @return string
*/
protected static function getFilename($file)
{
$parts = explode(DIRECTORY_SEPARATOR, $file);
return end($parts);
}
/**
* @param string $filename
* @return string
@ -151,10 +177,10 @@ class Builder
*/
public static function getOrCreatePage(Directory $parent, $path)
{
$title = DauxHelper::pathinfo($path)['filename'];
$title = static::getName($path);
// If the file doesn't have an extension, set .md as a default
if (DauxHelper::pathinfo($path)['extension'] == '') {
if (pathinfo($path, PATHINFO_EXTENSION) == '') {
$path .= '.md';
}
@ -172,7 +198,7 @@ class Builder
if ($title == 'index') {
// TODO :: clarify the difference between 'index' and '_index'
$page->setName('_index' . DauxHelper::pathinfo($path)['extension']);
$page->setName('_index' . pathinfo($path, PATHINFO_EXTENSION));
$page->setTitle($parent->getTitle());
} else {
$page->setName($path);

Bestand weergeven

@ -1,7 +1,5 @@
<?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\DauxHelper;
class Content extends Entry
{
/**
@ -28,13 +26,4 @@ class Content extends Entry
{
$this->content = $content;
}
/**
* @param string $file
* @return string
*/
protected function getFilename($file)
{
return DauxHelper::pathinfo($file)['filename'];
}
}

Bestand weergeven

@ -1,5 +1,7 @@
<?php namespace Todaymade\Daux\Tree;
use SplFileInfo;
abstract class Entry
{
/** @var string */
@ -14,29 +16,25 @@ abstract class Entry
/** @var Directory */
protected $parent;
/** @var SplFileInfo */
protected $info;
/** @var string */
protected $path;
/** @var integer */
protected $last_modified;
/**
* @param Directory $parent
* @param string $uri
* @param string $path
* @param integer $last_modified
* @param SplFileInfo $info
*/
public function __construct(Directory $parent, $uri, $path = null, $last_modified = null)
public function __construct(Directory $parent, $uri, SplFileInfo $info = null)
{
$this->setUri($uri);
$this->setParent($parent);
if ($path !== null) {
$this->path = $path;
}
if ($last_modified !== null) {
$this->last_modified = $last_modified;
if ($info !== null) {
$this->info = $info;
$this->path = $info->getPathname();
}
}
@ -141,6 +139,14 @@ abstract class Entry
return $this->path;
}
/**
* @return SplFileInfo
*/
public function getFileinfo()
{
return $this->info;
}
/**
* @return string
*/