PHP:确保遗留脚本 运行 没有错误,替换为 `php_check_syntax`
PHP: Make sure legacy scripts run without errors, replacement for `php_check_syntax`
我有很多没有单元测试的遗留代码。在大多数情况下,它们是通过直接执行每个单独的 .php 文件自动加载 类、运行 的独立脚本。
我想要一些方法来自动执行 运行 这些脚本,检查它们是否 运行 没有错误地完成。这不仅会检测到错误的新代码,而且会自动找到哪些脚本是 运行 错误(无需尝试解析错误日志)。
我看到了 php_check_syntax
但它已被弃用。它似乎比 php -l
更可取,因为它不只是 lint - 它执行脚本,并且似乎将 运行 时间错误捕获到一个变量,然后可以读取该变量。这似乎正是我想做的。
是否有 php_check_syntax
的替代品可以帮助我完成此任务?
也许创建一个使用 try
和 catch
(和 finally
)块的包装器会起作用。
我接受了拉斐尔的回答。这是解决这个问题的正确方法。我在这里发布了从它创建的完整脚本。
它是来自命令行的 运行:
php scripttest.php "http://example.com/?param1=test"
并在出现任何错误时以代码 9 退出。它将使用 php -l
命令获取解析错误,并使用 Try Catch 块获取异常。它不会检测脚本是否提前退出。这必须通过检查空输出来检测。
<?php
$url = $argv[1];
$parsed_url = parse_url($url);
if(!$url || $parsed_url == false){
echo "Provided url is empty or malformed.";
exit(9);
}
$docroot = "/Library/WebServer/Webs";
if ($parsed_url['scheme'] == "https") $docroot .= "/example.com-https";
elseif($parsed_url['scheme'] == "http") $docroot .= "/example.com";
else{
echo "Couldn't detect protocol.";
exit(9);
}
$filepath = $docroot.$parsed_url['path'];
if(is_dir($filepath)) $filepath .= 'index.php';
if(!file_exists($filepath)){
echo "Filepath doesn't exist.";
exit(9);
}
/**
* lint
*/
$success_str = "No syntax errors detected";
$output = array();
exec("php -l $filepath", $output);
if(!$output){
echo "linting exec produced no output.";
exit(9);
}
$success = (substr($output[0], 0, strlen($success_str)) === $success_str);
if(!$success){
echo join("\n", $output);
exit(9);
}
/**
* run-time check
*/
$_REQUEST = parse_str ($parsed_url['query']);
try{
$finished = false;
$has_exception = false;
$exception = '';
ob_start(); // buffer all output
require $filepath; // execute script
ob_end_clean(); // wipe buffer
$finished = true;
}catch(Exception $e){
$has_exception = true;
$exception = $e->getMessage();
}
if($finished && !$has_exception){
echo "SUCCESS";
exit(0);
}else{
echo "Failed with exception $exception";
exit(9);
}
我有很多没有单元测试的遗留代码。在大多数情况下,它们是通过直接执行每个单独的 .php 文件自动加载 类、运行 的独立脚本。
我想要一些方法来自动执行 运行 这些脚本,检查它们是否 运行 没有错误地完成。这不仅会检测到错误的新代码,而且会自动找到哪些脚本是 运行 错误(无需尝试解析错误日志)。
我看到了 php_check_syntax
但它已被弃用。它似乎比 php -l
更可取,因为它不只是 lint - 它执行脚本,并且似乎将 运行 时间错误捕获到一个变量,然后可以读取该变量。这似乎正是我想做的。
是否有 php_check_syntax
的替代品可以帮助我完成此任务?
也许创建一个使用 try
和 catch
(和 finally
)块的包装器会起作用。
我接受了拉斐尔的回答。这是解决这个问题的正确方法。我在这里发布了从它创建的完整脚本。
它是来自命令行的 运行:
php scripttest.php "http://example.com/?param1=test"
并在出现任何错误时以代码 9 退出。它将使用 php -l
命令获取解析错误,并使用 Try Catch 块获取异常。它不会检测脚本是否提前退出。这必须通过检查空输出来检测。
<?php
$url = $argv[1];
$parsed_url = parse_url($url);
if(!$url || $parsed_url == false){
echo "Provided url is empty or malformed.";
exit(9);
}
$docroot = "/Library/WebServer/Webs";
if ($parsed_url['scheme'] == "https") $docroot .= "/example.com-https";
elseif($parsed_url['scheme'] == "http") $docroot .= "/example.com";
else{
echo "Couldn't detect protocol.";
exit(9);
}
$filepath = $docroot.$parsed_url['path'];
if(is_dir($filepath)) $filepath .= 'index.php';
if(!file_exists($filepath)){
echo "Filepath doesn't exist.";
exit(9);
}
/**
* lint
*/
$success_str = "No syntax errors detected";
$output = array();
exec("php -l $filepath", $output);
if(!$output){
echo "linting exec produced no output.";
exit(9);
}
$success = (substr($output[0], 0, strlen($success_str)) === $success_str);
if(!$success){
echo join("\n", $output);
exit(9);
}
/**
* run-time check
*/
$_REQUEST = parse_str ($parsed_url['query']);
try{
$finished = false;
$has_exception = false;
$exception = '';
ob_start(); // buffer all output
require $filepath; // execute script
ob_end_clean(); // wipe buffer
$finished = true;
}catch(Exception $e){
$has_exception = true;
$exception = $e->getMessage();
}
if($finished && !$has_exception){
echo "SUCCESS";
exit(0);
}else{
echo "Failed with exception $exception";
exit(9);
}