7 * Converts the code to HTML by fetching it from cache,
8 * or by letting the other renderes generate it and then
9 * storing it in the cache.
11 * @param File $file File to render
12 * @param Tool_Result $res Tool result to integrate
16 public function toHtml(File $file, Tool_Result $res = null)
19 $cacheFile = $this->getCacheFile($file);
20 if ($res === null && $cacheFile !== null) {
21 $html = $this->loadHtmlFromCache($cacheFile);
24 $html = $this->renderFile($file, $res);
25 if ($res === null && $cacheFile !== null) {
26 $this->storeHtmlIntoCache($cacheFile, $html);
32 protected function renderFile(File $file, Tool_Result $res = null)
34 $ext = $file->getExt();
35 $class = '\\phorkie\\Renderer_Unknown';
37 if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
38 $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
39 } else if ($file->isText()) {
40 $class = '\\phorkie\\Renderer_Geshi';
41 } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
42 $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
43 if (substr($type, 0, 6) == 'image/') {
44 $class = '\\phorkie\\Renderer_Image';
49 return $rend->toHtml($file, $res);
53 * @return null|string NULL when there is no cache, string with HTML
56 protected function loadHtmlFromCache($cacheFile)
58 if (!file_exists($cacheFile)) {
61 return file_get_contents($cacheFile);
64 protected function storeHtmlIntoCache($cacheFile, $html)
66 file_put_contents($cacheFile, $html);
69 protected function getCacheFile(File $file)
71 if (!$GLOBALS['phorkie']['cfg']['cachedir']
72 || !is_dir($GLOBALS['phorkie']['cfg']['cachedir'])
77 return $GLOBALS['phorkie']['cfg']['cachedir']
78 . '/' . $file->repo->id
79 . '-' . $file->repo->hash
80 . '-' . str_replace('/', '-', $file->getFilename())