查找 PHP(和/或 SMARTY)中定义变量的位置?

发布于 2024-09-04 07:37:45 字数 362 浏览 5 评论 0原文

我目前正在做一个非常大的项目,为了尽快完成它面临很大的压力,而且我遇到了一个严重的问题。以一种非常奇怪的方式编写最后定义的变量的程序员 - 配置变量并不都在同一个文件中,它们分布在超过 500 个文件和 10 万多行代码的整个项目中,我有弄清楚某个变量在哪里,这样我就可以解决问题了。
有没有办法追踪这个变量?我相信他正在使用 SMARTY (由于类似的问题,我无法忍受),并且该变量是模板变量。我相当确定我正在寻找的变量最初被定义为 PHP 变量,然后该变量被传递到 SMARTY 中,所以我想追踪 PHP 变量,但是如果这是不可能的 - 我该如何追踪他在哪里为 SMARTY 定义了变量?

PS 我在 Vista 中,并且没有 ssh 访问服务器的权限,所以“grep”是不可能的。

I'm currently working on a very large project, and am under a lot of pressure to finish it soon, and I'm having a serious problem. The programmer who wrote this last defined variables in a very odd way - the config variables aren't all in the same file, they're spread out across the entire project of over 500 files and 100k+ lines of code, and I'm having a hell of a time figuring out where a certain variable is, so I can fix an issue.
Is there a way to track this variable down? I believe he's using SMARTY (Which I can not stand, due to issues like this), and the variable is a template variable. I'm fairly sure that the variable I'm looking for was initially defined as a PHP variable, then that variable is passed into SMARTY, so I'd like to track down the PHP one, however if that's impossible - how can I track down where he defined the variable for SMARTY?

P.S. I'm in Vista, and don't have ssh access to the server, so 'grep' is out of the question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空城仅有旧梦在 2024-09-11 07:37:45

暴力方式,因为有时聪明的变量不是直接分配的,但它们的名称可以存储在变量中,由许多字符串连接或某些函数的结果,这使得无法通过简单的搜索/greping在文件中找到。

首先,编写自己的函数来打印可读的回溯,即:

function print_backtrace()
{
    $backtrace = debug_backtrace(FALSE);
    foreach($backtrace as $trace)
        echo "{$trace['file']} :: {$trace['line']}<br>";
}

打开主 smarty 文件(默认为 Smarty.class.php),在第 580 行左右有一个名为 assign 的函数。修改它以监视所需的变量名称:

function assign($tpl_var, $value = null)
{
    if($tpl_var == 'FOOBAR') /* Searching for FOOBAR */
    {
        print_backtrace();
        exit;
    }

第二个函数可能需要进行相同的修改 - assign_by_ref。现在,运行脚本后,您应该得到如下输出:

D:\www\test_proj\libs\smarty\Smarty.class.php :: 584
D:\www\test_proj\classes.php :: 11
D:\www\test_proj\classes.php :: 6
D:\www\test_proj\functions.php :: 7
D:\www\test_proj\index.php :: 100

第二行指向首次分配变量的位置。

Brute force way, because sometimes smarty variables are not directly assigned, but their names can be stored in variables, concatenated from many strings or be result of some functions, that makes it impossible to find in files by simply searching / greping.

Firstly, write your own function to print readable backtrace, ie:

function print_backtrace()
{
    $backtrace = debug_backtrace(FALSE);
    foreach($backtrace as $trace)
        echo "{$trace['file']} :: {$trace['line']}<br>";
}

Open main smarty file (Smarty.class.php by default) and around line 580 there is function called assign. Modify it to watch for desired variable name:

function assign($tpl_var, $value = null)
{
    if($tpl_var == 'FOOBAR') /* Searching for FOOBAR */
    {
        print_backtrace();
        exit;
    }

The same modification may be required for second function - assign_by_ref. Now after running script you should have output like that:

D:\www\test_proj\libs\smarty\Smarty.class.php :: 584
D:\www\test_proj\classes.php :: 11
D:\www\test_proj\classes.php :: 6
D:\www\test_proj\functions.php :: 7
D:\www\test_proj\index.php :: 100

Second line points to the place where variable was first assigned.

十雾 2024-09-11 07:37:45

这类事情是我在所有 Windows 计算机上安装 Cygwin 的第一大原因。

grep myvariablename `find project_dir -name "*.php"`

我无法想象没有有效的 grep 的编程。

This sort of thing is the #1 reason I install Cygwin on all my windows machines.

grep myvariablename `find project_dir -name "*.php"`

I can't imagine programming without a working grep.

dawn曙光 2024-09-11 07:37:45

还有一个有趣的进一步选择,虽然很难看,但如果你真的迷路了,它会很有帮助。

如果您想知道 THE_NAME 的定义位置,请在您确定首先运行的位置写下这样的行:

error_reporting(E_ALL);
define('THE_NAME', 'Chuck Norris');

如果稍后 PHP 将运行您要查找的定义,它将写出如下通知这样:

Notice: Constant THE_NAME already defined 
in /home/there/can-rip-a-page-out-of-facebook.com/SomeConfiguration.php on line 89 

那么您就知道您要查找的定义位于文件 SomeConfiguration.php 的第 89 行中。

要使其正常工作,您必须考虑

  • 框架中是否存在 HTTP 转发 则您设置的代码
  • 如果有其他命令设置 PHP 错误报告模式,

因此有时添加一些 exit('here') 以避免模糊输出会有所帮助。也许您必须缩小范围,或者必须提前设置 error_reporting,但您会找到它。

There is an interesting further option, ugly like hell but helpful if you are really lost.

If you would like to know where THE_NAME was defined, write lines like these on a place you are sure is run first:

error_reporting(E_ALL);
define('THE_NAME', 'Chuck Norris');

If later PHP will run the definition you are looking for, it will write a notice like this:

Notice: Constant THE_NAME already defined 
in /home/there/can-rip-a-page-out-of-facebook.com/SomeConfiguration.php on line 89 

Then you know that the definition you are looking for is in the file SomeConfiguration.php on line 89.

To have this working, you must consider

  • if there are HTTP forwards in the framework on the way to the code you set in
  • if there are further commands setting the PHP error reporting mode

So sometimes it helps to add some exit('here') in order not to blur the output. Maybe you have to narrow down a bit or you have to set error_reporting earlier, but you'll find it.

物价感观 2024-09-11 07:37:45

这不是一个完美的解决方案,但我发现 agent ransack 很有用用于搜索大型目录和文件。可能会帮助您缩小范围。搜索结果将允许您阅读在结果窗格中找到匹配项的确切行。

It's not a perfect solution, but I find agent ransack useful for searching large directories and files. Might help you narrow things down. The search results will allow you to read the exact line it finds a match on in the result pane.

对你而言 2024-09-11 07:37:45

如果您使用 netbeans 编辑器,只需“右键单击”-> “转到定义”

ctrl + 单击 变量。

如果编辑器无法弄清楚,您可以回退到“在文件中查找”选项。

If you use the netbeans editor just "right click" -> "go to Definition"

Or ctrl + click on the variable.

If the editor can't figure it out, you could fallback to the "Find in files" option.

宛菡 2024-09-11 07:37:45

只需使用可用的 PHP IDE 之一(如果您真的很绝望,也可以使用像 Notepad++ 这样的简单文本编辑器)并在所有源文件中搜索变量的名称(大多数 PHP IDE 还支持查找函数/变量的定义位置并允许您可以跳转到相关的代码段)。虽然你不知道哪一段代码调用了模板,这看起来很奇怪(无论是 Smarty 还是其他任何东西并不重要)。您应该能够从 URI 开始深入查看代码(使用任何支持调试的 IDE),因为这样您就一定会看到所述变量的定义位置。

Just use one of the available PHP IDEs (or a simple text editor like Notepad++ if you're really desperate) and search for the name of the variable in all source files (most PHP IDEs also support finding where functions/vars were defined and allow you to jump to the relevant piece of code). Though it seems weird that you don't know what piece of code calls the template (whether it's Smarty or anything else doesn't really matter). You should be able to drill down in the code starting from the URI (using any IDE which supports debugging), because that way you're bound to see where said variable is defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文