aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2015-07-07 16:10:56 +0200
committerChristian Weiske <cweiske@cweiske.de>2015-07-07 16:10:56 +0200
commit2cd81a74dcf24acf85c86639ad60a140037d4451 (patch)
tree4bc925ee86b2b2060f774e6270134cde06571db6 /src
parent246ac1d5966639f2036795d859cb2b195c762236 (diff)
downloadphorkie-2cd81a74dcf24acf85c86639ad60a140037d4451.tar.gz
phorkie-2cd81a74dcf24acf85c86639ad60a140037d4451.zip
simple cache for rendered files
Diffstat (limited to 'src')
-rw-r--r--src/phorkie/File.php18
-rw-r--r--src/phorkie/Renderer/Cache.php84
-rw-r--r--src/phorkie/Repository.php1
-rw-r--r--src/phorkie/SetupCheck.php1
4 files changed, 87 insertions, 17 deletions
diff --git a/src/phorkie/File.php b/src/phorkie/File.php
index 2afda4c..300e810 100644
--- a/src/phorkie/File.php
+++ b/src/phorkie/File.php
@@ -84,22 +84,8 @@ class File
public function getRenderedContent(Tool_Result $res = null)
{
- $ext = $this->getExt();
- $class = '\\phorkie\\Renderer_Unknown';
-
- if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
- $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
- } else if ($this->isText()) {
- $class = '\\phorkie\\Renderer_Geshi';
- } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
- $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
- if (substr($type, 0, 6) == 'image/') {
- $class = '\\phorkie\\Renderer_Image';
- }
- }
-
- $rend = new $class();
- return $rend->toHtml($this, $res);
+ $cache = new Renderer_Cache();
+ return $cache->toHtml($this, $res);
}
/**
diff --git a/src/phorkie/Renderer/Cache.php b/src/phorkie/Renderer/Cache.php
new file mode 100644
index 0000000..4137b05
--- /dev/null
+++ b/src/phorkie/Renderer/Cache.php
@@ -0,0 +1,84 @@
+<?php
+namespace phorkie;
+
+class Renderer_Cache
+{
+ /**
+ * Converts the code to HTML by fetching it from cache,
+ * or by letting the other renderes generate it and then
+ * storing it in the cache.
+ *
+ * @param File $file File to render
+ * @param Tool_Result $res Tool result to integrate
+ *
+ * @return string HTML
+ */
+ public function toHtml(File $file, Tool_Result $res = null)
+ {
+ $html = null;
+ $cacheFile = $this->getCacheFile($file);
+ if ($res === null && $cacheFile !== null) {
+ $html = $this->loadHtmlFromCache($cacheFile);
+ }
+ if ($html === null) {
+ $html = $this->renderFile($file, $res);
+ if ($res === null && $cacheFile !== null) {
+ $this->storeHtmlIntoCache($cacheFile, $html);
+ }
+ }
+ return $html;
+ }
+
+ protected function renderFile(File $file, Tool_Result $res = null)
+ {
+ $ext = $file->getExt();
+ $class = '\\phorkie\\Renderer_Unknown';
+
+ if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
+ $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
+ } else if ($file->isText()) {
+ $class = '\\phorkie\\Renderer_Geshi';
+ } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
+ $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
+ if (substr($type, 0, 6) == 'image/') {
+ $class = '\\phorkie\\Renderer_Image';
+ }
+ }
+
+ $rend = new $class();
+ return $rend->toHtml($file, $res);
+ }
+
+ /**
+ * @return null|string NULL when there is no cache, string with HTML
+ * otherwise
+ */
+ protected function loadHtmlFromCache($cacheFile)
+ {
+ if (!file_exists($cacheFile)) {
+ return null;
+ }
+ return file_get_contents($cacheFile);
+ }
+
+ protected function storeHtmlIntoCache($cacheFile, $html)
+ {
+ file_put_contents($cacheFile, $html);
+ }
+
+ protected function getCacheFile(File $file)
+ {
+ if (!$GLOBALS['phorkie']['cfg']['cachedir']
+ || !is_dir($GLOBALS['phorkie']['cfg']['cachedir'])
+ ) {
+ return null;
+ }
+
+ return $GLOBALS['phorkie']['cfg']['cachedir']
+ . '/' . $file->repo->id
+ . '-' . $file->repo->hash
+ . '-' . str_replace('/', '-', $file->getFilename())
+ . '.html';
+ }
+}
+?>
diff --git a/src/phorkie/Repository.php b/src/phorkie/Repository.php
index 8cf5ff6..62a6e38 100644
--- a/src/phorkie/Repository.php
+++ b/src/phorkie/Repository.php
@@ -96,7 +96,6 @@ class Repository
public function loadHash()
{
- return;
if ($this->hash !== null) {
return;
}
diff --git a/src/phorkie/SetupCheck.php b/src/phorkie/SetupCheck.php
index 32fb79e..9cae724 100644
--- a/src/phorkie/SetupCheck.php
+++ b/src/phorkie/SetupCheck.php
@@ -26,6 +26,7 @@ class SetupCheck
$this->writableDirs = array(
'gitdir' => Tools::foldPath($cfg['gitdir']),
'workdir' => Tools::foldPath($cfg['workdir']),
+ 'cachedir' => Tools::foldPath($cfg['cachedir']),
);
$this->elasticsearch = $cfg['elasticsearch'];
}