Test a Composer package from a local version / copy

Problem: I need to test a small change to a Composer package that’s on GitHub / Packagist, but I don’t want (yet) to request the change from the maintainer or fork my own version on Packagist or whatever… I literally just want to change one digit in a version number requirement.

Not that it’s particularly relevant, but the Package in question is a Volume storage driver for Craft CMS that makes use of Fortrabbit’s Object Storage.

Solution: Composer has a repositories feature which allows us to add Repositories other than the default Packagist.org one by adding a new block to the project’s composer.json. There are quite a lot of different sorts of repositories that can be setup, but I’m interested in the path one. This option will allow me to stick a package’s contents almost anywhere on my local machine, and path to it. Example:

"repositories": [
  {
    "type": "path",
    "url": "path/to/package/",
    "options": {
        "symlink": true
    }
  }
]

Note the symlink value. This means that when the package is installed, a symlink will be created in the main project’s vendor directory pointing to the path set. This means that change made in the local copy will be instantly available in the main project without needing to update or copy anything.

Once the above is added to the project’s composer.json, it can be installed the ‘usual’ way. I.e

$ composer require myquickfix/a-composer-test

…and if there’s a package with that description in the path that was set in the url declaration, it will be installed.

All being well, I see the package being installed and symlinked in the command line output:

Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing myquickfix/a-composer-test (1.0.1): Symlinking from path/to/myquickfix/a-composer-test
Generating optimized autoload files...

Stability flag gotcha: When running the above, it’s quite common to get an error about a matching version. Like this:

[InvalidArgumentException]
Could not find a matching version of package myquickfix/a-composer-test. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (dev).

This can usually be fixed temporarily by adding or setting "minimum-stability": "dev" in the main project’s composer.json file. There’s a lot more to it if you want to go down a Composer version/stability rabbit hole.

It took a few attempts to get this working, usually my making naming mistakes with paths, or messing about with stability flags and getting it wrong. Please let me know if this works for you Nope, not helpful...Yep, more useful than not! (No Ratings Yet)
Loading...

Leave a Reply

Your email address will not be published. Required fields are marked *