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
}
}
Nacosvel DataSource Manager is made available under the MIT License (MIT). Please see License File for more information.