php - 使用 Laravel 服务提供者覆盖连接器类

标签 php laravel

我在 Laravel 5.2 中工作,我正在尝试让它与 Vertica 一起工作。几个月前,我和我的同事想出了 this解决方案,但我们现在正试图让事情变得不那么 hacky,并使用服务提供商来让事情正常运行,这样我们实际上可以更轻松地升级 Laravel。所以到目前为止我们所做的是:

1) 创建两个扩展其对应对象的新类:

新的 BaseConnector:

namespace App\Vertica;
include 'clsPDOVertica.php';

use Illuminate\Support\Arr;
use \Illuminate\Database\Connectors\Connector as BaseConnector;

class Connector extends BaseConnector
{
    /**
     * Create a new PDO connection.
     *
     * @param  string  $dsn
     * @param  array   $config
     * @param  array   $options
     * @return \PDO
     */
    public function createConnection($dsn, array $config, array $options)
    {
        $username = Arr::get($config, 'username');

        $password = Arr::get($config, 'password');

        return new PDOVertica($dsn, $username, $password, $options);
    }
}

新的 PostgresConnector:
namespace App\Vertica;

use \Illuminate\Database\Connectors\PostgresConnector as BasePostgresConnector;

class PostgresConnector extends BasePostgresConnector
{

    /**
     * Create a DSN string from a configuration.
     *
     * @param  array   $config
     * @return string
     */
    protected function getDsn(array $config)
    {
        // First we will create the basic DSN setup as well as the port if it is in
        // in the configuration options. This will give us the basic DSN we will
        // need to establish the PDO connections and return them back for use.
        extract($config, EXTR_SKIP);

        $host = isset($host) ? "host={$host};" : '';

        $dsn = "Driver={$driverpath};{$host}Database={$database}";

        // If a port was specified, we will add it to this Postgres DSN connections
        // format. Once we have done that we are ready to return this connection
        // string back out for usage, as this has been fully constructed here.
        if (isset($config['port'])) {
            $dsn .= ";port={$port}";
        }

        if (isset($config['sslmode'])) {
            $dsn .= ";sslmode={$sslmode}";
        }

        return $dsn;
    }
}

现在,我们正在尝试定义一个服务提供者来本质上告诉 Laravel 使用我们的类而不是默认类……但到目前为止还没有成功。这是提供者的代码:
namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class VerticaServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        // dd(new \Illuminate\Database\Connectors\PostgresConnector);
        $this->app->singleton('\Illuminate\Database\Connectors\Connector', function()
        {
            return new \App\Vertica\Connector();
        });

        $this->app->singleton('\Illuminate\Database\Connectors\PostgresConnector', function()
        {
            return new \App\Vertica\PostgresConnector();
        });
    }
}

到目前为止,我们的 VerticaServiceProvider 的 register 方法被调用,但显然,内部的绑定(bind)是错误的,因为我们的类没有被调用。有人知道我们做错了什么吗?

最佳答案

Laravel 无法解析 Connector容器中的类,因此尝试按类名覆盖连接器将不起作用。

您可以在 Illuminate/Database/Connectors/ConnectionFactory::createConnector 中查看连接器是如何解决的。 Laravel 只是做了一个 return new PostgresConnector (或适用于驱动程序的任何一个),因此它不会在容器中查找类名。

然而,在它"new"出现之前,Connector ,它会检查容器以查看是否有使用字符串 'db.connector.[driver]' 绑定(bind)到驱动程序的连接器, 其中 [driver]是数据库驱动程序名称。

因此,与其尝试绑定(bind)容器中的类名,不如绑定(bind)字符串'db.connector.your-driver-name'。 .因此,如果您创建了自己的自定义驱动程序(例如 vertica ),您可以将连接器绑定(bind)到 'db.connector.vertica' .或者,如果你想覆盖内置的 postgres 连接器,你可以将你的连接器绑定(bind)到 'db.connector.pgsql' .

基于您尝试覆盖 postgres 连接器的假设,您的服务提供商注册方法如下所示:

public function register()
{
    $this->app->bind('db.connector.pgsql', \App\Vertica\PostgresConnector::class);
}

关于php - 使用 Laravel 服务提供者覆盖连接器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42583366/

相关文章:

php - 更新查询 laravel 返回错误结果并且没有错误显示为什么?

PHP cURL,提取 XML 响应

laravel - 无法使用 Dompdf Laravel 9 加载图像

php - Laravel:如何使用 Homestead 安装 php7

php - PhpStorm 中 Eloquent ORM 代码提示

laravel - 如何在 Laravel 中插入数据库之前连接自动增量值

php - 删除所有斜杠正则表达式

php - 如何用 Idiorm 语法写这个

php - 使用 Symfony2 Doctrine2 控制台/生成功能更改表格?

PHP CPU 和内存使用情况