How to require package that are compatiable with PHP version? - php

In composer.json I have dependencies on many PHP packages. I want for dependencies to resolve to packages, that are compatible with PHP 5.5, but still can be ran at PHP 7.
How do I do that?

In general, PHP is backward compatible, so you should be able to run your library on any newer version, so it should be enough for you to specify the lowest required version.
To specify the particular PHP version, use the platform configuration directive:
{
"name": "some/library",
"version": "1.0.0",
"config":
"platform": {
"php": "5.5"
}
}
}

Related

Composer installing packages for newer version

I run app in docker on my computer and I have a problem with updating php in composer. When I change php version in require composer updates packages to version higher that I specified and get syntax error.
I am updating php from 7.4 to 8.0, the steps i made were:
I change php require to this (i have tried "^8.0.0" and "~8.0.0" too)
"require": {
"php": "8.0.*",
...
then I change Dockerfile to pull from newer image FROM php:8.0-apache (i have checked php version inside the container and it is PHP 8.0.27 (cli))
next I run docker-compose exec -T app composer update --prefer-dist --ignore-platform-reqs
then new lock file is generated and there is for example this which was installed as dependency of another package, i dont have it as my require
{
"name": "monolog/monolog",
"version": "3.2.0",
...
"require": {
"php": ">=8.1",
...
},
when i check the package which has monolog as dependency there is this "monolog/monolog": "^1.17||^2.0||^3.0", so older version was available but newer was picked and installed even when it does not meet the requirements.
Can someone help and tell me what am I doing wrong please?

phpunit 5.6.4 doesn't work in php 5.6 for doctrine's dependency via composer

I have a problem composer install dependencies.
We use php which is version 5.6.24.
When installing phpunit 5.6.4, we install doctrine needs php^7. This is our partial composer.lock after require and install phpunit.
"packages-dev": [
{
"name": "doctrine/instantiator",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/doctrine/instantiator.git",
"reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/instantiator/zipball/185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda",
"reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda",
"shasum": ""
},
"require": {
"php": "^7.1"
},
Why does it cause and How do we fix this dependency from php 7 to 5?
As I thought, It is possible of using phpunit 5.6.4 in php 5 as https://packagist.org/packages/phpunit/phpunit#5.6.4 show. (We use cakephp 2.10.3 depending on sebastian/exporter 1.2. So we can use only under 5.6.4 phpunit version.)
and we use docker ,so we install library following command .
docker run --rm -it -v $(pwd):/app composer:1.4.1 require --dev phpunit/phpunit:"5.6.4" --ignore-platform-reqs --update-with-dependencies
This problem is caused by incompatible of doctrine version (see http://doctrine-project.org/2017/07/25/php-7.1-requirement-and-composer.html) and phpdocumentor/reflection-docblock and as it show. I can override invalid versions to use command composer
require (or --dev) package:$correct_version
Of course , as well as docker environment.
Composer file can have ambiguous dependencies' match setting like ^、〜 operator, therefore dependencies will be broken if library installed to be required by dependencies change requirements which has updated incompatible before. So we need implicitly definitions of their correct version in composer.json. Thanks to #TonyChiboucas's comment add your own requirement definition. It becomes a clue to resolve my problem.

Behat fails runing after updating dependencies

Just updated composer on a functioning Behat, and now getting the following messages open running behat.
PHP Deprecated: "Symfony\Component\Console\Helper\DialogHelper" is deprecated since version 2.5 and will be removed in 3.0.
Im using Behat-3 (~3.0#dev).
Any advice will be appreciated :)
I don't know what your composer.json looks like but I would suggest you to use stable versions for packages in require blocks. Try to use specific versions when possible.
"require": {
"hello/world": "1.2.3",
"abc/cde": "~4.3.0",
"php": ">=5.4.0"
},
"require-dev": {
"similar/to-above": "1.11"
},
"require-test": {
"similar/to-above": "3.2.6"
},

How do I use the latest version of Zend Framework 2.3.5 using Composer PHP?

What do I add in my Composer.json file so it downloads version 2.3.5 of the Zend Framework? I've tried reading the Zend docs but it doesn't mention Composer.
{
"require" : {
"silex/silex": "~1.1",
"monolog/monolog": "~1.7",
"aws/aws-sdk-php": "~2.6",
"zendframework/zendservice-amazon": "2.3.5"
},
"require-dev": {
"heroku/heroku-buildpack-php": "*"
},
"repositories": [
{
"type": "composer",
"url": "https://packages.zendframework.com/"
}
]
}
After I run composer update, it gives me this error message:
C:\Users\Ricky\graffiti-galore>composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested package zendframework/zendservice-amazon could not be found in any version, there may be a typo in the package name.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion for more details.
Read http://getcomposer.org/doc/articles/troubleshooting.md for further common problems.
In your require statement, it looks like you're using the wrong include for Zend. In your require statement:
"zendframework/zendservice-amazon": "2.3.5"
should be
"zendframework/zend-config": "2.3.5",
"zendframework/zend-http": "2.3.5"
Or if you want to avoid requiring a specific version number,
"zendframework/zend-config": "2.*",
"zendframework/zend-http": "2.*"
and for the part in minimum stability
"minimum-stability": "dev"
zendservice-amazon is not part of Zend Framework 2, none of the ZendService libs are. Its latest version is 2.0.3, all versions are listed here: https://packagist.org/packages/zendframework/zendservice-amazon
There is no version 2.3.5 for zendframework/zendservice-amazon, so obviously the install fails. Look at https://packagist.org/packages/zendframework/zendservice-amazon to see the available versions and fix the version selector (I'd suggest ~2.0).
You also don't need the repositories part in your composer.json, all the packages are also on Packagist, Composer's main and default package repository.

Force composer to require PHP Version between Version X and Version Y

I have a mix of different PHP versions running on my servers (max 5.3.5) and development machines (max 5.5.9).
Now I ran into the problem that I did a "composer update" to get the latest Version of some external Bundles.
My composer.json looks like
"require": {
"php": ">=5.3.3",
.....
},
I get some Bundles that required PHP 5.5. There is no problem on my dev machines; the problem occurs on the server.
Is it possibile to tell Composer to require a PHP version between 5.3.3 and 5.3.5? Or a max available Version?
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.
Any Ideas?
Since the config parameter in composer.json is available. You could something like this:
{
"name": ".../...",
"config": {
"platform": {
"php": "5.3.5"
}
},
"require": {
...
}
}
https://getcomposer.org/doc/06-config.md#platform
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.
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.
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).
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).
Remove your composer.lock and vendor directory.
Now place platform option to composer.json
"config": {
"platform": {
"php": "7.0"
}
},
and finally, run command composer install
Try this (remove comma):
"require": {
"php": ">=5.3.3 <=5.3.5",
.....
},
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.
Next Significant Release (Tilde Operator)#
The ~ operator is best explained by example:
~1.2 is equivalent to
=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.
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.
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.
Is there any possibility to tell composer to require a PHP version
between 5.3.3 and 5.3.5?
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-
or you may use composer.json like this:
{
"require": {
"guzzlehttp/guzzle": ">=5.3.4 <6"
}
}
- I personally prefer this way because it's much easier to read and remember IMHO.

Categories