Use DirectoryIterator to build the tree
This commit is contained in:
parent
26be012128
commit
db520e36e6
@ -136,24 +136,6 @@ class DauxHelper
|
|||||||
return implode(DIRECTORY_SEPARATOR, $absolutes);
|
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
|
* Locate a file in the tree. Returns the file if found or false
|
||||||
*
|
*
|
||||||
|
@ -1,10 +1,54 @@
|
|||||||
<?php namespace Todaymade\Daux\Tree;
|
<?php namespace Todaymade\Daux\Tree;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
use SplFileInfo;
|
||||||
use Todaymade\Daux\Daux;
|
use Todaymade\Daux\Daux;
|
||||||
use Todaymade\Daux\DauxHelper;
|
use Todaymade\Daux\DauxHelper;
|
||||||
|
|
||||||
class Builder
|
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
|
* Build the initial tree
|
||||||
*
|
*
|
||||||
@ -13,7 +57,7 @@ class Builder
|
|||||||
*/
|
*/
|
||||||
public static function build($node, $ignore)
|
public static function build($node, $ignore)
|
||||||
{
|
{
|
||||||
if (!$dh = opendir($node->getPath())) {
|
if (!$it = new \FilesystemIterator($node->getPath())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,27 +66,19 @@ class Builder
|
|||||||
$ignore['files'][] = 'config.json';
|
$ignore['files'][] = 'config.json';
|
||||||
}
|
}
|
||||||
|
|
||||||
while (($file = readdir($dh)) !== false) {
|
/** @var \SplFileInfo $file */
|
||||||
if ($file == '.' || $file == '..') {
|
foreach ($it as $file) {
|
||||||
|
if (static::isIgnored($file, $ignore)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$path = $node->getPath() . DIRECTORY_SEPARATOR . $file;
|
if ($file->isDir()) {
|
||||||
|
$new = new Directory($node, static::removeSortingInformations($file->getFilename()), $file);
|
||||||
if (is_dir($path) && in_array($file, $ignore['folders'])) {
|
$new->setName(static::getName($file->getPathName()));
|
||||||
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']);
|
|
||||||
$new->setTitle(static::removeSortingInformations($new->getName(), ' '));
|
$new->setTitle(static::removeSortingInformations($new->getName(), ' '));
|
||||||
static::build($new, $ignore);
|
static::build($new, $ignore);
|
||||||
} else {
|
} else {
|
||||||
static::createContent($node, $path);
|
static::createContent($node, $file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,19 +87,19 @@ class Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Directory $parent
|
* @param Directory $parent
|
||||||
* @param string $path
|
* @param SplFileInfo $file
|
||||||
* @return Content|Raw
|
* @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();
|
$config = $parent->getConfig();
|
||||||
|
|
||||||
if (!in_array(pathinfo($path, PATHINFO_EXTENSION), $config['valid_content_extensions'])) {
|
if (!in_array($file->getExtension(), $config['valid_content_extensions'])) {
|
||||||
$uri = static::removeSortingInformations(static::getFilename($path));
|
$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->setTitle(static::removeSortingInformations($name, ' '));
|
||||||
$entry->setName($name);
|
$entry->setName($name);
|
||||||
|
|
||||||
@ -75,7 +111,7 @@ class Builder
|
|||||||
$uri .= '.html';
|
$uri .= '.html';
|
||||||
}
|
}
|
||||||
|
|
||||||
$entry = new Content($parent, $uri, $path, filemtime($path));
|
$entry = new Content($parent, $uri, $file);
|
||||||
|
|
||||||
if ($entry->getUri() == $config['index_key']) {
|
if ($entry->getUri() == $config['index_key']) {
|
||||||
if ($parent instanceof Root) {
|
if ($parent instanceof Root) {
|
||||||
@ -92,16 +128,6 @@ class Builder
|
|||||||
return $entry;
|
return $entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $file
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected static function getFilename($file)
|
|
||||||
{
|
|
||||||
$parts = explode(DIRECTORY_SEPARATOR, $file);
|
|
||||||
return end($parts);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $filename
|
* @param string $filename
|
||||||
* @return string
|
* @return string
|
||||||
@ -151,10 +177,10 @@ class Builder
|
|||||||
*/
|
*/
|
||||||
public static function getOrCreatePage(Directory $parent, $path)
|
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 the file doesn't have an extension, set .md as a default
|
||||||
if (DauxHelper::pathinfo($path)['extension'] == '') {
|
if (pathinfo($path, PATHINFO_EXTENSION) == '') {
|
||||||
$path .= '.md';
|
$path .= '.md';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,7 +198,7 @@ class Builder
|
|||||||
|
|
||||||
if ($title == 'index') {
|
if ($title == 'index') {
|
||||||
// TODO :: clarify the difference between 'index' and '_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());
|
$page->setTitle($parent->getTitle());
|
||||||
} else {
|
} else {
|
||||||
$page->setName($path);
|
$page->setName($path);
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
<?php namespace Todaymade\Daux\Tree;
|
<?php namespace Todaymade\Daux\Tree;
|
||||||
|
|
||||||
use Todaymade\Daux\DauxHelper;
|
|
||||||
|
|
||||||
class Content extends Entry
|
class Content extends Entry
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -28,13 +26,4 @@ class Content extends Entry
|
|||||||
{
|
{
|
||||||
$this->content = $content;
|
$this->content = $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $file
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function getFilename($file)
|
|
||||||
{
|
|
||||||
return DauxHelper::pathinfo($file)['filename'];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
<?php namespace Todaymade\Daux\Tree;
|
<?php namespace Todaymade\Daux\Tree;
|
||||||
|
|
||||||
|
use SplFileInfo;
|
||||||
|
|
||||||
abstract class Entry
|
abstract class Entry
|
||||||
{
|
{
|
||||||
/** @var string */
|
/** @var string */
|
||||||
@ -14,29 +16,25 @@ abstract class Entry
|
|||||||
/** @var Directory */
|
/** @var Directory */
|
||||||
protected $parent;
|
protected $parent;
|
||||||
|
|
||||||
|
/** @var SplFileInfo */
|
||||||
|
protected $info;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $path;
|
protected $path;
|
||||||
|
|
||||||
/** @var integer */
|
|
||||||
protected $last_modified;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Directory $parent
|
* @param Directory $parent
|
||||||
* @param string $uri
|
* @param string $uri
|
||||||
* @param string $path
|
* @param SplFileInfo $info
|
||||||
* @param integer $last_modified
|
|
||||||
*/
|
*/
|
||||||
public function __construct(Directory $parent, $uri, $path = null, $last_modified = null)
|
public function __construct(Directory $parent, $uri, SplFileInfo $info = null)
|
||||||
{
|
{
|
||||||
$this->setUri($uri);
|
$this->setUri($uri);
|
||||||
$this->setParent($parent);
|
$this->setParent($parent);
|
||||||
|
|
||||||
if ($path !== null) {
|
if ($info !== null) {
|
||||||
$this->path = $path;
|
$this->info = $info;
|
||||||
}
|
$this->path = $info->getPathname();
|
||||||
|
|
||||||
if ($last_modified !== null) {
|
|
||||||
$this->last_modified = $last_modified;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,6 +139,14 @@ abstract class Entry
|
|||||||
return $this->path;
|
return $this->path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return SplFileInfo
|
||||||
|
*/
|
||||||
|
public function getFileinfo()
|
||||||
|
{
|
||||||
|
return $this->info;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user