5 class Tree
implements \RecursiveIterator
{
13 * List of first level entries
16 private $list = array();
26 * @param \mdref\Repo $repo
28 public function __construct($path, Repo
$repo) {
29 if (realpath($path)."/" === $repo->getPath()) {
30 $list = [$path ."/". $repo->getName() .".md"];
31 } elseif (!($list = glob("$path/*.md"))) {
32 $list = glob("$path/*/*.md");
35 $this->list = array_filter($list, $this->generateFilter($list));
36 usort($this->list, $this->generateSorter());
45 private function generateFilter(array $list) {
46 return function($v) use($list) {
50 if (false !== array_search("$v.md", $list, true)) {
55 if (isset($pi["extension"]) && "md" !== $pi["extension"]) {
66 private function generateSorter() {
67 return function($a, $b) {
68 $ab = basename($a, ".md");
69 $bb = basename($b, ".md");
71 if ($ab{0} === ":" && $bb{0} === ":") {
72 return strcmp($ab, $bb);
73 } elseif ($ab{0} === ":") {
75 } elseif ($bb{0} === ":") {
79 $ad = is_dir(dirname($a)."/$ab");
80 $bd = is_dir(dirname($b)."/$bb");
83 return strcmp($ab, $bb);
90 $au = preg_match("/^\p{Lu}/", $ab);
91 $bu = preg_match("/^\p{Lu}/", $bb);
94 return strcmp($ab, $bb);
101 return strcmp($ab, $bb);
106 * Implements \Iterator
107 * @return \mdref\Entry
109 public function current() {
110 return $this->repo
->getEntry($this->repo
->hasFile(current($this->iter
)));
114 * Implements \Iterator
116 public function next() {
121 * Implements \Iterator
124 public function key() {
125 return key($this->iter
);
129 * Implements \Iterator
131 public function rewind() {
132 $this->iter
= $this->list;
137 * Implements \Iterator
140 public function valid() {
141 return null !== key($this->iter
);
145 * Implements \RecursiveIterator
148 public function hasChildren() {
149 return $this->current()->hasIterator();
153 * Implements \RecursiveIterator
154 * @return \mdref\Tree
156 public function getChildren() {
157 return $this->current()->getIterator();