WordPress插件+作曲家? [英] WordPress plugin + Composer?

查看:86
本文介绍了WordPress插件+作曲家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作使用一些第三方库的WordPress插件。将Composer用于WordPress插件是一种常见的做法吗?

I'm making WordPress plugin that is using a few third party libraries. Is it common practice to use Composer for WordPress plugin?

如果可以使用它,那么我认为我应该随插件一起提供所有Composer文件,因为我不想让人们手动运行 composer install

If it's okay to use it, then I assume that I should provide all Composer files along with my plugin, because I don't want to make people manually run composer install.

另一个问题是,使用Composer是否安全?自动加载?我将其配置为自动加载自己的类,并且当然也自动加载了库。方便。

Another question is, is it safe to use Composer's autoloading? I configured it to autoload my own classes and the libraries are of course autoloaded as well. That's convenient.

使用带有WordPress插件的Composer会产生开销吗?还是还有其他问题?

Is using Composer with WordPress plugin an overhead? Or does it have any additional issues?

推荐答案

这是一个老问题,但是3年以来没有任何变化。使用Composer要求WordPress插件/主题中的依赖项通常不是一个好主意。 PHP不允许使用相同的FQN加载多个类。因此,如果两个不同的插件将独立安装相同的库,则会从库的随机安装中加载类,这可能会导致真正奇怪的错误(特别是如果这些是同一库的不同版本)。为避免此类问题,每个项目应该只有一个 composer.json 实例,因此在这种情况下,Composer应该在WordPress级别运行。

This is old question, but nothing has changed since 3 years. Using Composer for requiring dependencies in WordPress plugin/themes is usually a bad idea. PHP does not allow to load more than one class with the same FQN. So if two different plugins will install the same library independently, classes from random installation of library will be loaded which may result really weird bugs (especially if these are different versions of the same library). To avoid such problems you should have only one composer.json instance per project, so in this case Composer should be run at WordPress level.

通常,如果您多次安装相同的软件包,则可能会遇到一些麻烦(如果每个插件维护者都单独使用Composer,则会发生这种情况)。请注意,这与Composer并不直接相关-如果手动复制库,则会遇到完全相同的问题(甚至可能更糟)。

In general if you have the same package installed multiple times, you probably will get some troubles (and this will happen if every plugin maintainer will use Composer on its own). Note that this is not directly related to Composer - if you copy libraries manually you will get exactly the same problem (maybe even worse).

如果您真的想使用Composer在您的插件中,您可以尝试使用 humbug / php-scoper ,它将修改已使用依赖项的命名空间并使它们唯一。

If you really want to use Composer in your plugin you may try tools like humbug/php-scoper which will modify namespaces of used dependencies and make them unique.

这篇关于WordPress插件+作曲家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆