How to require a fork with composer

混江龙づ霸主 提交于 2019-11-26 12:06:24
Seldaek

The most common (and easier) way of doing it is using a VCS repository.

All you have to do is add your fork as a repository and update the version constraint to point to your custom branch. Your custom branch name must be prefixed with dev-.

Example assuming you patched monolog to fix a bug in the bugfix branch:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/igorw/monolog"
        }
    ],
    "require": {
        "monolog/monolog": "dev-bugfix"
    }
}

Note that you don't change the require statement except to specify your bugfix branch. You still reference the upstream package (monolog/monolog), not your personal fork (igorw/monolog). You can read details in the docs

Neilime

Using VCS works:

"name": "test/test",
"repositories": [{
    "type": "vcs",
    "url": "http://github.com/Nodge/lessphp"
}],
"require": {
    "leafo/lessphp": "dev-master"
},

But if I require a module that has this composer.json, it doesn't work. It installs the original project, not the fork.

Example

"name": "example/example",
"require": {
    "test/test": "dev-master"
},

I should mention again the repository. Is that normal?

Boedy

If you can't get @Neilime answer to work for you, make sure your fork uses a different branch.

For example push your changes to a branch on your fork called my-bugfix, do not added dev- prefix in your branch name but in your composer.json you have to add it. Your composer file will look like:

"repositories":
[
    {
        "type": "vcs",
        "url": "http://github.com/yourname/packageName"
    }
],
"require": {
    "owner/packageName": "dev-my-bugfix"
},

According to the Composer documentation http://getcomposer.org/doc/05-repositories.md#vcs, it's enough to specify the original repository (not the fork) in the require ("nodge/lessphp" in your case). Composer will then install YOUR fork (look at the code in the vendors)

I have tried many options but After I got this post I saw the light and it just worked perfect.

This is what you have to do:

1- Fork de repository

2- Create a branch and make the required modifications.

3- Add the repository label to your composer.json

"repositories": [

        {
            "type": "vcs",
            "url": "https://github.com/user/yourforkname"
        }
    ]

4- In the command line inside your project require your fork like this:

composer require vendor/packagename:dev-branchname

And Voilá!!

You have your fork version working

I usually add a "dist" node to the package definition. I never had a problem using it this way.

I can't remember where I got this trick from, though, for any further explanations.

{
    "repositories": [
        {
            "type": "package",
            "package": {
                "version": "dev-master",
                "name": "nodge/lessphp",
                "source": {
                    "url": "https://github.com/Nodge/lessphp.git",
                    "type": "git",
                    "reference": "master"
                },
                "autoload": {
                    "classmap": ["lessc.inc.php"]
                },
                "dist": {
                    "url": "https://github.com/Nodge/lessphp/archive/master.zip",
                    "type": "zip"
                }
            }
        }
    ],
    "require": {
        "nodge/lessphp": "*"
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!