@@ -12,12 +12,17 @@ class Theme {
12
12
protected $ page = NULL ;
13
13
protected $ class = NULL ;
14
14
protected $ features = NULL ;
15
+ protected $ useCache = FALSE ;
15
16
16
- public function __construct ($ app ) {
17
+ public function __construct ($ app, $ useCache = TRUE ) {
17
18
$ this ->app = $ app ;
18
19
$ this ->class = new \ReflectionClass ($ this );
19
20
$ this ->features = array ();
20
21
$ app ->theme = $ this ;
22
+ $ this ->useCache = $ useCache ;
23
+ if ($ this ->useCache ) {
24
+ $ this ->initCache ();
25
+ }
21
26
}
22
27
23
28
/** The entry function for the theme. Renders the complete page in this theme.
@@ -47,6 +52,10 @@ public function render($page) {
47
52
}
48
53
49
54
$ html = $ layout ->renderPage ();
55
+
56
+ if ($ this ->useCache ) {
57
+ $ this ->saveCache ();
58
+ }
50
59
echo $ html ;
51
60
}
52
61
@@ -123,15 +132,8 @@ public function renderComponent($component) {
123
132
public function getRenderer ($ component ) {
124
133
// An annotation can overwrite the renderer
125
134
$ 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
-
133
135
if ($ renderer == NULL ) {
134
- $ renderer = new Renderer ( $ this , $ component );
136
+ $ renderer = $ this -> findCachedRenderer ( $ component );
135
137
}
136
138
return $ renderer ;
137
139
}
@@ -144,6 +146,33 @@ protected function getAnnotatedRenderer($component) {
144
146
return $ rc ;
145
147
}
146
148
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
+
147
176
protected function searchRendererInNamespace ($ namespace , $ component ) {
148
177
$ class = new \ReflectionClass ($ component );
149
178
$ rc = NULL ;
@@ -188,5 +217,25 @@ public function getErrorPage($htmlCode, $text, $throwable = NULL) {
188
217
if ($ rc == NULL ) $ rc = new \WebApp \Error \ErrorPage ($ this ->app , $ htmlCode , $ text , $ throwable );
189
218
return $ rc ;
190
219
}
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
+
191
240
}
192
241
0 commit comments