使用XDebug与Symfony2和Twig

时间:2015-08-18 05:50:18

标签: php symfony twig phpstorm xdebug

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

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

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

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

1 个答案:

答案 0 :(得分:4)

所以这是一种绝对的痛苦,但我设法让它运行得很甜蜜。

加速流浪

如果你正在使用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框的主机,通常默认为10.0.2.2