在 Symfony2 和 Twig 中使用 XDebug

jxm*_*ett 0 php xdebug symfony twig phpstorm

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

有没有办法使用的方式XDebugSymfony2Twig,并将它以可接受的速度运行?

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

我的应用程序运行在一个Ubuntu 14.04 Vagrant box和我使用PhpStormOSX

jxm*_*ett 5

所以这是一个绝对的痛苦,但我已经设法让它顺利运行。

加速流浪

如果您使用 Vagrant,请确保您的同步文件夹使用 NFS。例如:

config.vm.synced_folder ".", "/var/www", type: "nfs"
Run Code Online (Sandbox Code Playgroud)

我看到加载 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然后在需要时使用浏览器插件启用它。我使用最简单的 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
Run Code Online (Sandbox Code Playgroud)

将 XDebug 与 PHP CLI 结合使用

使用上面的配置,在运行命令行脚本时默认情况下不会启用 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.