f23db7e17faea3f6d7569b27b1c7d9bd11da0fb1
[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     public function getVc()
46     {
47         return new \VersionControl_Git($this->repoDir);
48     }
49
50     /**
51      * Loads the list of files in this repository
52      *
53      * @return File[] Array of files
54      */
55     public function getFiles()
56     {
57         $files = glob($this->repoDir . '/*');
58         $arFiles = array();
59         foreach ($files as $path) {
60             $arFiles[] = new File($path, $this);
61         }
62         return $arFiles;
63     }
64
65     public function getFileByName($name)
66     {
67         $base = basename($name);
68         if ($base != $name) {
69             throw new Exception('No directories supported for now');
70         }
71         $path = $this->repoDir . '/' . $base;
72         if (!is_readable($path)) {
73             throw new Exception_Input('File does not exist');
74         }
75         return new File($path, $this);
76     }
77
78     public function getDescription()
79     {
80         if (!is_readable($this->repoDir . '/.git/description')) {
81             return null;
82         }
83         return file_get_contents($this->repoDir . '/.git/description');
84     }
85
86     /**
87      * Get a link to the repository
88      *
89      * @param string $type Link type. Supported are:
90      *                     - "edit"
91      *                     - "display"
92      *                     - "fork"
93      *
94      * @return string
95      */
96     public function getLink($type)
97     {
98         if ($type == 'edit') {
99             return '/' . $this->id . '/edit';
100         } else if ($type == 'display') {
101             return '/' . $this->id;
102         } else if ($type == 'fork') {
103             return '/' . $this->id . '/fork';
104         }
105         throw new Exception('Unknown type');
106     }
107
108 }
109
110 ?>