如何在Laravel 5.5中扩展供应商包服务提供商 [英] How to extend vendor package service provider in Laravel 5.5

查看:65
本文介绍了如何在Laravel 5.5中扩展供应商包服务提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个集成了Xero记帐的软件包.

I am using a package that integrates Xero accounting.

它们在以下位置有一个名为XeroServiceProvider.php的文件:/vendor/drawmyattention/xerolaravel/Providers/XeroServiceProvider.php.

They have a file called XeroServiceProvider.php in the following location: /vendor/drawmyattention/xerolaravel/Providers/XeroServiceProvider.php.

我需要在应用程序中扩展此服务提供者,但我不喜欢直接编辑此文件的想法.

I need to extend this service provider in my application but I don't like the idea of editing this file directly.

有没有一种方法可以轻松扩展此服务提供商而无需更新供应商文件?

Is there a way I can extend this service provider easily without updating vendor files?

这是我需要扩展的文件:

Here is the file I need to extend:

namespace DrawMyAttention\XeroLaravel\Providers;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\ServiceProvider;
use \App\Invoice;

class XeroServiceProvider extends ServiceProvider
{
    private $config = 'xero/config.php';

    public function boot()
    {
        $this->publishes([
            __DIR__.'/../config.php' => config_path($this->config),
        ]);
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('XeroInvoice', function(){
           //return new \XeroPHP\Models\Accounting\Invoice();
           return new Invoice();
        });

    }
}

推荐答案

运行php artisan make:provider ExtendedXeroServiceProvider

将其添加到providers下的./config/app.php

打开./app/Providers/ExtendedXeroServiceProvider.php

extends ServiceProvider更改为extends XeroServiceProvider

也添加use DrawMyAttention\XeroLaravel\Providers\XeroServiceProvider

将原始服务提供商添加到./composer.json

Add the original service provider to the discovery blacklist in ./composer.json

编辑

在撰写本文时,drawmyattention/xerolaravel软件包不使用自动发现,但是在使用时,可以将其添加到composer.json中:

as of the time of writing, the drawmyattention/xerolaravel package does not use autodiscovery, but in the event that it does, this can be added to the composer.json:

"extra": {
    "laravel": {
        "dont-discover": [
            "DrawMyAttention\\XeroLaravel\\Providers\\XeroServiceProvider"
        ]
    }
},

这篇关于如何在Laravel 5.5中扩展供应商包服务提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆