Adam Lambert
Adam Lambert

Reputation: 1421

Composer - Using a custom fork of a package dependency

I am using the package teamtnt/laravel-scout-tntsearch-driver and I wish to make a very small change to one of the files within teamtnt/tntsearch which is one of the packages dependencies.

Usually I would:

  1. Create a fork of the package.
  2. Add the repository into my composer.json as follows:
  "repositories": [
    {"type": "vcs", "url": "https://github.com/user/packagefork"}
  ],
  1. Require/Upgrade the package to the correct version (usually dev-master) keeping the original name spacing and it all works fine.

However, with a dependency which is not directly included in my composer.json file, this does not seem to work. Do I need to fork both the base package, and the dependency package even though I do not need to change anything within the base?

I am hoping there is a simple way to do this, without having to fork each level.

Upvotes: 1

Views: 549

Answers (1)

Adam Lambert
Adam Lambert

Reputation: 1421

This was actually quite simple. Not too sure why it did not work originally! Instructions below for anyone wondering:

  1. Fork the package (i.e. GitHub)
  2. Add the repo from your username, to your projects main composer.json as follows:
  "repositories": [
    {"type": "vcs", "url": "https://github.com/youruser/tntsearch"}
  ],
  1. Edit the composer.json file within the new fork you created in step 1 (youruser/tntsearch) and create/add to the extras key:
    "extra": {
        "branch-alias": {
            "dev-master": "2.0.x-dev"
        }
    }

This effectively allows you to install your dev-master version, but allow other packages where this is a dependency to request the 2.0 version (in this case). You do need to be careful in this case that you have forked the correct version and any upgrades are correctly managed later down the line, or things may break!

More info on composer alias here

  1. Require/Upgrade the package using original package namespace, at the dev-master version.
composer require teamtnt/tntsearch:dev-master

The name spacing and package versions will remain the same as before your fork, but the edits from your fork will be pulled into your project instead.

Upvotes: 4

Related Questions