6

is it possible to tell composer to install a dependency only when using specified PHP versions?

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. However installing this library on PHP 5.5+ is completely pointless. So, Is it possible to tell composer to install ircmaxell/password-compat only when running on versions <5.5?

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.

P.S. Please posts only direct answers how to do this, not any workarounds or suggestions to drop 5.4 support. I can also come up with them, I am looking for the smart solution here :)

6
  • Yes look at this odino.org/managing-php-dependencies-with-composer Commented Aug 17, 2015 at 16:56
  • I think your best bet is taking a look at the scripts property and working something out with that. Commented Aug 17, 2015 at 16:56
  • @RiggsFolly Maybe I'm missing something, but I can't find anything in that article that answers the OP's question? Commented Aug 17, 2015 at 16:59
  • Yes, I also don't see an answer :) I hope I made my question clear, I will try to clarify more Commented Aug 17, 2015 at 17:07
  • There are 3 main parts in the composer.json, Look at PART 3 "require": { "php": ">=5.3.2", "kriswallsmith/Buzz": ">=0.5" } But I may be wrong Commented Aug 17, 2015 at 17:13

2 Answers 2

3

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.

You could also make password-compat an optional dependency (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. 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I see that it is not massive and I will do that if I won't get suitable solution here. I want to keep my project clean, this is why I'm asking for this.
Well thanks for more thoughts :) but 1) making it an optional dependency is not very nice, needs end user to think, complicates travis build. 2) making a secondary package doesn't solve the problem, the decision logic if to install it or not is still missing. 3) I am curious if this is possible in general, yes I can also come up with many similar workarounds :)
See the 'short answer' part then ;) Pretty confident in that.
2

Yes, it is possible.

Consider having one branch, e.g. 1.x for legacy php versions, like

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

and 2.x branch for 5.5+

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

This way loose version requirements, i.e. some/library:* will resolve to appropriate versions.

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.

1 Comment

Well, thanks, this looks as most close approach, however I also don't like this kind of automagic so I won't do it if it is not possible to do in a cleaner way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.