glob returns false on openshift
[mdref/mdref] / mdref / Tree.php
1 <?php
2
3 namespace mdref;
4
5 class Tree implements \RecursiveIterator {
6 /**
7 * The repository
8 * @var \mdref\Repo
9 */
10 private $repo;
11
12 /**
13 * List of first level entries
14 * @var array
15 */
16 private $list = array();
17
18 /**
19 * The list iterator
20 * @var array
21 */
22 private $iter;
23
24 /**
25 * @param string $path
26 * @param \mdref\Repo $repo
27 */
28 public function __construct($path, Repo $repo) {
29 if (!($list = glob("$path/*.md"))) {
30 $list = glob("$path/*/*.md");
31 }
32 if ($list) {
33 $this->list = array_filter($list, $this->generateFilter($list));
34 sort($this->list, SORT_STRING);
35 }
36 $this->repo = $repo;
37 }
38
39 /**
40 * @param array $list
41 * @return callable
42 */
43 private function generateFilter(array $list) {
44 return function($v) use($list) {
45 if ($v{0} === ".") {
46 return false;
47 }
48 if (false !== array_search("$v.md", $list, true)) {
49 return false;
50 }
51
52 $pi = pathinfo($v);
53 if (isset($pi["extension"]) && "md" !== $pi["extension"]) {
54 return false;
55 }
56
57 return true;
58 };
59 }
60
61 /**
62 * Implements \Iterator
63 * @return \mdref\Entry
64 */
65 public function current() {
66 return $this->repo->getEntry($this->repo->hasFile(current($this->iter)));
67 }
68
69 /**
70 * Implements \Iterator
71 */
72 public function next() {
73 next($this->iter);
74 }
75
76 /**
77 * Implements \Iterator
78 * @return int
79 */
80 public function key() {
81 return key($this->iter);
82 }
83
84 /**
85 * Implements \Iterator
86 */
87 public function rewind() {
88 $this->iter = $this->list;
89 reset($this->iter);
90 }
91
92 /**
93 * Implements \Iterator
94 * @return bool
95 */
96 public function valid() {
97 return null !== key($this->iter);
98 }
99
100 /**
101 * Implements \RecursiveIterator
102 * @return bool
103 */
104 public function hasChildren() {
105 return $this->current()->hasIterator();
106 }
107
108 /**
109 * Implements \RecursiveIterator
110 * @return \mdref\Tree
111 */
112 public function getChildren() {
113 return $this->current()->getIterator();
114 }
115 }