use anonymous image
[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 getRenderedContent(Tool_Result $res = null)
62     {
63         $ext   = $this->getExt();
64         $class = '\\phorkie\\Renderer_Unknown';
65
66         if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
67             $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
68         } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
69             $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
70             if (substr($type, 0, 5) == 'text/') {
71                 $class = '\\phorkie\\Renderer_Geshi';
72             } else if (substr($type, 0, 6) == 'image/') {
73                 $class = '\\phorkie\\Renderer_Image';
74             }
75         }
76
77         $rend = new $class();
78         return $rend->toHtml($this, $res);
79     }
80
81     /**
82      * Get a link to the file
83      *
84      * @param string $type Link type. Supported are:
85      *                     - "raw"
86      *                     - "tool"
87      * @param string $option
88      *
89      * @return string
90      */
91     public function getLink($type, $option = null)
92     {
93         if ($type == 'raw') {
94             return '/' . $this->repo->id . '/raw/' . $this->getFilename();
95         } else if ($type == 'tool') {
96             return '/' . $this->repo->id . '/tool/' . $option . '/' . $this->getFilename();
97         }
98         throw new Exception('Unknown type');
99     }
100
101     public function getMimeType()
102     {
103         $ext = $this->getExt();
104         if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
105             return null;
106         }
107         return $GLOBALS['phorkie']['languages'][$ext]['mime'];
108     }
109
110     /**
111      * @return array Array of Tool_Info objects
112      */
113     public function getToolInfos()
114     {
115         $tm = new Tool_Manager();
116         return $tm->getSuitable($this);
117     }
118
119     /**
120      * Tells if the file contains textual content and is editable.
121      *
122      * @return boolean
123      */
124     public function isText()
125     {
126         $ext = $this->getExt();
127         if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
128             return false;
129         }
130
131         $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
132         return substr($type, 0, 5) === 'text/';
133     }
134 }
135
136 ?>