first tool supported: xmllint
[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      *                     - "tool"
79      * @param string $option
80      *
81      * @return string
82      */
83     public function getLink($type, $option = null)
84     {
85         if ($type == 'raw') {
86             return '/' . $this->repo->id . '/raw/' . $this->getFilename();
87         } else if ($type == 'tool') {
88             return '/' . $this->repo->id . '/tool/' . $option . '/' . $this->getFilename();
89         }
90         throw new Exception('Unknown type');
91     }
92
93     public function getMimeType()
94     {
95         $ext = $this->getExt();
96         if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
97             return null;
98         }
99         return $GLOBALS['phorkie']['languages'][$ext]['mime'];
100     }
101
102     /**
103      * @return array Array of Tool_Info objects
104      */
105     public function getToolInfos()
106     {
107         $tm = new Tool_Manager();
108         return $tm->getSuitable($this);
109     }
110 }
111
112 ?>