public
Description: Midgard Components Framework 3rd generation
Homepage: http://www.midgard-project.org
Clone URL: git://github.com/bergie/midcom.git
Cleaning evals away from templating engine, using include() instead
bergie (author)
Fri Apr 04 07:33:39 -0700 2008
commit  b6bf50aef24d00272cc980505acee763c6dc59d9
tree    a70b0c6c2bd4382dc51ec89310b31d3156de156d
parent  48ff6368ad0eade8136c69ca017e257e957dffaa
...
3
4
5
 
6
7
8
...
3
4
5
6
7
8
9
0
@@ -3,6 +3,7 @@ services_authorization: midcom_core_services_authorization_simple
0
 services_templating: midcom_core_services_templating_midgard
0
 services_sessioning: midcom_core_services_sessioning_midgard
0
 services_uimessages: midcom_core_services_uimessages_midgard
0
+cache_directory: __MIDGARDCACHE__/bergietest
0
 enable_benchmark: false
0
 enable_included_list: false
0
 enable_jquery_framework: false
...
35
36
37
38
39
40
41
42
43
44
 
45
46
47
...
35
36
37
 
 
 
38
39
40
 
41
42
43
44
0
@@ -35,13 +35,10 @@ elseif (isset($_MIDCOM))
0
 
0
 if (isset($_MIDCOM))
0
 {
0
- // Start output buffer so we can do templating
0
- ob_start();
0
-
0
     $_MIDCOM->templating->template();
0
 
0
     // Read contents from the output buffer and pass to MidCOM rendering
0
- $_MIDCOM->templating->display(ob_get_clean());
0
+ $_MIDCOM->templating->display();
0
 }
0
 else
0
 {
...
130
131
132
 
133
134
135
...
130
131
132
133
134
135
136
0
@@ -130,6 +130,7 @@ class midcom_core_services_dispatcher_manual implements midcom_core_services_dis
0
     public function set_route($route_id, array $arguments)
0
     {
0
         $this->route_id = $route_id;
0
+ $_MIDCOM->context->route_id = $this->route_id;
0
         $this->action_arguments = $arguments;
0
     }
0
 
...
296
297
298
 
299
300
301
...
318
319
320
 
321
322
323
...
296
297
298
299
300
301
302
...
319
320
321
322
323
324
325
0
@@ -296,6 +296,7 @@ class midcom_core_services_dispatcher_midgard implements midcom_core_services_di
0
                 {
0
                     //echo "DEBUG: simple match route_id:{$route_id}\n";
0
                     $this->route_id = $route_id;
0
+ $_MIDCOM->context->route_id = $this->route_id;
0
                     return true;
0
                 }
0
                 // Did not match, try next route
0
@@ -318,6 +319,7 @@ class midcom_core_services_dispatcher_midgard implements midcom_core_services_di
0
 
0
             // We have a complete match, setup route_id arguments and return
0
             $this->route_id = $route_id;
0
+ $_MIDCOM->context->route_id = $this->route_id;
0
             // Map variable arguments
0
             foreach ($route_path_matches[1] as $index => $varname)
0
             {
...
20
21
22
23
24
 
25
26
27
28
29
30
31
 
32
33
34
...
20
21
22
 
 
23
24
25
26
27
28
29
 
30
31
32
33
0
@@ -20,14 +20,13 @@ interface midcom_core_services_templating
0
     
0
     /**
0
      * Include the content template based on either global or controller-specific template entry point.
0
- */
0
- public function content();
0
+ public function content();*/
0
     
0
     /**
0
      * Show the loaded contents using the template engine
0
      *
0
      * @param string $content Content to display
0
      */
0
- public function display($content);
0
+ public function display();
0
 }
0
 ?>
0
\ No newline at end of file
...
16
17
18
 
19
20
21
...
24
25
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
28
29
...
174
175
176
 
177
178
179
 
180
181
182
...
188
189
190
 
 
 
 
 
191
192
193
...
219
220
221
222
 
223
224
225
...
325
326
327
328
329
330
331
 
 
332
333
334
...
336
337
338
339
 
340
341
342
343
344
345
346
347
348
349
 
 
 
 
 
 
 
 
 
350
351
352
...
354
355
356
357
 
358
359
360
 
 
 
 
 
361
362
363
...
372
373
374
375
 
376
377
378
...
16
17
18
19
20
21
22
...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
...
206
207
208
209
210
211
 
212
213
214
215
...
221
222
223
224
225
226
227
228
229
230
231
...
257
258
259
 
260
261
262
263
...
363
364
365
 
 
 
 
366
367
368
369
370
...
372
373
374
 
375
376
 
 
 
 
 
 
 
 
 
377
378
379
380
381
382
383
384
385
386
387
388
...
390
391
392
 
393
394
395
396
397
398
399
400
401
402
403
404
...
413
414
415
 
416
417
418
419
0
@@ -16,6 +16,7 @@ class midcom_core_services_templating_midgard implements midcom_core_services_te
0
     private $dispatcher = null;
0
     private $stacks = array();
0
     private $stack_elements = array();
0
+ private $cache_directory = '';
0
 
0
     private $elements_shown = array();
0
 
0
@@ -24,6 +25,37 @@ class midcom_core_services_templating_midgard implements midcom_core_services_te
0
         $this->stacks[0] = array();
0
     }
0
     
0
+ private function get_cache_identifier()
0
+ {
0
+ return "{$_MIDGARD['host']}-{$_MIDGARD['page']}-{$_MIDGARD['style']}-" . $_MIDCOM->context->get_current_context() . "-{$_MIDCOM->context->route_id}";
0
+ }
0
+
0
+ private function get_cache_directory()
0
+ {
0
+ switch ($_MIDGARD['config']['prefix'])
0
+ {
0
+ case '/usr':
0
+ case '/usr/local':
0
+ return '/var/cache/midgard';
0
+ default:
0
+ return "{$_MIDGARD['config']['prefix']}/var/cache/midgard";
0
+ }
0
+ }
0
+
0
+ private function prepare_cache()
0
+ {
0
+ $this->cache_directory = str_replace('__MIDGARDCACHE__', $this->get_cache_directory(), $_MIDCOM->configuration->get('cache_directory'));
0
+ if (!file_exists($this->cache_directory))
0
+ {
0
+ mkdir($this->cache_directory, 0777, true);
0
+ }
0
+
0
+ if (!is_writable($this->cache_directory))
0
+ {
0
+ throw new Exception("Cache directory {$cache_directory} is not writable");
0
+ }
0
+ }
0
+
0
     public function append_directory($directory)
0
     {
0
         if (!file_exists($directory))
0
@@ -174,9 +206,10 @@ class midcom_core_services_templating_midgard implements midcom_core_services_te
0
     {
0
         if (is_array($element))
0
         {
0
+ // Element is array in the preg_replace_callback case (evaluating element includes)
0
             $element = $element[1];
0
         }
0
-
0
+
0
         $stack = $_MIDCOM->context->get_current_context();
0
         if (!isset($this->stacks[$stack]))
0
         {
0
@@ -188,6 +221,11 @@ class midcom_core_services_templating_midgard implements midcom_core_services_te
0
             $this->stack_elements[$stack] = array();
0
         }
0
         
0
+ if ($element == 'content')
0
+ {
0
+ $element = $_MIDCOM->context->content_entry_point;
0
+ }
0
+
0
         if (isset($this->stack_elements[$stack][$element]))
0
         {
0
             return $this->stack_elements[$stack][$element];
0
@@ -219,7 +257,7 @@ class midcom_core_services_templating_midgard implements midcom_core_services_te
0
                 
0
                 $this->stack_elements[$stack][$element] = $element_content;
0
                 
0
- eval('?>' . preg_replace_callback("/<\\(([a-zA-Z0-9 _-]+)\\)>/", array($this, 'get_element'), $this->stack_elements[$stack][$element]));
0
+ return preg_replace_callback("/<\\(([a-zA-Z0-9 _-]+)\\)>/", array($this, 'get_element'), $this->stack_elements[$stack][$element]);
0
             }
0
         }
0
         
0
@@ -325,10 +363,8 @@ class midcom_core_services_templating_midgard implements midcom_core_services_te
0
         $_MIDCOM->context->create();
0
         $data = $this->dynamic_call($component_name, $route_id, $arguments, false);
0
         
0
- ob_start();
0
- $this->content();
0
-
0
- $this->display(ob_get_clean());
0
+ $this->template('content_entry_point');
0
+ $this->display();
0
         
0
         $_MIDCOM->context->delete();
0
     }
0
@@ -336,17 +372,17 @@ class midcom_core_services_templating_midgard implements midcom_core_services_te
0
     /**
0
      * Include the template based on either global or controller-specific template entry point.
0
      */
0
- public function template()
0
+ public function template($element_identifier = 'template_entry_point')
0
     {
0
- $this->get_element($_MIDCOM->context->template_entry_point);
0
- }
0
-
0
- /**
0
- * Include the content template based on either global or controller-specific template entry point.
0
- */
0
- public function content()
0
- {
0
- $this->get_element($_MIDCOM->context->content_entry_point);
0
+ $this->prepare_cache();
0
+ $cache_file = $this->cache_directory . '/' . $this->get_cache_identifier() . '.php';
0
+
0
+ if (file_exists($cache_file))
0
+ {
0
+ return;
0
+ }
0
+
0
+ file_put_contents($cache_file, $this->get_element($_MIDCOM->context->$element_identifier));
0
     }
0
     
0
     /**
0
@@ -354,10 +390,15 @@ class midcom_core_services_templating_midgard implements midcom_core_services_te
0
      *
0
      * @param string $content Content to display
0
      */
0
- public function display($content)
0
+ public function display()
0
     {
0
         $data = $_MIDCOM->context->get();
0
 
0
+ $cache_file = $this->cache_directory . '/' . $this->get_cache_identifier() . '.php';
0
+ ob_start();
0
+ include($cache_file);
0
+ $content = ob_get_clean();
0
+
0
         switch ($data['template_engine'])
0
         {
0
             case 'tal':
0
@@ -372,7 +413,7 @@ class midcom_core_services_templating_midgard implements midcom_core_services_te
0
                     $_MIDCOM->timer->setMarker('post-require');
0
                 }
0
                 
0
- $tal = new PHPTAL();
0
+ $tal = new PHPTAL($this->get_cache_identifier());
0
 
0
                 $tal->show_toolbar = false;
0
                 if ( isset($_MIDCOM->toolbar)
...
95
96
97
98
 
99
100
101
...
95
96
97
 
98
99
100
101
0
@@ -95,7 +95,7 @@
0
 
0
             <div id="content">
0
                 <div class="main">
0
- <?php $_MIDCOM->templating->content(); ?>
0
+ <(content)>
0
                 </div>
0
             </div>
0
             

Comments

    No one has commented yet.