9 class Repo
implements \IteratorAggregate
{
11 * The name of the repository
17 * The path to the repository
23 * The edit url template
29 * Path to the repository containing the name.mdref file
31 * @throws \InvalidArgumentException
33 public function __construct($path) {
34 if (!($mdref = current(glob("$path/*.mdref")))) {
35 throw new \
InvalidArgumentException(
36 sprintf("Not a reference, could not find '*.mdref': '%s'",
40 $this->path
= realpath($path);
41 $this->name
= basename($mdref, ".mdref");
42 $this->edit
= trim(file_get_contents($mdref));
46 * Get the repository's name
49 public function getName() {
54 * Get the path of the repository or a file in it
58 public function getPath($file = "") {
59 return $this->path
. "/$file";
63 * Get the edit url for a ref entry
64 * @param string $entry
67 public function getEditUrl($entry) {
68 return sprintf($this->edit
, $entry);
72 * Get the file path of an entry in this repo
73 * @param string $entry
74 * @return string file path
76 public function hasEntry($entry, &$canonical = null) {
77 $trim = rtrim($entry, "/");
78 $file = $this->getPath("$trim.md");
80 if ($trim !== $entry) {
85 $file = $this->getPath($this->getName()."/$entry.md");
87 $canonical = $this->getName() . "/" . $entry;
93 * Get the canonical entry name of a file in this repo
95 * @return string entry
97 public function hasFile($file) {
98 if (($file = realpath($file))) {
99 $path = $this->getPath();
100 $plen = strlen($path);
101 if (!strncmp($file, $path, $plen)) {
102 $dirname = dirname(substr($file, $plen));
103 $basename = basename($file, ".md");
105 if ($dirname === ".") {
109 return $dirname . "/". $basename;
114 public function hasStub(&$path = null) {
115 $path = $this->getPath($this->getName() . ".stub.php");
116 return is_file($path) && is_readable($path);
120 * Get an Entry instance
121 * @param string $entry
122 * @return \mdref\Entry
123 * @throws \OutOfBoundsException
125 public function getEntry($entry) {
126 return new Entry($entry, $this);
130 * Get the root Entry instance
131 * @return \mdref\Entry
133 public function getRootEntry() {
134 return new Entry($this->name
, $this);
138 * Implements \IteratorAggregate
139 * @return \mdref\Tree
141 public function getIterator() {
142 return new Tree($this->path
, $this);