强制作曲家要求版本 X 和版本 Y 之间的 PHP 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26277151/
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
Force composer to require PHP Version between Version X and Version Y
提问by Fabian
we have a mix of different PHP versions running on your servers (max 5.3.5) and development machines (max 5.5.9).
我们在您的服务器(最大 5.3.5)和开发机器(最大 5.5.9)上混合运行不同的 PHP 版本。
Now we ran into the problem that we did a "composer update" to get the latest Version of some external Bundles. Since your composer.json looks like
现在我们遇到了问题,我们进行了“作曲家更新”以获取某些外部 Bundle 的最新版本。由于您的 composer.json 看起来像
"require": {
"php": ">=5.3.3",
.....
},
we get some Bundles that required PHP 5.5. No problem on our dev machines, but on the server :(
我们得到了一些需要 PHP 5.5 的 Bundle。在我们的开发机器上没问题,但在服务器上:(
Is there any possibility to tell composer to require a PHP version between 5.3.3 and 5.3.5? Or a max available Version?
有没有可能告诉作曲家需要 5.3.3 和 5.3.5 之间的 PHP 版本?或者最大可用版本?
I tried
我试过
"require": {
"php": ">=5.3.3, <=5.3.5",
.....
},
and
和
"require": {
"php": "<=5.3.5",
.....
},
but both didn't work out. I get a "The requested package php could not be found in any version, there may be a typo in the package name." Error.
但两者都没有成功。我得到一个“在任何版本中都找不到请求的包 php,包名称中可能有拼写错误。” 错误。
Any Ideas? Thanks in advance
有任何想法吗?提前致谢
采纳答案by Sven
I find it questionable to say the least that you are developing with the newest PHP available and are running production with a very outdated version. There will be plenty of possible problems arising from this, not only because of security patches that you would be missing, but more importantly because of PHP bug fixes that got introduced mostly in versions 5.3.9 and 5.3.23 that changes PHP behavior in some details pretty fundamentally. Not talking about the risk of accidentally using features of 5.4 or 5.5.
我发现至少可以说您正在使用最新的 PHP 进行开发并且正在使用非常过时的版本运行生产,这是有问题的。将会有很多可能的问题由此产生,不仅是因为您会丢失安全补丁,更重要的是因为主要在 5.3.9 和 5.3.23 版本中引入的 PHP 错误修复在某些情况下改变了 PHP 的行为。细节非常基本。不是在谈论意外使用 5.4 或 5.5 功能的风险。
And there really is no way to make Composer deal with this situation. The PHP version that is used when running composer update
determines the resolution of dependencies, being influenced by PHP version and installed PHP extensions.
而且真的没有办法让 Composer 处理这种情况。运行时使用的 PHP 版本composer update
决定了依赖项的解析,受 PHP 版本和安装的 PHP 扩展的影响。
You cannot define that a package should only be used for PHP versions between 5.3.3 and 5.3.5 if the PHP you are using for the update is not matching this version requirement. Because the used PHP version exceeds the upper version constraint, such a package is not eligible for fulfilling the version requirement, and Composer reports that no package has been found (not telling that it has seen the packages, but they had to be ignored because of the version constraint).
如果您用于更新的 PHP 不符合此版本要求,则您不能定义包仅应用于 5.3.3 和 5.3.5 之间的 PHP 版本。因为使用的 PHP 版本超过了版本上限,这样的包不符合版本要求,并且 Composer 报告没有找到包(不是说它已经看到了这些包,但因为它们不得不被忽略)版本约束)。
There are probably three obvious ways out:
可能有三种明显的出路:
Downgrade your development environment to the production version you are really using. If more than one is used: The oldest one. That way any requirements for PHP versions will be matched. Run
composer update
then, and you are done.Upgrade your production environment. Needs no further explanation, but I have to mention that not only are you missing a lot of very nice PHP features, you are also missing a substantial performance increase, because PHP 5.5 is really that much faster than 5.3.
Add a "platform.php" configuration to either the global or project's composer.json. This will tell Composer to override the PHP version running Composer itself, and instead calculate the dependencies with that different PHP version.
composer config -g platform.php 5.3.5
for global setting (will affect all further Composer runs), without-g
for local setting (will only affect Composer operations in that project, in case you develop on more than one project with different production versions of PHP).
将您的开发环境降级到您真正使用的生产版本。如果使用多个:最旧的一个。这样,PHP 版本的任何要求都将匹配。
composer update
然后运行,你就完成了。升级您的生产环境。无需进一步解释,但我不得不提一下,您不仅错过了许多非常好的 PHP 功能,还错过了显着的性能提升,因为 PHP 5.5 确实比 5.3 快得多。
将“platform.php”配置添加到全局或项目的 composer.json。这将告诉 Composer 覆盖运行 Composer 本身的 PHP 版本,并计算与该不同 PHP 版本的依赖关系。
composer config -g platform.php 5.3.5
用于全局设置(将影响所有进一步的 Composer 运行),不-g
用于本地设置(只会影响该项目中的 Composer 操作,以防您在多个项目上使用不同的 PHP 生产版本进行开发)。
回答by NuSphere
Since the config parameter in composer.json is available. You could something like this:
由于 composer.json 中的 config 参数可用。你可以是这样的:
{
"name": ".../...",
"config": {
"platform": {
"php": "5.3.5"
}
},
"require": {
...
}
}
回答by Mike Milkman
Try this (remove comma):
试试这个(去掉逗号):
"require": {
"php": ">=5.3.3 <=5.3.5",
.....
},
回答by Billy
What about trying the tilde operator
试试波浪号运算符怎么样
Tilde Operator ~1.2 Very useful for projects that follow semantic versioning. ~1.2 is equivalent to >=1.2,<2.0. For more details, read the next section below.
波浪号运算符 ~1.2 对于遵循语义版本控制的项目非常有用。~1.2 相当于 >=1.2,<2.0。有关更多详细信息,请阅读下面的下一部分。
Next Significant Release (Tilde Operator)#
下一个重要版本(波浪号运算符)#
The ~ operator is best explained by example: ~1.2 is equivalent to
~ 操作符最好通过例子来解释:~1.2 等价于
=1.2,<2.0, while ~1.2.3 is equivalent to >=1.2.3,<1.3. As you can see it is mostly useful for projects respecting semantic versioning. A common usage would be to mark the minimum minor version you depend on, like ~1.2 (which allows anything up to, but not including, 2.0). Since in theory there should be no backwards compatibility breaks until 2.0, that works well. Another way of looking at it is that using ~ specifies a minimum version, but allows the last digit specified to go up.
=1.2,<2.0,而 ~1.2.3 等价于 >=1.2.3,<1.3。如您所见,它对于尊重语义版本控制的项目最有用。一个常见的用法是标记你所依赖的最小次要版本,比如 ~1.2(它允许任何高达但不包括 2.0 的东西)。因为理论上在 2.0 之前应该不会有向后兼容性中断,所以效果很好。另一种看待它的方式是使用 ~ 指定最低版本,但允许指定的最后一位数字上升。
Note: Though 2.0-beta.1 is strictly before 2.0, a version constraint like ~1.2 would not install it. As said above ~1.2 only means the .2 can change but the 1. part is fixed.
注意:虽然 2.0-beta.1 严格在 2.0 之前,但像 ~1.2 这样的版本限制不会安装它。如上所述 ~1.2 仅意味着 .2 可以更改但 1. 部分是固定的。
Note: The ~ operator has an exception on its behavior for the major release number. This means for example that ~1 is the same as ~1.0 as it will not allow the major number to increase trying to keep backwards compatibility.
注意: ~ 运算符在其主要版本号的行为上有一个例外。这意味着例如 ~1 与 ~1.0 相同,因为它不允许主编号增加以保持向后兼容性。
回答by sh6210
Remove your composer.lockand vendordirectory.
删除您的composer.lock和vendor目录。
Now place platformoption to composer.json
现在将平台选项放置到composer.json
"config": {
"platform": {
"php": "7.0"
}
},
and finally, run command composer install
最后,运行命令composer install
回答by whyer
Is there any possibility to tell composer to require a PHP version between 5.3.3 and 5.3.5?
有没有可能告诉作曲家需要 5.3.3 和 5.3.5 之间的 PHP 版本?
Yes, there it is one:
是的,有一个:
Hyphenated Version Range ( - )
Inclusive set of versions. Partial versions on the right include are completed with a wildcard. For example 1.0 - 2.0 is equivalent to >=1.0.0 <2.1 as the 2.0 becomes 2.0.*. On the other hand 1.0.0 - 2.1.0 is equivalent to >=1.0.0 <=2.1.0.
Example: 1.0 - 2.0
https://getcomposer.org/doc/articles/versions.md#hyphenated-version-range-
带连字符的版本范围 ( - )
包含版本集。右侧包含的部分版本使用通配符完成。例如,1.0 - 2.0 等价于 >=1.0.0 <2.1,因为 2.0 变为 2.0.*。另一方面,1.0.0 - 2.1.0 相当于 >=1.0.0 <=2.1.0。
示例:1.0 - 2.0
https://getcomposer.org/doc/articles/versions.md#hyphenated-version-range-
P.S. I got here from search engine but didn't find an answer here, so when I found it on Composer's site I decided to post it here. Hope this helps someone who got here like I did.
PS 我是从搜索引擎到这里的,但在这里没有找到答案,所以当我在 Composer 的网站上找到它时,我决定把它贴在这里。希望这对像我一样来到这里的人有所帮助。