douyou2732 2015-01-23 06:58
浏览 52
已采纳

覆盖使用Composer安装的库中的类的策略

I am using Codeigniter and Composer. One of the requirements is PHPExcel. Now I need to change a function in one of the classes. What should be the best strategy to do it? Should I change the code in the vendor folder? If so, how to maintain the change across all the instances? If not how do I override that particular class. Though I mention PHPExcel I would like a generic solution.

I am not sure if this is the right forum for this question. If not i will remove this. Please let me know if any more details are needed.

Thank You.

  • 写回答

3条回答 默认 最新

  • doubomudichen0832 2016-01-04 21:21
    关注

    In composer.json, under ["autoload"]["psr-4"], add an entry with namespace as the key and path as the value:

    {
         "autoload": {
    
             "psr-4": {
    
                 "BuggyVendor\\Namespace\\": "myfixes/BuggyVendor/Namespace"
             }
         }
    }
    

    Copy files you want to override under that path (keeping sub-namespace directory structure) and edit them there. They will be picked in preference to the library package's original "classpath". It would seem that namespace->path mappings added to composer.json in this manner are considered before those added by required packages. Note: I just tried it and it worked, though I don't know if it is an intended feature or what possible gotchas are.

    EDIT: found a gotcha. Sometimes when you subsequently require another package with composer require vendor/package, you will "lose" the override. If this happens, you must issue composer dump-autoload manually. This will restore the correct autoload order honoring your override.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?