/ PHP

Alias a version for Composer

I previously published instructions on using a specific commit hash for Composer. This is a quick and useful way for referencing the most recent version of our work during the development process.

In some cases, specifying a commit hash as the current version may compromise the dependency tree, causing a conflict like:

Your requirements could not be resolved to an installable set of packages.

This can be caused by a conflicting version with a child dependency:

acme/acme-app @ 1.0.0
requires:
 -- guzzlehttp/guzzle @ 5.0.3
 -- acme/acme-service @ 1.0.0
    requires:
    -- guzzlehttp/guzzle @ 5.0.1

In this case we have another dependency acme/acme-service that requires the guzzlehttp/guzzle package at a different version than the repository we are working in. Let's say we need 5.0.3 in the application because it fixes an issue we are currently working on.

As long as the API for the newer version is compatible with the older one (usually the case for most minor/patch changes), we can satisfy the acme/acme-service dependency by telling the package to use version 5.0.3 as 5.0.1:

{
    "require": {
        "guzzlehttp/guzzle": "5.0.3 as 5.0.1",
        "acme/acme-service": "1.0.0"
    }
}

We can also reference a commit hash as an alias:

{
    "require": {
        "guzzlehttp/guzzle": "dev-master#d61a4539f57d65620785604b8b380890225c518e as 5.0.1",
        "acme/acme-service": "1.0.0"
    }
}

You can learn more about Composer Aliases here.

I do not generally use this type of version aliasing in a production scenario if it can be avoided. There are some obvious pitfalls with testing and general confusion. However, it can be useful for quickly resolving a conflict during the development process.