Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
3 lars 1
<?php
2
/* $Id: Easy_Templates.class.php 8 2007-04-24 14:17:50Z markus $ */
3
 
4
/**
5
 * Easy Templates parsing engine.
6
 *
7
 * With this class you easily could handle template files
8
 * and parse the contents in it. A detailed documentation
9
 * you will find in the comments. Or in this DocBlock.
10
 *
11
 * PHP versions 4 and 5
12
 *
13
 * LICENSE: This source file is subject to version 3.0 of the PHP license
14
 * that is available through the world-wide-web at the following URI:
15
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
16
 * the PHP License and are unable to obtain it through the web, please
17
 * send a note to license@php.net so we can mail you a copy immediately.
18
 *
19
 * @category   Web_Contents
20
 * @package    Easy_Templates
21
 * @author     Markus Niewerth <markus@weban.de>
22
 * @copyright  1997-2005 The PHP Group
23
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
24
 * @since      File available since Release 1.0.1
25
 */
26
 
27
	/*
28
		Implementation von Klassen-basierter Programmierung.
29
		Die neu Klasse Weban_Shop wird im späteren Verlauf mit
30
		den Funktionen aus dem alten Shop Modul gefüllt, Somit
31
		können alle Funktionen nach und nach ausgetauscht werden.
32
 
33
		Zur Information: Es existieren noch keine extended Klassen,
34
		alle benötigten Klassen eines Moduls werden in der Hauptklasse
35
		instanziert und können dann auch über das $Weban_Shop Object
36
		angesprochen werden.
37
	*/
38
 
39
// {{{ constants
40
define("__CONSTRUCT_ERROR__", 	"Can't load Template file: <strong>%s</strong>");
41
define("__ERROR_MESSAGE__", 	"Keine ID bergeben, %s");
42
define("__RETURN_TEMPLATES__", 	false);
43
// }}}
44
// {{{ GLOBALS
45
 
46
 // -------------------
47
 // IF any put in here!
48
 // -------------------
49
 
50
// }}}
51
// {{{ Easy_Templates
52
 
53
/**
54
 * Easy Templates parsing engine.
55
 *
56
 * With this class you easily could handle template files
57
 * and parse the contents in it. A detailed documentation
58
 * you will find in the comments. Or in this DocBlock.
59
 *
60
 * PHP versions 4 and 5
61
 *
62
 * LICENSE: This source file is subject to version 3.0 of the PHP license
63
 * that is available through the world-wide-web at the following URI:
64
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
65
 * the PHP License and are unable to obtain it through the web, please
66
 * send a note to license@php.net so we can mail you a copy immediately.
67
 *
68
 * @category   Web_Contents
69
 * @package    Easy_Templates
70
 * @author     Markus Niewerth <markus@weban.de>
71
 * @copyright  1997-2005 The PHP Group
72
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
73
 * @since      File available since Release 1.0.1
74
 */
75
 
76
class Easy_Templates {
77
 
78
	// {{{ properties
79
 
80
	/**
81
	* @var   int   $SiteID
82
	* Seiten oder Status ID
83
	*/
84
	var $SiteID;
85
 
86
	/**
87
	* @var   res   $fp
88
	* Resource fr den filestream
89
	*/
90
	var $fp           =   '';
91
 
92
	/**
93
	* @var   array   $uncompiled
94
	* @var   array   $compiled
95
	* Eingabe und Ausgabe arrays
96
	*/
97
	var $uncompiled   =   array();
98
	var $compiled     =   array();
99
 
100
	/**
101
	* @var   int   $i			Template zaehler, gibt die geladenen
102
	* @var   int   $loaded   	Zaehler array
103
	* Uebertraegt den aktuellen Zaehler als einen Schluessel
104
	*/
105
	var $i;
106
	var $loaded = array();
107
 
108
	/**
109
	* @var   int   $extension
110
	* Template extension (sucht nach diesen endungen)
111
	*/
112
	var $extension;
113
 
114
	/**
115
	* @var array $ctcscript
116
	* Beinhaltet template scripts, die
117
	* dynamisch zur Laufzeit ausgefhrt werden.
118
	*
119
	*/
120
	var $ctcscript = array();
121
 
122
	/**
123
	* @var string $parsed
124
	* Zu parsende CTC Script parts
125
	*/
126
	var $parsed = "";
127
 
128
	/**
129
	* @var array $_cache         Cached templates
130
	* @var array $_evalcache     Parsed cached templates
131
	*
132
	*/
133
	var $_cache       = array();
134
	var $_evalcache   = array();
135
 
136
	/**
137
	* @var array $_cache
138
	* Cached templates
139
	*/
140
	var $_loadtimes = array();
141
 
142
	// }}}
143
	// {{{ Easy_Templates()
144
 
145
	/**
146
	* Easy_Templates::Easy_Templates
147
	* C o n s t r u c t o r
148
	* @param   $SiteID     Aktuelle Seite oder Kontext ID
149
	* @param   $extension  Die Template extension (zb. .tpl, .html usw.)
150
	*/
151
	function Easy_Templates($SiteID=0, $extension='.html')
152
	{
153
		if (empty($SiteID)) {
154
			sprintf("Easy_Templates::Easy_Templates(".__ERROR_MESSAGE__.")", $extension);
155
		}
156
		$this->SiteID=$SiteID;
157
		$this->i=0; // templates loaded
158
		$this->extension = $extension;
159
 
160
		$_loadtimes['start']    = array();
161
		$_loadtimes['end']      = array();
162
 
163
		// Definiert eine Methode und gleichzeitig auch die Funktion
164
		if (!function_exists('pre'))  {
165
			function pre($arr)
166
			{
167
				print("<pre>\n");
168
				print_r($arr);
169
				print("</pre>\n");
170
			}
171
		}
172
		if (!function_exists('microtime_float'))  {
173
			function microtime_float()
174
			{
175
				list($usec, $sec) = explode(" ", microtime());
176
				return ((float)$usec + (float)$sec);
177
			}
178
		}
179
		// {GLOBALS}[loaded]
180
 
181
	}
182
 
183
	// }}}
184
	// {{{ parse()
185
 
186
	/**
187
	* @name parse
188
	* This Function uses a simple
189
	* routine for executing small
190
	* IF commands.
191
	*
192
	* @param String $content  [The template file]
193
	* @param Bool   $fallback [not active atm]
194
	* @param Array  $args     [variables for the template execution]
195
	*/
196
	function parse($content, $fallback=NULL, $args, $showPart=NULL, $delima = "{", $delimb = "}", $return=__RETURN_TEMPLATES__)
197
	{
198
		global $loaded, $loadtime;
199
 
200
		$this->_loadtimes['start'][$this->i] = microtime_float();
201
		$__tmplpart = $showPart;
202
 
203
		extract($args, EXTR_OVERWRITE );
204
 
205
		$this->parsed 		= NULL;
206
		$this->cache 		= NULL;
207
		$this->ctcscript 	= NULL;
208
		$this->uncompiled 	= NULL;
209
		$this->compiled 	= NULL;
210
 
211
		/**
212
		 * Open the File Stream, to get
213
		 * the template contents.
214
		 * We use @, cause we wanna have
215
		 * a clean template parser.
216
		 */
217
		$this->fp = @fopen($content. $this->extension, 'r');
218
		if (!$this->fp) {
219
			$this->fp = @fopen($tplDir.$fallback.$this->extension, 'r');
220
		}
221
		if (!$this->fp) {
222
			$show 			= "E_MESSAGE";
223
			$E_MESSAGE 		= sprintf(__CONSTRUCT_ERROR__,$content . $this->extension);
224
			$this->cache 	= "\n<!-- IF [show] 'E_MESSAGE' -->\n".$delima."E_MESSAGE".$delimb."\n<!-- ENDIF -->\n";
225
		}
226
 
227
		if ($this->fp) {
228
			array_push ($this->loaded, $this->i);
229
			/**
230
			 * Read the template contents
231
			 */
232
			$this->cache = fread($this->fp, filesize($content . $this->extension));
233
			fclose($this->fp);
234
		}
235
 
236
		/**
237
		 * Parse internal Functions
238
		 * called CTC Script.
239
		 * Its a simple IF construct.
240
		 * Example:
241
		 * <!-- IF [variable] 'value' -->
242
		 * TEMPLATE CONTENTS GOES HERE
243
		 * <!-- ENDIF -->
244
		 * NOTE:
245
		 * The parser is not tested
246
		 * with normal HTML Comments.
247
		 *
248
		 */
249
 
250
		/**
251
		 * @variable String $pcre
252
		 * The Regular Expression to get
253
		 * the template script executed.
254
		 * It cost me several hours for
255
		 * testing and executing.
256
		 * Dieser Teil steuert die If Bedingungen in Kommentaren
257
		 * Siehe Kommentar weiter oben.
258
		 */
259
		$pcre = "/<!--[\s]{1,}IF[\s]{1,}\[([a-zA-Z0-9_]{1,})\][\s]{1,}\'([a-zA-Z0-9_]{1,})\'[\s]{1,}-->(.*)<!--[\s]{1,}ENDIF[\s]{1,}-->/sU";
260
		//$case = "/<!--[\s]{1,}CASE[\s]{1,}\[([a-zA-Z0-9]{1,})\][\s]{1,}\'([a-zA-Z0-9]{1,})\'[\s]{1,}-->(.*)<!--[\s]{1,}ENDCASE[\s]{1,}-->/sU";
261
		/**
262
		 * Execute the Regular Expression to
263
		 * handle CTC Template Script.
264
		 */
265
 
266
		preg_match_all($pcre, $this->cache, $this->ctcscript);
267
 
268
		/**
269
		 * [1] => variables
270
		 * [2] => values
271
		 * [3] => html contents
272
		 */
273
 
274
		if (is_array($this->ctcscript[1])) {
275
			foreach(array_keys($this->ctcscript[1]) AS $elem) {
276
				$var = $this->ctcscript[1][$elem];
277
				if ($$var==$this->ctcscript[2][$elem]) {
278
					$this->parsed .= $this->ctcscript[3][$elem];
279
				}
280
			}
281
		}
282
 
283
 
284
		// 	preg_match_all($case, $this->cache, $this->ctcscript);
285
 
286
			/**
287
			* [1] => variables
288
			* [2] => values
289
			* [3] => html contents
290
			*/
291
 
292
		// 	if(is_array($this->ctcscript[1])) {
293
		// 		foreach(array_keys($this->ctcscript[1]) AS $elem) {
294
		// 			$var = $this->ctcscript[1][$elem];
295
		// 			if($$var==$this->ctcscript[2][$elem]) {
296
		// 				$cases[] = $this->ctcscript[3][$elem];
297
		// 			}
298
		// 		}
299
		// 	}
300
		//pre($cases);
301
 
302
 
303
 
304
		$this->setTemplatesLoaded();
305
		// durch $delima und $delimb einstellbar
306
		// Standard Template Marker: { }
307
		$this->uncompiled = preg_replace("/($delima)(.*)($delimb)/U","$\\2", $this->parsed);
308
 
309
		// Standard String verarbeitung
310
		$this->compiled = addslashes($this->uncompiled);
311
		$this->compiled = "echo \"".trim($this->compiled)."\"";
312
		// entfernt ### durch '
313
		// ist fr die Ausgabe von Arrays zust�dig
314
		$this->compiled = str_replace("###","'",$this->compiled);
315
		// Dieser Teil ist fr die Ausgabe von Arrays.
316
		// Syntax: %%.{arrayName}[0][key_x].%%
317
		$this->compiled = str_replace("%%.","\".",$this->compiled);
318
		$this->compiled = str_replace(".%%",".\"",$this->compiled);
319
		$this->_cache[] = $this->compiled;
320
 
321
		ob_start();
322
		{
323
			eval($this->compiled.";");
324
				$ret_eval           = ob_get_contents();
325
				$this->_evalcache[] = $ret_eval;
326
			ob_end_clean();
327
		}
328
 
329
		$this->_loadtimes['end'][$this->i] 	= microtime_float();
330
		$this->i++;
331
		$GLOBALS['loadtime'] 				= $this->getLoadTimes();
332
 
333
		if ($return) {
334
			return $ret_eval;
335
		} else {
336
			print ($ret_eval);
337
		}
338
	}
339
 
340
	// }}}
341
	// {{{ parseFromCache()
342
 
343
	function parseFromCache($part, $args)
344
	{
345
		// Extract
346
		extract ($args, EXTR_OVERWRITE);
347
		return  eval($this->_cache[$part].";");
348
	}
349
 
350
	// }}}
351
	// {{{ getLoadTimes()
352
 
353
	function getLoadTimes()
354
	{
355
		$returnTime = null;
356
		if (count($this->_loadtimes['start']) == 0) {
357
			return false;
358
		} else {
359
			foreach(array_keys($this->_loadtimes['start']) AS $elem) {
360
				$returnTime += $this->_loadtimes['end'][$elem] - $this->_loadtimes['start'][$elem];
361
			}
362
		}
363
		$GLOBALS['loadtime'] = round($returnTime*100,4);
364
		return round($returnTime*100,4);
365
	}
366
 
367
	// }}}
368
	// {{{ setTemplatesLoaded()
369
 
370
	function setTemplatesLoaded()
371
	{
372
		$GLOBALS['loaded'] = count($this->loaded);
373
	}
374
 
375
	// }}}
376
	// {{{ pre()
377
 
378
	function pre($array)
379
	{
380
		print("<pre>");
381
		print_r($array);
382
		print("</pre>");
383
	}
384
	// }}}
385
}
386
// }}}
387
?>