6 use InvalidArgumentException
;
10 use function file_get_contents
;
13 use function realpath
;
21 class Repo
implements IteratorAggregate
{
23 * The name of the repository
29 * The path to the repository
35 * The edit url template
41 * Path to the repository containing the name.mdref file
43 * @throws \InvalidArgumentException
45 public function __construct(string $path) {
46 if (!($mdref = current(glob("$path/*.mdref")))) {
47 throw new InvalidArgumentException(
48 sprintf("Not a reference, could not find '*.mdref': '%s'", $path));
51 $this->path
= realpath($path);
52 $this->name
= basename($mdref, ".mdref");
53 $this->edit
= trim(file_get_contents($mdref));
57 * Get the repository's name
60 public function getName() : string {
65 * Get the path of the repository or a file in it
69 public function getPath(string $file = "") : string {
70 return $this->path
. "/$file";
74 * Get the edit url for a ref entry
75 * @param string $entry
78 public function getEditUrl(string $entry) : string {
79 return sprintf($this->edit
, $entry);
83 * Get the file path of an entry in this repo
85 * @param string $entry
86 * @param string|null $canonical
87 * @return string file path
89 public function hasEntry(string $entry, ?
string &$canonical = null) : ?
string {
90 $trim = rtrim($entry, "/");
91 $file = $this->getPath("$trim.md");
93 if ($trim !== $entry) {
98 $file = $this->getPath($this->getName()."/$entry.md");
100 $canonical = $this->getName() . "/" . $entry;
107 * Get the canonical entry name of a file in this repo
108 * @param string $file
109 * @return string entry
111 public function hasFile(string $file) : ?
string {
112 if (($file = realpath($file))) {
113 $path = $this->getPath();
114 $plen = strlen($path);
115 if (!strncmp($file, $path, $plen)) {
116 $dirname = dirname(substr($file, $plen));
117 $basename = basename($file, ".md");
119 if ($dirname === ".") {
123 return $dirname . "/". $basename;
130 * Check whether the repo has a stub file to serve
131 * @param string|null $path receives the path if there's a stub
134 public function hasStub(string &$path = null) : bool {
135 $path = $this->getPath($this->getName() . ".stub.php");
136 return is_file($path) && is_readable($path);
140 * Get an Entry instance
141 * @param string $entry
142 * @return \mdref\Entry
143 * @throws \OutOfBoundsException
145 public function getEntry(string $entry) : Entry
{
146 return new Entry($entry, $this);
150 * Get the root Entry instance
151 * @return \mdref\Entry
153 public function getRootEntry() : Entry
{
154 return new Entry($this->name
, $this);
158 * Implements \IteratorAggregate
159 * @return \mdref\Tree
161 public function getIterator() : Tree
{
162 return new Tree($this->path
, $this);