From db520e36e6ac43f5e4766c017ec8a6399345fc7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Sun, 16 Aug 2015 21:19:55 +0200 Subject: [PATCH] Use DirectoryIterator to build the tree --- libs/DauxHelper.php | 18 -------- libs/Tree/Builder.php | 98 +++++++++++++++++++++++++++---------------- libs/Tree/Content.php | 11 ----- libs/Tree/Entry.php | 30 +++++++------ 4 files changed, 80 insertions(+), 77 deletions(-) diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index 9d3e53f..fe93bd1 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -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 * diff --git a/libs/Tree/Builder.php b/libs/Tree/Builder.php index da0ba3d..c46c1f1 100644 --- a/libs/Tree/Builder.php +++ b/libs/Tree/Builder.php @@ -1,10 +1,54 @@ 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); diff --git a/libs/Tree/Content.php b/libs/Tree/Content.php index 420733a..d7df3ce 100644 --- a/libs/Tree/Content.php +++ b/libs/Tree/Content.php @@ -1,7 +1,5 @@ content = $content; } - - /** - * @param string $file - * @return string - */ - protected function getFilename($file) - { - return DauxHelper::pathinfo($file)['filename']; - } } diff --git a/libs/Tree/Entry.php b/libs/Tree/Entry.php index 2d989e3..07dffc0 100644 --- a/libs/Tree/Entry.php +++ b/libs/Tree/Entry.php @@ -1,5 +1,7 @@ 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 */