0

I'm developing a plugin system, which of course needs to include the plugin files. So far everything works very well - exceptions get catched etc.

But my very last wish would be to even be safe from corrupt plugin files containing syntax errors. Is there a way to catch Fatal Errors in included files?

Robert
  • 431
  • 7
  • 19

1 Answers1

4

There is runkit_lint() that requires PHP runkit, or simply run php with the -l option for Lint:

if(strpos(exec('/path/to/php -l filename.php'), 'No syntax errors detected') !== false) {
    // file parses :-)
} else {
    // parse errors :-(
}

Or similar logic. Note that this only checks for parse errors not fatal errors (require fails, etc.)

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • So basically I need to precheck every file before actually including it? Is runkit eventually cheaper in terms of performance? – Robert Mar 23 '15 at 10:43
  • I haven't bench-marked, but you could lint the files once and save the "safe" ones in the db or file and check that on subsequent calls and only check new files. – AbraCadaver Mar 23 '15 at 13:57