覆盖供应商类Hyn \ Tenancy \ Database \ Connection Laravel上的函数

时间:2019-02-18 15:21:44

标签: php laravel-5 multi-tenant

public function generateConfigurationArray(Website $website): array 
{

    switch ($mode) {
        case static::DIVISION_MODE_SEPARATE_DATABASE:

            // ORIGINAL CODE
            $clone['username'] = $clone['database'] = $website->uuid;
            $clone['password'] = $this->passwordGenerator->generate($website);
            break;
        case static::DIVISION_MODE_BYPASS:
            break;
        default:
            throw new ConnectionException("Division mode '$mode' unknown.");
    }
}

我想从Hyn \ Tenancy \ Database \ Connection(laravel多租户)中覆盖此功能,但是我一点也不知道,有人给我一些例子吗?

public function generateConfigurationArray(Website $website): array
{

    switch ($mode) {
        case static::DIVISION_MODE_SEPARATE_DATABASE:

            // MAKE CHANGE CODE
            $clone['username'] = 'username';
            $clone['database'] = $website->uuid;
            $clone['password'] = 'password';
            break;
        case static::DIVISION_MODE_BYPASS:
            break;
        default:
            throw new ConnectionException("Division mode '$mode' unknown.");
    }
}

我尝试了服务提供者,但由于函数具有参数,我不知道如何传递它,并且也出错,这也是我放入寄存器$ this-> app-> bind('Hyn \租户\数据库\连接','应用程序\供应商\自定义连接');

PHP致命错误:App \ Vendor \ CustomConnection :: get()声明必须与Hyn \ Tenancy \ Database \ Connection :: get():Illuminate \ Database \ Connection兼容,这是我得到的错误

CustomConnectionServiceProvider.php     

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Hyn\Tenancy\Providers\Tenants\ConnectionProvider as ConnectionProvider;
use Hyn\Tenancy\Database\Connection;

class CustomConnectionServiceProvider extends ConnectionProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        /*$loader = \Illuminate\Foundation\AliasLoader::getInstance();
        $loader->alias('Hyn\Tenancy\Database\Connection', 'app\Vendor\CustomConnection');*/

        $this->app->bind('Hyn\Tenancy\Database\Connection', 'App\Vendor\CustomConnection');
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
    }
}

app / vendor / CustomConnection.php

<?php

namespace App\Vendor;

use Hyn\Tenancy\Database\Connection as Connection;

class CustomConnection extends Connection
{
    public function generateConfigurationArray(Website $website): array
    {
        $clone = config(sprintf(
            'database.connections.%s',
            $website->managed_by_database_connection ?? $this->systemName()
        ));

        $mode = config('tenancy.db.tenant-division-mode');

        $this->emitEvent(new Events\Database\ConfigurationLoading($mode, $clone, $this));

        // Even though username/password mutate, let's store website UUID so we can match it up.
        $clone['uuid'] = $website->uuid;

        switch ($mode) {
            case static::DIVISION_MODE_SEPARATE_DATABASE:
                $clone['username'] = 'username'
                %clone['database'] = $website->uuid;
                $clone['password'] = 'password';
                break;
            case static::DIVISION_MODE_SEPARATE_PREFIX:
                $clone['prefix'] = sprintf('%d_', $website->id);
                break;
            case static::DIVISION_MODE_BYPASS:
                break;
            default:
                throw new ConnectionException("Division mode '$mode' unknown.");
        }

        $this->emitEvent(new Events\Database\ConfigurationLoaded($clone, $this));

        return $clone;
    }
}

0 个答案:

没有答案