357f99174f52e6811eb09865bdfe29ef2758c699
7 use function array_filter
;
8 use function array_search
;
16 use function pathinfo
;
17 use function preg_match
;
22 class Tree
implements RecursiveIterator
{
30 * List of first level entries
33 private $list = array();
43 * @param \mdref\Repo $repo
45 public function __construct(string $path, Repo
$repo) {
46 if (realpath($path)."/" === $repo->getPath()) {
47 $list = [$path ."/". $repo->getName() .".md"];
48 } elseif (!($list = glob("$path/*.md"))) {
49 $list = glob("$path/*/*.md");
52 $this->list = array_filter($list, $this->generateFilter($list));
53 usort($this->list, $this->generateSorter());
62 private function generateFilter(array $list) : \Closure
{
63 return function($v) use($list) {
67 if (false !== array_search("$v.md", $list, true)) {
72 if (isset($pi["extension"]) && "md" !== $pi["extension"]) {
83 private function generateSorter() : \Closure
{
84 return function($a, $b) {
85 $ab = basename($a, ".md");
86 $bb = basename($b, ".md");
88 if ($ab{0} === ":" && $bb{0} === ":") {
89 return strcmp($ab, $bb);
90 } elseif ($ab{0} === ":") {
92 } elseif ($bb{0} === ":") {
96 $ad = is_dir(dirname($a)."/$ab");
97 $bd = is_dir(dirname($b)."/$bb");
100 return strcmp($ab, $bb);
107 $au = preg_match("/^\p{Lu}/", $ab);
108 $bu = preg_match("/^\p{Lu}/", $bb);
111 return strcmp($ab, $bb);
118 return strcmp($ab, $bb);
123 * Implements \Iterator
124 * @return \mdref\Entry
126 public function current() : Entry
{
127 return $this->repo
->getEntry($this->repo
->hasFile(current($this->iter
)));
131 * Implements \Iterator
133 public function next() : void
{
138 * Implements \Iterator
141 public function key() : int {
142 return key($this->iter
);
146 * Implements \Iterator
148 public function rewind() : void
{
149 $this->iter
= $this->list;
154 * Implements \Iterator
157 public function valid() : bool {
158 return null !== key($this->iter
);
162 * Implements \RecursiveIterator
165 public function hasChildren() : bool {
166 return $this->current()->hasIterator();
170 * Implements \RecursiveIterator
171 * @return \mdref\Tree
173 public function getChildren() : Iterator
{
174 return $this->current()->getIterator();