如何内联 PHP include/require,或从多个 PHP 中生成一个 PHP?
我正在尝试分析/重构/重写一个复杂的 PHP 网站。虽然我可以使用单独的文件,但我想将所有内容合并到一个文件中,这样我就可以弄清楚上下文而无需在文件之间跳转。
有没有办法使用 PHP(可执行文件)或其他工具来 “内联”包含,将结果作为一个 PHP 文件返回?
我意识到 PHP 不会预处理包含文件,而是在遇到包含文件时执行包含文件的内容。
本质上,我正在寻找结果就像 PHP 预处理在执行之前包含并需要。
编辑:基于以下 Dan Grossman 建议的工作解决方案。不幸的是,我正在使用的代码库使用 include() 做了一些巧妙的技巧,但这是一个美好的开始。
<?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);
?>
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);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写自己的预处理器,应该不难。这是一个递归函数。该函数采用 .php 文件作为参数,将文件读入字符串 (file_get_contents),搜索任何 include/require 语句并将它们替换为文件的内容 (preg_replace_callback,其中回调是带有包含文件名称的函数作为其参数),然后以字符串形式返回内容。递归调用将解析包含文件等中的包含内容,最终得到一个字符串,并将其写入新的 PHP 文件 (file_put_contents)。
非递归方法是编写一个函数,仅查找 include/require、打开包含的文件并用其内容替换 include/require。在其先前的输出上循环调用此函数,直到两次迭代的输出相同并且您知道您已经替换了所有包含内容。
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.
管理员 已包含类/函数,您应该能够读取文件。那里称为“编译器”。它将包含所有文件并将其移动到一个 .php 文件
它可能会帮助你
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