将 XDebug 与 Symfony2 和 Twig 结合使用

Using XDebug with Symfony2 and Twig

我正在使用 Symfony2Twig templates 构建一个项目。一切 运行 都很好,但是当我尝试使用 XDebug 时,一切都停止了。 页面加载需要 30 秒或更长时间,并且经常超时,即使页面非常简单也是如此。

有没有办法将 XDebugSymfony2Twig 一起使用,并以可接受的速度使其 运行?

我看到很多建议说只禁用 xdebug,但我真的不想那样做。

我的应用在 Ubuntu 14.04 Vagrant box 上 运行,我在 OSX 上使用 PhpStorm

所以这绝对是一种痛苦,但我已经设法运行甜蜜地解决了它。

加速 Vagrant

如果您使用的是 Vagrant,make sure your synced folders use NFS。例如:

config.vm.synced_folder ".", "/var/www", type: "nfs"

我看到加载 Symfony2 页面的速度提高了大约 400%。

为了提高速度,您还可以启用 opcache。它随 PHP 5.5 一起提供,可作为早期版本的扩展使用。这又使我的速度提高了 200-300%。 (总共约1000%)。

加速 XDebug

最大的速度影响来自 xdebug 的堆栈跟踪参数集合。您可以通过将 xdebug.collect_params = 0 添加到 php.ini.

来禁用它

代码覆盖率检查也有性能成本,所以如果您不需要它们,请禁用它们。设置 xdebug.coverage_enable = 0.

分析也很慢,所以只在需要时启用它。设置 xdebug.profiler_enable = 0xdebug.profiler_enable_trigger = 1 然后使用浏览器插件在需要时启用它。我用 Easiest XDebug for Firefox.

这应该是您需要的一切。下面是我完整的 xdebug 配置(针对不太出色的代码突出显示的应用程序):

; == Xdebug ==

; When executing a script, what should xdebug collect information about?
; This seems to be purely for displaying the error stack.
; Collection of parameters can slow xdebug to a crawl.
; Breakpoints in PhpStorm will still have all of this info.
xdebug.collect_includes = 0
xdebug.collect_params = 0
xdebug.collect_return = 0
xdebug.collect_vars = 0

; http://xdebug.org/docs/code_coverage
; If you want to check that your unit tests cover all lines of your code you should enable this.
xdebug.coverage_enable = 0

; A name to identify this instance of xdebug.
xdebug.idekey = "PHPSTORM"

; Maximum function nexting allowed. Symfony needs ~70 or 80. Complex twig templates require more.
xdebug.max_nesting_level = 100

; 1 = Use xdebug's implementation of var_dump.
; 2 = Also add file names and line numbers to var dumps.
xdebug.overload_var_dump = 2

; Disable the profiler by default (because it slows things down) but allow it to be set with a cookie.
; Set a cookie: XDEBUG_PROFILE / Use a browser plugin.
xdebug.profiler_enable = 0
xdebug.profiler_enable_trigger = 1
xdebug.profiler_output_dir = "/var/www/logs/profiler"
xdebug.profiler_output_name = "cachegrind.out.%t-%s"

; Disable XDebug by default but allow it to be enabled with a cookie.
; Set a cookie: XDEBUG_SESSION / Use a browser plugin.
xdebug.remote_autostart = 0
xdebug.remote_enable = 1

; When remote_connect_back=1 xdebug automatically attempts to connect to the computer making the request.
; It means you don't have to worry about setting your ip address / host name in php.ini. Useful for lazy people :-)
; DON'T DO THIS IN PRODUCTION BECAUSE ANYONE WILL BE ABLE TO MAKE XDEBUG CONNECTIONS.
; When remote_connect_back=0, xdebug will attempt to connect to xdebug.remote_host instead (not specified in this example).
xdebug.remote_connect_back = 1;

; Allow traces with a cookie.
; Set a cookie: XDEBUG_TRACE / Use a browser plugin.
xdebug.trace_enable_trigger = 1;
xdebug.trace_output_dir = "/var/www/logs/trace"
xdebug.trace_output_name = "trace.%c"

; When doing var dumps, limit the amount of information shown.
xdebug.var_display_max_children = 128
xdebug.var_display_max_data = 512
xdebug.var_display_max_depth = 3

通过 PHP CLI 使用 XDebug

使用上面的配置,当运行宁命令行脚本时,默认情况下不会启用xdebug。要使用 PHPStorm 调试 cli 脚本,运行 php 像这样:

php -dxdebug.remote_autostart=1 -dxdebug.remote_host=10.0.2.2 your_script.php

xdebug.remote_host 应设置为安装了 PHPStorm 的机器的 IP 地址。这可能是 Vagrant box 的主机,通常默认为 10.0.2.2.