简体   繁体   English

指定PHP版本的Composer依赖项

[英]Composer dependency for specified PHP version

is it possible to tell composer to install a dependency only when using specified PHP versions? 是否可以告诉composer仅在使用指定的PHP版本时才安装依赖项?

Reason: my library uses password_hash function, which is available in 5.5+, and there is a compatibility library ircmaxell/password-compat for PHP 5.4. 原因:我的库使用了password_hash函数,该函数在5.5+中可用,并且有一个兼容库ircmaxell / password-compat for PHP 5.4。 However installing this library on PHP 5.5+ is completely pointless. 但是,在PHP 5.5+上安装此库完全没有意义。 So, Is it possible to tell composer to install ircmaxell/password-compat only when running on versions <5.5? 那么,是否有可能告诉作曲家只在<5.5版本上运行时才安装ircmaxell / password-compat?

Short story to make the question more clear - I want to tell composer to: 短篇小说让问题更加清晰 - 我想告诉作曲家:

IF php version < 5.5:
    install ircmaxell/password-compat
ELSE:
    skip ircmaxell/password-compat

Thanks in advance. 提前致谢。

PS Please posts only direct answers how to do this, not any workarounds or suggestions to drop 5.4 support. PS请仅发布直接答案如何执行此操作,而不是任何变通办法或建议删除5.4支持。 I can also come up with them, I am looking for the smart solution here :) 我也可以拿出来,我在这里寻找智能解决方案:)

The short answer is "It's not possible". 简短的回答是“这是不可能的”。

The dependency is not a massive one. 依赖性不是很大。 Why not simply let it install anyway? 为什么不简单地让它安装呢? If you are on PHP 5.5 the built-in password functions will still be used. 如果您使用的是PHP 5.5,则仍将使用内置密码功能。

You could also make password-compat an optional dependency ( suggests ). 您还可以使用password-compat作为可选依赖项( suggests )。 The issue then is that it's up to the maintainer to install it alongside your application. 那么问题在于维护人员可以将它与您的应用程序一起安装。

Lastly, you could make a secondary, virtual package. 最后,您可以创建一个辅助虚拟包。 Say your package is called 'Acme', It would be possible to create a secondary 'Acme-php54' package that depends on both password_compat and your main project. 说你的包被称为“Acme的,这将有可能创造一个次要“的Acme-php54”包, 同时依赖于password_compat和你的主要项目。 This keeps the dependency outside of your project, but I would argue that the simplest is to just always install it as long as you intend to support PHP 5.4, and just drop PHP 5.4 in a little while when it EOLs. 这样可以保持项目之外的依赖关系,但我认为最简单的方法就是只要你打算支持PHP 5.4就可以随时安装它,并且只需要在EOLs中稍微删除PHP 5.4。

Yes, it is possible. 对的,这是可能的。

Consider having one branch, eg 1.x for legacy php versions, like 考虑有一个分支,例如1.x用于遗留php版本,例如

{
    "name": "some/library",
    "version": "1.0.0",
    "require": {
        "ircmaxell/password-compat": "*"
    }
}

and 2.x branch for 5.5+ 和2.x分支为5.5+

{
    "name": "some/library",
    "version": "2.0.0",
    "require": {
        "php": ">=5.5"
    }
}

This way loose version requirements, ie some/library:* will resolve to appropriate versions. 这样松散版本的要求,即some/library:*将解析为适当的版本。

Another way is to instruct the users to add 另一种方法是指示用户添加

"replace": {
    "ircmaxell/password-compat": "*"
}

by themselves if needed. 如果需要的话。

Note that some users (including myself) can change their php interpreter on the fly and won't be too happy to debug this kind of automagical issues. 请注意,一些用户(包括我自己)可以动态更改他们的PHP解释器,并且不会太高兴调试这种自动化问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM