dougua9328 2019-02-18 15:21
浏览 35

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

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.");
    }
}

i would like to override this function from Hyn\Tenancy\Database\Connection (laravel multi-tenant) but i have no idea at all, anyone give me some example?

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.");
    }
}

i had try service provider, but becuase the function have a parameter, i dont know how to pass it , and got error too this is the provider code i put into register $this->app->bind('Hyn\Tenancy\Database\Connection', 'App\Vendor\CustomConnection');

PHP Fatal error: Declaration of App\Vendor\CustomConnection::get() must be compatible with Hyn\Tenancy\Database\Connection::get(): Illuminate\Database\Connection in this is the error i got

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求计算赫斯特(Hurst)指数
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大