Have a PHP project, and want to re-use some classes in a new project. Moving them to their own repository and turning them into a Composer dependency is a clean way to do that. If hosted on GitHub/BitBucket, it’s even quite simple to publish the package on Packagist with automatic updates based on git tags. However, if still heavily developing both the project and the dependency, the round trip through repo/packagist is a pain.
But today I discovered there’s an option called --prefer-source
which seems to solve most of this pain. And here’s a basic note-to-self on how to get that to work…
0. Make sure dependency is a composer dependency
{
"name": "my/package",
"autoload":
{
"psr-4": {"": "src/"}
}
}
1. Add dependency repo and package to root project
{
"repositories":
[
{"type": "vcs", "url": "https://github.com/username/my-project"}
],
"require":
{
"my/project": "dev-master",
}
2. Run update with –prefer-source
We should now have the package downloaded and, more importantly, if you check ./vendor/my/package it should have the .git directory, meaning you can make immediately working changes there directly, and commit when you’re happy… Our other root project(s) depending on it should then get the update from the source repository after an easy composer update. ðŸ‘
Note: I’m a bit fuzzy on what composer does to keep track on whatever different happens through --prefer-source
, and it’s an option for both composer install
and composer update
. For example, at first attempt, I tried to use composer update --prefer-source
on a dependency that had already been downloaded, and the .git directory did not turn up, but if I just deleted the vendor directory for that package and then re-ran the command, then the .git was there.
So, feel free to comment if you have some light on that topic 😛🤓