Using forked version of PHP packages with composer

So you want to use a fork of any existing PHP vendor package and use the fork for update by composer dependency manager , instead of original source. This does not need you to push any update or register your package on packagist.org. Instead, the approach recommended is to follow a few simple steps mentioned below:

Step 1

We will start with an example. Let’s say you want to fork the package kartik-v/yii2-widgets on the Github repository.

Step 2

Fork the library on GitHub. It is important for you to check if the source contains a valid composer.json file. This is important for the rest of the steps to work. After the fork, let’s say your forked package version is now at https://github.com/yourname/yii2-widgets. As a next step, you must create a custom branch (you will update the version constraint in composer.json later to point to your custom branch). Your custom branch name must be prefixed with dev-. For example, let us assume you create a branch named custom and your patched updates will be within a branch named dev-custom.

Step 3

Push your library changes to the custom branch above (You can check which one it is on Packagist, under “Source:” – Example).

Step 4

Override your composer.json in your Yii2 applications root folder to point to your fork repository. The original composer.json before would have something like below:

"require": {
    "kartik-v/yii2-widgets": "*"
},

The composer.json after edit will need to have these additional lines. Note that the dev-custom branch named above is used in the require section as shown below.

{
    "repositories":
    [
            {
                "type": "vcs",
                "url": "https://github.com/yourname/yii2-widgets"
            }
    ],
    "require": {
        "kartik-v/yii2-widgets": "dev-custom"
    },
}

Step 5

Now use one of the following options as mentioned below, to install the package from your console in your application root.

Option 1: Newbie users

A safer command which uses your composer.lock settings.

php composer.phar install

Option 2: Experienced users

This command will refresh/update all your packages with dependencies to the latest version.

php composer.phar update

Composer should tell you that files have been modified. Answer yes to any questions. This should enable to use your own fork now for all future updates by composer on your environment.

3 thoughts on “Using forked version of PHP packages with composer

  1. I really like your JQuery Plugins and Yii Extensions. Now you have really complemented it with these real useful tips and tutorials. Thanks for making it free and available.

  2. But what will happen if I decide to use both my version of yii2-widget and yours too, because some of yours extensions depends on yours version of yii2-widget ? Arent they going to be in namespace conflict ?

    1. You will maintain only one version of the repo in your application. Let’s say you have configured a fork of kartik-v/yii2-widgets from vendor-fork/yii2-widgets as mentioned here. Namespace will still use kartik\widgets\ClassName but it will refer to your forked repo version.

      The precise advantage is you do not need to worry about namespace dependencies. Secondly, you can sync the fork with the source based on your needs.

Comments are closed.