Skip to content

Commit 1bab735

Browse files
committed
Add renderer cache (in memory only)
1 parent daf7389 commit 1bab735

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

src/WebApp/Theme.php

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ class Theme {
1212
protected $page = NULL;
1313
protected $class = NULL;
1414
protected $features = NULL;
15+
protected $useCache = FALSE;
1516

16-
public function __construct($app) {
17+
public function __construct($app, $useCache = TRUE) {
1718
$this->app = $app;
1819
$this->class = new \ReflectionClass($this);
1920
$this->features = array();
2021
$app->theme = $this;
22+
$this->useCache = $useCache;
23+
if ($this->useCache) {
24+
$this->initCache();
25+
}
2126
}
2227

2328
/** The entry function for the theme. Renders the complete page in this theme.
@@ -47,6 +52,10 @@ public function render($page) {
4752
}
4853

4954
$html = $layout->renderPage();
55+
56+
if ($this->useCache) {
57+
$this->saveCache();
58+
}
5059
echo $html;
5160
}
5261

@@ -123,15 +132,8 @@ public function renderComponent($component) {
123132
public function getRenderer($component) {
124133
// An annotation can overwrite the renderer
125134
$renderer = $this->getAnnotatedRenderer($component);
126-
$themeClass = $this->class;
127-
while (($renderer == NULL) && ($themeClass !== FALSE)) {
128-
$namespace = $themeClass->getNamespaceName();
129-
$renderer = $this->searchRendererInNamespace($namespace, $component);
130-
$themeClass = $themeClass->getParentClass();
131-
}
132-
133135
if ($renderer == NULL) {
134-
$renderer = new Renderer($this, $component);
136+
$renderer = $this->findCachedRenderer($component);
135137
}
136138
return $renderer;
137139
}
@@ -144,6 +146,33 @@ protected function getAnnotatedRenderer($component) {
144146
return $rc;
145147
}
146148

149+
protected function findCachedRenderer($component) {
150+
$renderer = NULL;
151+
if ($this->useCache) {
152+
$className = $this->getRendererCache(get_class($component));
153+
if ($className != NULL) {
154+
$renderer = new $className($this, $component);
155+
}
156+
}
157+
158+
if ($renderer == NULL) {
159+
$themeClass = $this->class;
160+
while (($renderer == NULL) && ($themeClass !== FALSE)) {
161+
$namespace = $themeClass->getNamespaceName();
162+
$renderer = $this->searchRendererInNamespace($namespace, $component);
163+
$themeClass = $themeClass->getParentClass();
164+
}
165+
166+
if ($renderer == NULL) {
167+
$renderer = new Renderer($this, $component);
168+
}
169+
if ($this->useCache) {
170+
$this->setRendererCache(get_class($component), get_class($renderer));
171+
}
172+
}
173+
return $renderer;
174+
}
175+
147176
protected function searchRendererInNamespace($namespace, $component) {
148177
$class = new \ReflectionClass($component);
149178
$rc = NULL;
@@ -188,5 +217,25 @@ public function getErrorPage($htmlCode, $text, $throwable = NULL) {
188217
if ($rc == NULL) $rc = new \WebApp\Error\ErrorPage($this->app, $htmlCode, $text, $throwable);
189218
return $rc;
190219
}
220+
221+
// Renderer Cache
222+
protected function initCache() {
223+
$this->rendererCache = array();
224+
// Loading from disk => concurrency!
225+
}
226+
227+
protected function saveCache() {
228+
// Saving to disk => concurrency!
229+
}
230+
231+
protected function getRendererCache($name) {
232+
if (isset($this->rendererCache[$name])) return $this->rendererCache[$name];
233+
return NULL;
234+
}
235+
236+
protected function setRendererCache($name, $value) {
237+
$this->rendererCache[$name] = $value;
238+
}
239+
191240
}
192241

0 commit comments

Comments
 (0)