How can you inline PHP includes/requires, or generate one PHP from several?
I'm trying to analyze/refactor/rewrite a complex PHP site. While I can work with separate files, I'd like to combine everything into one file so I can figure out the context without jumping between files.
Is there any way with PHP (the executable) or other tools to "inline" the includes, returning the result as one PHP file?
I realize that PHP does not preprocess includes, instead executing contents of includes files as it encounters them.
Essentially I'm looking for the result as开发者_开发百科 if PHP preprocessed includes and requires before executing.
Edit: Working solution based on Dan Grossman's suggestion below. The codebase I'm working from unfortunately does some clever tricks with include(), but this is a wonderful start.
<?php
function combinePhpFile($input) {
$myFile = file_get_contents($input[2]);
$myFile = preg_replace_callback('~\\b(include|require) "([^"]*)";~', 'combinePhpFile', $myFile);
return $myFile;
}
// preg_replace_callback wants an array? It GETS an array!
$result = combinePhpFile(array("dummy", "dummy", "index.php"));
file_put_contents("result.txt", $result);
?>
Write your own preprocessor, it shouldn't be hard. It's one recursive function. That function takes a .php file as its argument, reads the file into a string (file_get_contents), searches for any include/require statements and replaces them with the contents of the file (preg_replace_callback where the callback is this function with the included file's name as its argument), then returns the content as a string. The recursive calls will resolve includes within included files and such, and you end up with one string which you write out to a new PHP file (file_put_contents).
A non-recursive method is to write a function that just looks for include/require, opens the included file and replaces the include/require with its contents. Call this function in a loop on its previous output until the output is the same on two iterations and you know you've replaced all the includes.
adminer has got class/functions included and you should be able to read through files. It's called "compiler" there. It takes all files with includes and moves it to one single .php file
It might help you
精彩评论