开发者

How can I track failed file inclusions with backtrace?

Asking for decent thoughts about this:

I'd like to implement some mechanism in PHP code that can run any external code and calls a callback function if one of the inclusions fails therein (include, require + *_once).

External code means that the code that is getting executed is not written by me nor is there control over it. It's included for testing. So having detailed info about inclusions failures deeper therein is helpful.

I'm runnning into the problem that it looks impossible to have a cal开发者_如何学运维lback when a PHP fatal error happens.

What I tried so far:

  • Registering an error handler via set_error_handler - Does not work with fatal errors.
  • Created an object instance with a __destruct() method - Is not invoked with fatal errors.
  • Registered a shutdown function - Is not called on fatal errors.

In any of these I just wanted to fetch a debug_backtrace and then work with the information given.

So question shortly is: how to track failed file inclusions from within PHP code and call a function then.

I fear the answer to the question is no from my recent tries and searches, so anything insightful is highly appreciated. Even if your answer only strengthens the "not possible" point.

Additionally it's helpful as well if it's possible to find out which file is going to be included, so creating a debug output before the inclusion (failing or not) could be done at least.

Remarks:

  • Preferable w/o extensions. However if something exists, I'm eager to know as well.
  • External code means that the code that is getting executed is not written by me nor is there control over it. It's included for testing. So having detailed info about inclusions failures deeper therein is helpful.

Related:

  • How can I get PHP to produce a backtrace upon errors?
  • set_error_handler() doens't work for FATAL error (register_shutdown_function + error_get_last)


My suggestions are untested, here are some things to try:

If the required/included php files are classes autoload could be an option

function __autoload($class)
{
    // try to load
}

If you can wrap the require/includes in a try catch block setting the error handler to use exceptions might also work: Update: doesn't work :(

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

If the code you need to test can be tested in the command line using popen and xdebug you execute the program in a separate process if pclose returns -1 you could parse the backtrace from xdebug.

    $cmd = 'php --php-ini path/php.ini file/to/run.php';
    $output = '';
    $popen  = popen($cmd, 'rb');

    while (!feof($popen)) {
        $output .= fread($popen, 4096);
    }

    if (pclose($popen) < 0) {
        // error - parse $output for xdebug backtrace
    }


You don't need it. To load classes use __autoload function. To include static files use require_once. To load dinamic files you should check them before using (file_exeists, is_readable, etc) and throw exceptions if files not found to take backtrace.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜