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 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行