飘易博客(作者:Flymorn)
订阅《飘易博客》RSS,第一时间查看最新文章!
飘易首页 | 留言本 | 关于我 | 订阅Feed

Laravel如何使用github上自己fork来的依赖包资源

Author:飘易 Source:飘易
Categories:PHP编程 PostTime:2017-8-29 15:07:09
正 文:

Laravel框架里通过composer可以非常方便的引入第三方开发好的各种依赖包,自从有了composer,不少程序员们开始宣称自己不生产代码,只是Github的搬运工了。


多数情况下,已有的依赖包可以解决绝大多数问题。但是开发的需求是各种各样的,我们难免需要改造一下现成的包,这个时候,可以在github上fork一下原来的项目到自己名下,然后修改、提交在自己名下的项目,如果你的代码确实可以提高原项目的质量,或者解决了某bug,可以造福更多人,可以Create pull request给原作者,只要原作者同意,就可以合并merge你的代码了。


但是,实际上可能你的代码只符合特定情况下的使用场景不适合合并到主版本库里去,或者原作者已经不太维护代码了,难道我们只能慢慢等待原作者合并?


NO!!   


fork项目到我们自己账户下之后,可以直接修改我们名下的这个项目,各种修改、提交,随便你啦。在laravel根目录下的 composer.json 里指定使用我们的fork版本就可以了。


下面以 https://github.com/Zizaco/entrust 项目为例,fork到我自己账户下的项目地址:https://github.com/flymorn/entrust 。


一般laravel根目录下的 composer.json 格式如下:

{

    "name": "laravel/laravel",

    "description": "The Laravel Framework.",

    "keywords": ["framework", "laravel"],

    "license": "MIT",

    "type": "project",

    "require": {

        "php": ">=5.5.9",

        "laravel/framework": "5.1.*",

        "caouecs/laravel-lang": "~3.0",

        "aliyuncs/oss-sdk-php": "~2.0",

        "overtrue/laravel-wechat": "~3.0",

        "lokielse/omnipay-alipay": "dev-master",

        "zizaco/entrust": "dev-master"

    },

    ...

}


使用我们自己fork的版本,只需要添加 repositories 字段:

{

    "name": "laravel/laravel",

    "description": "The Laravel Framework.",

    "keywords": ["framework", "laravel"],

    "license": "MIT",

    "type": "project",

    "repositories": [

        {

            "type": "vcs",

            "url": "https://github.com/flymorn/entrust"

        }

    ],

    "require": {

        "php": ">=5.5.9",

        "laravel/framework": "5.1.*",

        "caouecs/laravel-lang": "~3.0",

        "aliyuncs/oss-sdk-php": "~2.0",

        "overtrue/laravel-wechat": "~3.0",

        "lokielse/omnipay-alipay": "dev-master",

        "zizaco/entrust": "dev-master"

    },

    ...

}


注意上面的红色部分。上面表明了我们要使用飘易自己fork来的 flymorn/entrust 代替原先的 zizaco/entrust 资源。请留意 require 里的必须是原始项目的包名;同时后面的版本号不再是原有项目的版本号,而是fork来的项目的版本号。比如上面的 dev-master 实际上是指 flymorn/entrust 项目的版本号。我们fork来的项目一般都是直接在这个主版本号 dev-master 上直接修改,因此,采用这种方式的版本号一般就是 dev-master 了。注意,根据VCS资源加载的要求,fork的自定义分支必须以 dev- 开头!


然后执行:composer update

或者更新单个依赖包:composer update zizaco/entrust

就可以啦。


如果执行的过程中出现类似下列的错误:

Loading composer repositories with package information
Reading composer.json of zizaco/entrust (2.0-dev)
Could not fetch https://api.github.com/repos/flymorn/entrust/commits/9cafb045b9e7b10a91d1685c84cf92ca60bb3dd3, please create a GitHub OAuth token to go over the API rate limit
Head to https://github.com/settings/tokens/new?scopes=repo&description=Composer+on+admin-PC+2017-08-29+1351
to retrieve a token. It will be stored in "C:/Users/admin/AppData/Roaming/Composer/auth.json" for future use by Composer.
Token (hidden):


那是因为访问github的资源需要令牌,你只需要到 

https://github.com/settings/tokens/new?scopes=repo

生成一个token,然后把token输入到命令行里即可。



关于composer的VCS使用说明如下:

Loading a package from a VCS repository#

There are a few use cases for this. The most common one is maintaining 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. If the library is on GitHub (this is the case most of the time), you can simply fork it there and push your changes to your fork. After that you update the project's composer.json. 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-". For version constraint naming conventions see Libraries for more information.

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"
    }
}

When you run php composer.phar update, you should get your modified version of monolog/monologinstead of the one from packagist.


具体的可以参考:https://getcomposer.org/doc/05-repositories.md#vcs 


本文完。

作者:飘易
来源:飘易
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。
上一篇:MYSQL之not in优化方法:left join
下一篇:mysql外键约束foreign key ON DELETE CASCADE ON UPDATE CASCADE
0条评论 “Laravel如何使用github上自己fork来的依赖包资源”
No Comment .
发表评论
名称(*必填)
邮件(选填)
网站(选填)

记住我,下次回复时不用重新输入个人信息
© 2007-2019 飘易博客 Www.Piaoyi.Org 原创文章版权由飘易所有