listing all pastes works
[phorkie.git] / src / Phorkie / Repository.php
index ae01f6c6868479c074b9c5d049a7012adb4138c2..aeccc72f826107a2465a4a9d4023490bc6b83175 100644 (file)
@@ -42,6 +42,25 @@ class Repository
         $this->repoDir = $repoDir;
     }
 
+    public function loadById($id)
+    {
+        if (!is_numeric($id)) {
+            throw new Exception_Input('Paste ID not numeric');
+        }
+        $this->id = (int)$id;
+
+        $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
+        if (!is_dir($repoDir)) {
+            throw new Exception_NotFound('Paste not found');
+        }
+        $this->repoDir = $repoDir;
+    }
+
+    public function getVc()
+    {
+        return new \VersionControl_Git($this->repoDir);
+    }
+
     /**
      * Loads the list of files in this repository
      *
@@ -57,8 +76,24 @@ class Repository
         return $arFiles;
     }
 
+    public function getFileByName($name)
+    {
+        $base = basename($name);
+        if ($base != $name) {
+            throw new Exception('No directories supported for now');
+        }
+        $path = $this->repoDir . '/' . $base;
+        if (!is_readable($path)) {
+            throw new Exception_Input('File does not exist');
+        }
+        return new File($path, $this);
+    }
+
     public function getDescription()
     {
+        if (!is_readable($this->repoDir . '/.git/description')) {
+            return null;
+        }
         return file_get_contents($this->repoDir . '/.git/description');
     }
 
@@ -68,6 +103,7 @@ class Repository
      * @param string $type Link type. Supported are:
      *                     - "edit"
      *                     - "display"
+     *                     - "fork"
      *
      * @return string
      */
@@ -77,6 +113,8 @@ class Repository
             return '/' . $this->id . '/edit';
         } else if ($type == 'display') {
             return '/' . $this->id;
+        } else if ($type == 'fork') {
+            return '/' . $this->id . '/fork';
         }
         throw new Exception('Unknown type');
     }