Using your own fork of a public library (without publishing anything to packagist.org)

You’re working away and you hit a bug in one of your Composer dependencies. You quickly find the problem, it’s a one line fix, back to work. The problem now is: how do you push that fix to the rest of your team and to your production environment?

Since Composer installed the package from GitHub you could submit a Pull Request, but who knows how quickly the library author will respond. Or maybe they won’t like your fix at all! In either case, you need to get this code shipped & you can’t be waiting for someone else’s approval.

Thankfully, with GitHub and Composer this problem is a cinch to resolve: fork the dependency’s codebase on GitHub & tell Composer to use a different repository for that dependency. Read on to find out how!

1. Fork the library on GitHub

Make sure your fork is publicly accessible. Since you’re forking an open source project you want your changes to be public, right? For the sake of example, let’s say that I want to fork monolog so that I can put my own awesome customizations in it. Ready, set… fork!

Note: For the rest of this example I’m assuming that the changes you want are in the master branch of your forked repository.

Clone your fork locally, make your one-line fix, push it back up to GitHub. Or, even easier, use GitHub’s editor to edit the file directly in the repository! Either way, you’ve now got a web-cloneable repository that has the fix you need.

2. Set composer.json to use the dev-master version of the library.

Open composer.json in your editor. Look for this:

…
"require": {
	"monolog/monolog": "~1.6",
	…
},
…

Change the version part ("~1.6") to "dev-master". Now you have:

…
"require": {
	"monolog/monolog": "dev-master",
	…
},
…

3. Add your repository to composer.json

One last thing, then we’re done editing composer.json.

If you don’t see a "repositories" key at the same indentation as "require" and "require-dev" then go ahead and add it. It’s an array of objects:

…
"repositories": [
	{
		"type": "vcs",
		"url": "https://github.com/your-username/monolog"
	}
],
…

Change the URL to the URL of your fork. This is JSON, so pay close attention to your syntax: no trailing commas!

4. Run composer update

Like so:

composer update monolog/monolog

This will replace the vendor/monolog/monolog code with the code from your repository and update your composer.lock.

5. Commit composer.json & composer.lock

This will allow you (or anyone else!) to replicate the exact same dependencies in any environment.

git commit -m "Change to customized monolog/monolog" composer.json composer.lock

You’re done! Not too painful.

Source: fff.red

Other Links