separate git and work directories - gives nicer public git clone urls
[phorkie.git] / src / phorkie / File.php
1 <?php
2 namespace phorkie;
3
4 class File
5 {
6     /**
7      * Full path to the file
8      *
9      * @var string
10      */
11     public $path;
12
13     /**
14      * Repository this file belongs to
15      *
16      * @var string
17      */
18     public $repo;
19
20     public function __construct($path, Repository $repo = null)
21     {
22         $this->path = $path;
23         $this->repo = $repo;
24     }
25
26     /**
27      * Get filename relative to the repository path
28      *
29      * @return string
30      */
31     public function getFilename()
32     {
33         return basename($this->path);
34     }
35
36     /**
37      * Return the full path to the file
38      *
39      * @return string
40      */
41     public function getPath()
42     {
43         return $this->path;
44     }
45
46     /**
47      * Get file extension without dot
48      *
49      * @return string
50      */
51     public function getExt()
52     {
53         return substr($this->path, strrpos($this->path, '.') + 1);
54     }
55
56     public function getContent()
57     {
58         return file_get_contents($this->path);
59     }
60
61     public function getHighlightedContent()
62     {
63         $ext = $this->getExt();
64         if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
65             $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
66         } else {
67             $class = '\\phorkie\\Renderer_Geshi';
68         }
69         $rend = new $class();
70         return $rend->toHtml($this);
71     }
72
73     /**
74      * Get a link to the file
75      *
76      * @param string $type Link type. Supported are:
77      *                     - "raw"
78      *                     - "display"
79      *
80      * @return string
81      */
82     public function getLink($type)
83     {
84         if ($type == 'raw') {
85             return '/' . $this->repo->id . '/raw/' . $this->getFilename();
86         }
87         throw new Exception('Unknown type');
88     }
89
90     public function getMimeType()
91     {
92         $ext = $this->getExt();
93         if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
94             return null;
95         }
96         return $GLOBALS['phorkie']['languages'][$ext]['mime'];
97     }
98 }
99
100 ?>