531ff6872950fd4dafa9715fc0550a9dedbfc3ed
[phorkie.git] / src / Phorkie / Repository.php
1 <?php
2 namespace Phorkie;
3
4
5 class Repository
6 {
7     /**
8      * Repository ID (number in repositories directory)
9      *
10      * @var integer
11      */
12     public $id;
13
14     /**
15      * Full path to the git repository
16      *
17      * @var string
18      */
19     public $repoDir;
20
21     /**
22      * Load Repository data from GET-Request
23      *
24      * @return void
25      *
26      * @throws Exception When something is wrong
27      */
28     public function loadFromRequest()
29     {
30         if (!isset($_GET['id'])) {
31             throw new Exception_Input('Paste ID missing');
32         }
33         if (!is_numeric($_GET['id'])) {
34             throw new Exception_Input('Paste ID not numeric');
35         }
36         $this->id = (int)$_GET['id'];
37
38         $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
39         if (!is_dir($repoDir)) {
40             throw new Exception_NotFound('Paste not found');
41         }
42         $this->repoDir = $repoDir;
43     }
44
45     /**
46      * Loads the list of files in this repository
47      *
48      * @return File[] Array of files
49      */
50     public function getFiles()
51     {
52         $files = glob($this->repoDir . '/*');
53         $arFiles = array();
54         foreach ($files as $path) {
55             $arFiles[] = new File($path, $this);
56         }
57         return $arFiles;
58     }
59
60     public function getFileByName($name)
61     {
62         $base = basename($name);
63         if ($base != $name) {
64             throw new Exception('No directories supported for now');
65         }
66         $path = $this->repoDir . '/' . $base;
67         if (!is_readable($path)) {
68             throw new Exception_Input('File does not exist');
69         }
70         return new File($path, $this);
71     }
72
73     public function getDescription()
74     {
75         if (!is_readable($this->repoDir . '/.git/description')) {
76             return null;
77         }
78         return file_get_contents($this->repoDir . '/.git/description');
79     }
80
81     /**
82      * Get a link to the repository
83      *
84      * @param string $type Link type. Supported are:
85      *                     - "edit"
86      *                     - "display"
87      *
88      * @return string
89      */
90     public function getLink($type)
91     {
92         if ($type == 'edit') {
93             return '/' . $this->id . '/edit';
94         } else if ($type == 'display') {
95             return '/' . $this->id;
96         }
97         throw new Exception('Unknown type');
98     }
99
100 }
101
102 ?>