php 运行 Composer 时禁用 xdebug

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31083195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:13:26  来源:igfitidea点击:

Disabling xdebug when running composer

phpcomposer-phpxdebug

提问by greg0ire

When running composer diagnose, I get the following error :

运行时composer diagnose,我收到以下错误:

The xdebug extension is loaded, this can slow down Composer a little. Disabling it when using Composer is recommended.

加载了 xdebug 扩展,这会稍微降低 Composer 的速度。建议在使用 Composer 时禁用它。

How can I disable xdebug only when I'm running Composer?

如何仅在运行 Composer 时禁用 xdebug?

采纳答案by Joyce Babu

Update: The issue has been fixed in Composer 1.3. Update composer to the latest version by executing composer self-update, instead of trying the following workaround.

更新:该问题已在Composer 1.3 中修复。通过执行将 composer 更新到最新版本composer self-update,而不是尝试以下解决方法。



Here is my modification of @ezzatron's code. I have updated the script to detect ini files from phpinfo output.

这是我对@ezzatron 代码的修改。我已更新脚本以从 phpinfo 输出中检测 ini 文件。

#!/bin/sh

php_no_xdebug () {
    temporaryPath="$(mktemp -t php.XXXX).ini"

    # Using awk to ensure that files ending without newlines do not lead to configuration error
    php -i | grep "\.ini" | grep -o -e '\(/[a-z0-9._-]\+\)\+\.ini' | grep -v xdebug | xargs awk 'FNR==1{print ""}1' | grep -v xdebug > "$temporaryPath"

    php -n -c "$temporaryPath" "$@"
    rm -f "$temporaryPath"
}

php_no_xdebug /usr/local/bin/composer.phar $@
# On MacOS with composer installed using brew, comment previous line
# Install jq by executing `brew install jq` and uncomment following line.
# php_no_xdebug /usr/local/Cellar/composer/`brew info --json=v1 composer | jq -r '.[0].installed[0].version'`/libexec/composer.phar $@

回答by ruleant

This command will disable the PHP5 Xdebug module for CLI (and thus composer) :

此命令将禁用 CLI(以及 composer)的 PHP5 Xdebug 模块:

sudo php5dismod -s cli xdebug

It removes the xdebug.inisymlink from /etc/php5/cli/conf.d/

它删除了xdebug.ini符号链接/etc/php5/cli/conf.d/

This was suggested on http://blog.lorenzbausch.de/2015/02/10/php-disable-xdebug-for-cli/

这是在http://blog.lorenzbausch.de/2015/02/10/php-disable-xdebug-for-cli/ 上提出的

Note that for Ubuntu 16.04 you probably need to run it like this:

请注意,对于 Ubuntu 16.04,您可能需要像这样运行它:

sudo phpdismod -s cli xdebug

回答by Gui-Don

I don't think there is an option to configure PHP so it can load different configurations according to the targeted script. At least, not without duplicating .ini files...

我认为没有配置 PHP 的选项,因此它可以根据目标脚本加载不同的配置。至少,不是没有复制 .ini 文件......

However, you can add thoses options when running composer with php:

但是,您可以在使用 php 运行 composer 时添加这些选项:

php -n -d extension=needed_ext.so composer.phar

-nwill tell PHP to ignore any php.ini. This will prevent xdebug from loading for this very command.

-n会告诉 PHP 忽略任何 php.ini。这将阻止 xdebug 为这个命令加载。

-doptions permits you to add any option you want (for exemple, activate needed_ext.so). You can use multiple -doptions. Of course, this is optional, you might not need it.

-doptions 允许您添加任何您想要的选项(例如,激活 required_ext.so)。您可以使用多个-d选项。当然,这是可选的,您可能不需要它。

Then you can create an alias, to make it sugary again.

然后你可以创建一个别名,让它再次变得甜蜜。

A typical solution (because composer needs json):

一个典型的解决方案(因为composer需要json):

php -n -d extension=json.so composer.phar

greg0ire > my solution, based on that:

greg0ire > 我的解决方案,基于此:

#!/bin/bash
options=$(ls -1 /usr/lib64/php/modules| \

    grep --invert-match xdebug| \

    # remove problematic extensions
    egrep --invert-match 'mysql|wddx|pgsql'| \

    sed --expression 's/\(.*\)/ --define extension=/'| \

    # join everything together back in one big line
    tr --delete '\n'
)

# build the final command line
php --no-php-ini $options ~/bin/composer $*

alias composer=/path/to/bash/script.sh

It looks ugly (I tried and failed to do that with xargs), but works… I had to disable some extensions though, otherwise I get the following warnings:

它看起来很丑(我尝试过使用 xargs 这样做但失败了),但是有效......不过我不得不禁用一些扩展,否则我会收到以下警告:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysqli.so' - /usr/lib64/php/modules/mysqli.so: undefined symbol: mysqlnd_connect in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pdo_mysql.so' - /usr/lib64/php/modules/pdo_mysql.so: undefined symbol: pdo_parse_params in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pdo_pgsql.so' - /usr/lib64/php/modules/pdo_pgsql.so: undefined symbol: pdo_parse_params in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/wddx.so' - /usr/lib64/php/modules/wddx.so: undefined symbol: php_XML_SetUserData in Unknown on line 0

回答by Adriano Rosa

By creating an alias you'll suppress that composerxdebugerror message.

通过创建别名,您将抑制该composerxdebug错误消息。

Just add this line to your ~/.bash_aliaseswithin your system and it should work flawlessly.

只需将此行添加到您~/.bash_aliases的系统中,它就可以完美运行。

alias composer="php -n /usr/local/bin/composer"

Reload the shell to make the new alias composeravailable.

重新加载 shell 以使新别名composer可用。

source ~/.bash_profile

USAGE:

用法:

$ composer --version

NOTE:
You don't necessarily need to use any other parameter.
Depending on your system you might have a .bashrcinstead of .bash_profile.

注意:
您不一定需要使用任何其他参数。
根据您的系统,您可能有一个.bashrc而不是.bash_profile.

UPDATE:

更新:

As @AlexanderKachkaev mention in the comments it's worth nothing to add the memory_limit as follows to avoid crashing im some situations:

正如@AlexanderKachkaev 在评论中提到的,添加 memory_limit 如下以避免在某些情况下崩溃是毫无价值的:

alias composer="php -d memory_limit=-1 -n /usr/local/bin/composer"

回答by ezzatron

I came up with an answer that works pretty well for OSX, and could probably be adapted for any PHP version that loads its extensions using individual .ini files in the "additional ini dir":

我想出了一个适用于 OSX 的答案,并且可能适用于使用“附加 ini 目录”中的单个 .ini 文件加载其扩展名的任何 PHP 版本:

#!/bin/sh

function php-no-xdebug {
    local temporaryPath="$(mktemp -t php-no-debug)"

    find /opt/local/etc//php.ini /opt/local/var/db//*.ini ! -name xdebug.ini | xargs cat > "$temporaryPath"
    php -n -c "$temporaryPath" "${@:2}"
    rm -f "$temporaryPath"
}

alias composer="php-no-xdebug php56 ~/bin/composer"

回答by Joost

I usually create a shell script per project, since every project has another PHP version. It's in a /bin/directory next to composer.pharand composer.jsonand I run it as ./bin/composerin my project directory.

我通常为每个项目创建一个 shell 脚本,因为每个项目都有另一个 PHP 版本。它位于and/bin/旁边的目录中,我在我的项目目录中运行它。composer.pharcomposer.json./bin/composer

It looks like this (for php56)

它看起来像这样(对于 php56)

#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

COMPOSER_DISABLE_XDEBUG_WARN=1 /opt/local/bin/php56 \
    -d xdebug.remote_enable=0 -d xdebug.profiler_enable=0 \
    -d xdebug.default_enable=0 $DIR/../composer.phar "$@"

The -doptions effectively disable xdebug. The COMPOSER_DISABLE_XDEBUG_WARN=1part disables the warning composer issues.

这些-d选项有效地禁用了 xdebug。该COMPOSER_DISABLE_XDEBUG_WARN=1部分禁用警告作曲家问题。

Disabling the xdebug extension is preferred (see composer troubleshooting), but I personally like the simpler script.

禁用 xdebug 扩展是首选(请参阅composerTroubleshooting ),但我个人喜欢更简单的脚本。

Some timings on my machine: 2 Run with xdebug and ini-enabled: 1m33

我的机器上的一些时间:2 使用 xdebug 和启用 ini 运行:1m33

Run with xdebug but ini-disabled: 0m19

使用 xdebug 运行但 ini 禁用:0m19

Run without xdebug: 0m10

在没有 xdebug 的情况下运行:0m10

回答by scipilot

If you use PHPStorm, the latest release (2016.2) comes with a feature to enable XDebug for CLI scripts on-demand, which means you can simply turn off XDebug globally on your development machine. The IDE will enable it on the fly when it is needed by code inside your projects.

如果您使用 PHPStorm,最新版本 (2016.2) 附带了一项功能,可以按需为 CLI 脚本启用 XDebug,这意味着您只需在开发机器上全局关闭 XDebug。IDE 将在项目中的代码需要时即时启用它。

https://blog.jetbrains.com/phpstorm/2016/06/xdebug-on-demand-for-cli-php-scripts-in-phpstorm-2016-2-eap/

https://blog.jetbrains.com/phpstorm/2016/06/xdebug-on-demand-for-cli-php-scripts-in-phpstorm-2016-2-eap/

PhpStorm 2016.2 introduces Xdebug On Demand mode where you can disable Xdebug for your global PHP install, and PhpStorm will only enable it when it needs to — when you're debugging your scripts, or when you need code coverage reports.

PhpStorm 2016.2 引入了 Xdebug On Demand 模式,您可以在该模式下为全局 PHP 安装禁用 Xdebug,而 PhpStorm 只会在需要时启用它——当您调试脚本时,或者当您需要代码覆盖率报告时。

You need to edit your PHP Interpreters preferences to include the path to XDebug, as described in the linked article.

您需要编辑 PHP 解释器首选项以包含 XDebug 的路径,如链接文章中所述。

To me this seems like the perfect solution, as I only usually want XDebug while I'm in the IDE.

对我来说,这似乎是完美的解决方案,因为我通常只需要在 IDE 中使用 XDebug。

However XDebug does have other potential uses when you are "offline" e.g. extended stack dumps in error logs, which you would lose by turning it off globally. Of course you shouldn't have XDebug enabled on production, so this would be limited to use cases like beta-testing or automated-testing CLI scripts in development.

但是,当您处于“离线”状态时,XDebug 确实有其他潜在用途,例如错误日志中的扩展堆栈转储,如果全局关闭它,您将丢失这些信息。当然,您不应该在生产中启用 XDebug,因此这将仅限于开发中的 Beta 测试或自动测试 CLI 脚本等用例。

回答by KHobbits

Rather than muddle with temporarily enabling or disabling the PHP module, when you might have concurrent processes using PHP (for example as part of a CI pipeline), you can tell PHP to point at a different module loading directory.

当您可能有使用 PHP 的并发进程时(例如作为 CI 管道的一部分),您可以告诉 PHP 指向不同的模块加载目录,而不是暂时启用或禁用 PHP 模块。

While this is similar to some of the solutions mentioned above, this solves a few edge cases, which is very useful when being used by Jenkins or other CI runner which runs tests on the same machine concurrently.

虽然这类似于上面提到的一些解决方案,但它解决了一些边缘情况,这在被 Jenkins 或其他在同一台机器上同时运行测试的 CI 运行器使用时非常有用。

The easiest way to do this is to use the environment variable PHP_INI_SCAN_DIR

最简单的方法是使用环境变量 PHP_INI_SCAN_DIR

Using this in a script or build task is easy:

在脚本或构建任务中使用它很容易:

export PHP_INI_SCAN_DIR=/etc/php.d.noxdebug php composer install

export PHP_INI_SCAN_DIR=/etc/php.d.noxdebug php composer install

Of course you would want to prepare /etc/php.d.noxdebug first, doing something like:

当然,您首先要准备 /etc/php.d.noxdebug,执行以下操作:

mkdir /etc/php.d.noxdebug cp /etc/php.d/* /etc/php.d.noxdebug rm /etc/php.d.noxdebug/xdebug.ini

mkdir /etc/php.d.noxdebug cp /etc/php.d/* /etc/php.d.noxdebug rm /etc/php.d.noxdebug/xdebug.ini

This means you have an environment similar to the old php environment, with only one module missing. Meaning you don't need to worry about needing to load the phar/json modules as you would with the php -n solution.

这意味着您的环境类似于旧的 php 环境,仅缺少一个模块。这意味着您无需像使用 php -n 解决方案那样担心需要加载 phar/json 模块。

回答by Thomas Clowes

As noted in Joyce's answer, this issue no longer exists in the latest version of Composer.

乔伊斯的回答所述,最新版本的 Composer 中不再存在此问题。

The Composer documentation has been updated to note this. It details how you can enable xdebug with Composer (if required).

Composer 文档已更新以说明这一点。它详细说明了如何使用 Composer 启用 xdebug(如果需要)。

You can update your version of Composer by utilising self-update.

您可以使用self-update更新您的 Composer 版本。

On my Mac I had to do: sudo php /opt/local/bin/composer self-update

在我的 Mac 上,我必须这样做: sudo php /opt/local/bin/composer self-update

Further details about this in the context of a Homebrew PHP install can be found in this issue.

在 Homebrew PHP 安装的上下文中可以找到有关此问题的更多详细信息。

回答by mindplay.dk

I came up with a solution for the Windows-based Composer installer - it should work for any Composer installation, it just basically makes a copy of the loaded INI file and comments out the xdebug zend extension, then loads that configuration file when it runs composer.

我想出了一个基于 Windows 的 Composer 安装程序的解决方案 - 它应该适用于任何 Composer 安装,它基本上只是复制加载的 INI 文件并注释掉 xdebug zend 扩展,然后在运行 composer 时加载该配置文件.

I've opened an issue to see if they'd like to integrate this change:

我已经打开了一个问题,看看他们是否想整合这个变化:

https://github.com/composer/windows-setup/issues/58

https://github.com/composer/windows-setup/issues/58

You can find my instructions and code there.

你可以在那里找到我的说明和代码。