I was so interested in the idea of pre-deployment checking, that I created my own script (in php) for doing this. And it already found some errors in my more then 250 files project, that I overlooked. Fortunately for my self-pride it isn't in a file written by me, but my co-worker 😉
Here's the script. It uses some functions I found in php manual for readdir:
<?php
ini_set('max_execution_time',0);
$allowed_types = array('php','php3');
$counter = 0;
?>
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Before-deployment syntax checker</title>
<style type="text/css">
.no_error {
color:black;
margin-left:10px;
}
.error {
color:red;
margin-left:30px;
}
</style>
</head>
<body>
<?php
if (!isset($_POST['path']))
{
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
Provide your local path to directory, you want to check your php files
in (without trailing slash): <br>
<input type="text" name="path">
<input type="submit" name="form_submitted" value="Submit">
</form> ';
}
else
{
$path = $_POST['path'];
echo "Trying path $path:<br>";
if (is_dir($path))
{
$result = ProcessDir(filearray($path.'/'));
echo "<br><br>Processed $counter files<br>";
if ($result) echo "<div class=\"no_error\">All files passed the test.</div>";
else echo "<div class=\"error\">There were some errors</div>";
}
else exit ("$path doesn't exist.");
}
?>
<a href="<?=$_SERVER['PHP_SELF']?>">Start from the beginning.</a>
</body>
</html>
<?php
function filearray($start="/") {
$output=array();
$getit=array();
$dir=opendir($start);
while (false !== ($found=readdir($dir))) { $getit[]=$found; }
foreach($getit as $num => $item) {
if (is_dir($start.$item) && $item!="." && $item!="..") {
$output["{$item}"]=filearray($start.$item."/");
}
if (is_file($start.$item)) { $output["$start$item"]="FILE"; }
}
closedir($dir);
return $output;
}
function file_type($file){
$path_chunks = explode("/", $file);
$thefile = $path_chunks[count($path_chunks) - 1];
$dotpos = strrpos($thefile, ".");
return strtolower(substr($thefile, $dotpos + 1));
}
function ProcessDir($file_array)
{
static $ok = true;
global $allowed_types;
global $counter;
foreach ($file_array as $name=>$item)
{
if (is_array($item)) ProcessDir($item);
else
{
if (in_array(file_type($name),$allowed_types))
{
$counter++;
$res_arr = array();
$result = exec("php -l \"$name\"",$res_arr);
if (strpos($result,"No syntax errors")!==false) echo "<div class=\"no_error\">$result</div>";
else
{
$ok = false;
echo "<div class=\"error\">";
foreach ($res_arr as $line) echo $line.'<br>';
echo "</div>";
}
}
}
}
return $ok;
}
?>