nacosvel/datasource-manager

Nacosvel DataSource Manager.

dev-main / 1.x-dev 2024-11-06 16:17 UTC

This package is auto-updated.

Last update: 2024-11-06 16:18:55 UTC


README

GitHub Tag Total Downloads Packagist Version Packagist PHP Version Support Packagist License

Installation

You can install the package via Composer:

composer require nacosvel/datasource-manager

文档

因不同框架的容器对象不同,需要借助 nacosvel/container-interop 完成容器交互。

use Nacosvel\Container\Interop\Discover;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Discover::container();
    }

}

不同框架实现方式可能不一致,可以在服务提供者中实现容器发现功能。

具体操作查看 nacosvel/container-interop

将数据库管理对象绑定到 Nacosvel\Contracts\DatabaseManager\DatabaseManagerInterface 接口。

use Illuminate\Support\ServiceProvider;
use Nacosvel\Container\Interop\Discover;
use Nacosvel\Contracts\DatabaseManager\DatabaseManagerInterface;
use Nacosvel\DataSourceManager\DatabaseManager;
use Nacosvel\DataSourceManager\TransactionManager;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->bind(DatabaseManagerInterface::class, function () {
            return new DatabaseManager($this->app['db']);
        });
        $this->app->bind('TM', function ($app) {
            return new TransactionManager($app[DatabaseManagerInterface::class], new class() {
                protected mixed $connection;

                public function beginTransaction(): string
                {
                    return 'beginTransaction';
                }

                public function __invoke(mixed $connection): static
                {
                    $this->connection = $connection;
                    return $this;
                }

                public function __call(string $method, array $parameters)
                {
                    return call_user_func_array([$this->connection, $method], $parameters);
                }
            });
        });
        Discover::container();
        // app('TM')->beginTransaction();// beginTransaction
    }

}

License

Nacosvel DataSource Manager is made available under the MIT License (MIT). Please see License File for more information.