Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit ccbafa6

Browse files
committed
Tagging the original release, 1.0.
0 parents  commit ccbafa6

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

class.smarty-lint.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
?>

smarty-lint

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/php -q
2+
<?
3+
4+
/**
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
*
19+
* @package smarty-lint
20+
* @link http://code.google.com/p/smarty-lint/
21+
* @author Jon Ursenbach <[email protected]>
22+
* @version 1.0
23+
*/
24+
25+
if (!isset($_SERVER["argv"]))
26+
errmsg("Must be run from the command line.");
27+
else if (!is_array($_SERVER["argv"]) || count($_SERVER["argv"]) <= 1)
28+
errmsg("usage: smarty-lint <template_file>");
29+
30+
foreach ($_SERVER["argv"] as $data)
31+
if (strpos($data, "-h") !== false || strpos($data, "--help") !== false)
32+
help();
33+
34+
require_once dirname(__FILE__) . "/class.smarty-lint.php";
35+
$lint = new smarty_lint;
36+
for ($x=1; $x<=(count($_SERVER["argv"]) - 1); $x++)
37+
$lint->check($_SERVER["argv"][$x]);
38+
39+
40+
/**
41+
* Display an error message and kill the page.
42+
*
43+
* @param string $error_msg
44+
*/
45+
function errmsg ( $error_msg )
46+
{
47+
echo "smarty-lint :: error\n";
48+
echo "\t" .$error_msg . "\n\n";
49+
exit;
50+
}
51+
52+
/**
53+
* Display the help page and kill the page.
54+
*
55+
*/
56+
function help ( )
57+
{
58+
$help = <<<HELP
59+
NAME
60+
smarty-lint - A lint implementation for the popular templating engine, Smarty.
61+
62+
SYNOPSIS
63+
smarty-lint [options] <template_file>
64+
65+
DESCRIPTION
66+
Loading the template file, you will load it however you would normally load it
67+
within your programs controllers. If you reference your template file as
68+
"tplsubfolder/tplname.tpl", or "tplname.template", pass those strings into
69+
smarty-lint.
70+
71+
OPTONS
72+
-h
73+
--help\t Help
74+
75+
AUTHOR
76+
Jon Ursenbach <[email protected]>
77+
78+
VERSION INFORMATION
79+
This help blurb describes smarty-lint, version 1.0.
80+
81+
COPYRIGHT
82+
smarty-lint is protected under the GPL v2. For information on the GPL v2 license,
83+
go to: http://www.fsf.org/licensing/licenses/info/GPLv2.html
84+
85+
HELP;
86+
87+
echo $help . "\n\n";
88+
exit;
89+
}
90+
91+
?>

0 commit comments

Comments
 (0)