Loading your own fork of a third party library

If you are using a certain library for your project and you decide to change something in the library, you will want your project to use the patched version.



Introduction

A few days ago, I received an email from a reader asking me how to change something in a package that I created. I told him that currently, it is not possible to do what he requested, but that he can fork the package, make the change and the use that modified version in his application by leveraging Composer.

This is a common case scenario and it is well described in the Composer documentation: Loading a package from a VCS repository.

In this tutorial I will show you how to fork a package (laravelista/comments), make changes to the package and use that modified version in your Laravel application.

Forking

First things first, navigate to the repository of the package that you want to make changes on. For the purpose of this tutorial, this is the package laravelista/comments.

You have to have a GitHub account for this operation and you have to be logged in.

You will see a Fork button in the upper right corner of the page.

fork

Click on it. It will ask you where do you want to fork it (to which account/team). I will fork it to my own personal GitHub profile @mabasic.

As you can see in the image bellow the repository is now forked to my personal account:

Forked

Now we can proceed to make needed changes to the repository.

Changing

You can make changes to the repository on GitHub using your browser or you can clone the repository to your PC and make the changes there.

git clone git@github.com:mabasic/comments.git comments-fork

cd comments-fork

I made changes to the CommentList.jsx and CommentForm.jsx files. For this example, I translated the text when there are no comments and when the user is not logged in, to Croatian.

Now for this package to compile the JS code we need to execute:

npm install

gulp

This will create everything that we need.

Commit the changes and push to GitHub. You can view the changes here.

Requiring

We are ready now to use our fork of the package in our Laravel application.

One thing to remember here is that we made the changes in the master branch. Usually, you would want to make the changes in a different branch or release a new version.

In your Laravel application open composer.json file and add the package to the require section:

"require": {
    ...
    "laravelista/comments": "dev-master"
},

To instruct Composer to use our forked version instead, add this above the require section:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/mabasic/comments"
    }
],

When you run composer update now, you should get something similar to this:

$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing laravelista/syndra (1.2.1)
    Loading from cache

  - Installing league/fractal (0.14.0)
    Loading from cache

  - Installing laravelista/comments (dev-master 2f1ef8a)
    Downloading: 100%

Notice the part Installing laravelista/comments (dev-master 2f1ef8a) The last string is the code of the commit that we just pushed to GitHub. You can verify that by going to the commits page for the package.

To further verify that we got the forked package, you can visit the vendor/laravelista/comments directory and manually check if the files that we made changes on are located there.

Thank you for reading this tutorial. I am sure that you will find it very useful. Have an awesome week!

Comments