1
+ <?
2
+
3
+ /**
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+ *
18
+ * @package smarty-lint
19
+ * @link http://code.google.com/p/smarty-lint/
20
+ * @author Jon Ursenbach <[email protected] >
21
+ * @version 1.0
22
+ */
23
+
24
+ require dirname (__FILE__ ) . "/smarty/Smarty.class.php " ;
25
+
26
+ ob_start ();
27
+
28
+ $ _SERVER ["LINT_ERRORS " ] = array ();
29
+ $ _SERVER ["LINT_FILE " ] = "" ;
30
+ $ _SERVER ["LINT_RESULT " ] = "" ;
31
+
32
+ set_error_handler ("check_errors " );
33
+ register_shutdown_function ("show_errors " );
34
+
35
+ class smarty_lint extends Smarty
36
+ {
37
+ function __construct ( )
38
+ {
39
+ $ this ->caching = 0 ;
40
+ $ this ->template_dir = dirname (__FILE__ ) . "/smarty/templates " ;
41
+ $ this ->compile_dir = dirname (__FILE__ ) . "/smarty/templates_c " ;
42
+ $ this ->cache_dir = dirname (__FILE__ ) . "/smarty/cache " ;
43
+ $ this ->config_dir = dirname (__FILE__ ) . "/smarty/configs " ;
44
+ }
45
+
46
+ /**
47
+ * Attempt to compile the file we want to run a lint check on.
48
+ *
49
+ * @param string $resource_name
50
+ */
51
+ function check ( $ resource_name )
52
+ {
53
+ $ _SERVER ["LINT_FILE " ] = $ resource_name ;
54
+ parent ::fetch ($ resource_name , null , null , false );
55
+ $ this ->remove_compiled_tpl ($ resource_name );
56
+ }
57
+
58
+ /**
59
+ * Remove all compiled templates for the file we're linting. We must do
60
+ * this, because Smarty does not conform to its own $this->caching = 0
61
+ * regulation (at least per v2.6.19).
62
+ *
63
+ * @param string $resource_name
64
+ */
65
+ function remove_compiled_tpl ( $ resource_name )
66
+ {
67
+ $ res = glob ($ this ->compile_dir . "/* " . $ resource_name . "* " );
68
+ if (is_array ($ res ) && count ($ res ) > 0 )
69
+ foreach ($ res as $ data )
70
+ if (file_exists ($ data ))
71
+ unlink ($ data );
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Custom error handler so we can parse out errors triggered
77
+ * within the Smarty compiling classes.
78
+ *
79
+ * @param const $errno
80
+ * @param string $errstr
81
+ * @param string $errfile
82
+ * @param int $errline
83
+ *
84
+ * @return bool
85
+ */
86
+ function check_errors ( $ errno , $ errstr , $ errfile , $ errline )
87
+ {
88
+ switch ($ errno )
89
+ {
90
+ case E_USER_ERROR :
91
+ $ _SERVER ["LINT_ERRORS " ][] = array (
92
+ "errno " => $ errno ,
93
+ "errstr " => $ errstr ,
94
+ "errfile " => $ errfile ,
95
+ "errline " => $ errline
96
+ );
97
+ break ;
98
+ }
99
+
100
+ return true ;
101
+ }
102
+
103
+ /**
104
+ * Display any errors that occured while compiling a template.
105
+ *
106
+ */
107
+ function show_errors ( )
108
+ {
109
+ ob_end_clean ();
110
+
111
+ if (is_array ($ _SERVER ["LINT_ERRORS " ]) && count ($ _SERVER ["LINT_ERRORS " ]) > 0 )
112
+ {
113
+ foreach ($ _SERVER ["LINT_ERRORS " ] as $ err )
114
+ {
115
+ $ _SERVER ["LINT_RESULT " ] .= $ err ["errfile " ] . ": " . $ err ["errline " ] . "\n" ;
116
+ $ _SERVER ["LINT_RESULT " ] .= "\t" . $ err ["errstr " ] . "\n" ;
117
+ }
118
+ }
119
+ else
120
+ $ _SERVER ["LINT_RESULT " ] .= "No errors present in " . $ _SERVER ["LINT_FILE " ] . ". \n" ;
121
+
122
+ echo $ _SERVER ["LINT_RESULT " ];
123
+ }
124
+
125
+ ?>
0 commit comments