在composer.json中的需求中使用条件OR?

We have a problem with composer. Our library requires a either ... or ... library. So basically it requires it like this:

"php-64bit": ">=5.4.0"

OR

"php": ">=5.4.0" AND "ext-example": "^1.0.2"

So basically it requires a specific PHP version. Additionally it requires a 64bit version of PHP OR a specific library to work.

Is this possible to do with composer? If so how? If not can we solve it in another way?

I assume, you intend to make your library available through Packagist.

Composer can run scripts triggered by events, but only those defined in the root composer.json. Include a script to detect the PHP environment OS agnostically (64 or 32bit) in your library. Since you require ">=5.4.0" in both cases, your script can conditionally require your additional library "ext-example": "^1.0.2" when in a 32bit environment.

Example Cmd.php in your library:

namespace Some\Name\Space;
class Cmd
{
    public static funtion check32() {
    // detect environment here... then:
    if ($is32) {
        $cmd = 'php composer.phar require vendor/32bit-library:dev-master';
        exec($cmd, $output, $return_var);
    }
}

This will run composer.phar in the app's root directory.

Referring entry in root composer.json:

"scripts": {
    "post-install-cmd": [
        "Some\\Name\\Space\\Cmd::check32"
    ]
}

The caveat here is that exec() must be available on your user's machine and that the user has to include your library as well as your post-install-cmd in their composer.json.

I'd think you should not go overboard with your dependency definition.

Both platform situations require PHP 5.4 or later. I'd add that as the only hard dependency.

Composer has a "suggest" feature. Your extension could be suggested with a descriptive text to indicate that only the 32bit platform would need it.

Your code would already have to deal with the situation, so you probably have checks implemented to see whether you are using 64bits (and omit using the extension) or not. That code might emit an error when being used on 32bit without the extension.

"require": {
    "php": ">=5.4"
},
"suggest": {
    "ext-example":"Required to use this package on 32bit PHP"
}

This avoids having the user add a script to his composer.json that does nothing more than helping him understand why it fails when first trying to install your package. He'd have to read the documentation anyway.