aboutsummaryrefslogtreecommitdiff
path: root/src/Phorkie/Repository.php
blob: aeccc72f826107a2465a4a9d4023490bc6b83175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Phorkie;


class Repository
{
    /**
     * Repository ID (number in repositories directory)
     *
     * @var integer
     */
    public $id;

    /**
     * Full path to the git repository
     *
     * @var string
     */
    public $repoDir;

    /**
     * Load Repository data from GET-Request
     *
     * @return void
     *
     * @throws Exception When something is wrong
     */
    public function loadFromRequest()
    {
        if (!isset($_GET['id'])) {
            throw new Exception_Input('Paste ID missing');
        }
        if (!is_numeric($_GET['id'])) {
            throw new Exception_Input('Paste ID not numeric');
        }
        $this->id = (int)$_GET['id'];

        $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
        if (!is_dir($repoDir)) {
            throw new Exception_NotFound('Paste not found');
        }
        $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
     *
     * @return File[] Array of files
     */
    public function getFiles()
    {
        $files = glob($this->repoDir . '/*');
        $arFiles = array();
        foreach ($files as $path) {
            $arFiles[] = new File($path, $this);
        }
        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');
    }

    /**
     * Get a link to the repository
     *
     * @param string $type Link type. Supported are:
     *                     - "edit"
     *                     - "display"
     *                     - "fork"
     *
     * @return string
     */
    public function getLink($type)
    {
        if ($type == 'edit') {
            return '/' . $this->id . '/edit';
        } else if ($type == 'display') {
            return '/' . $this->id;
        } else if ($type == 'fork') {
            return '/' . $this->id . '/fork';
        }
        throw new Exception('Unknown type');
    }

}

?>