Skip to content

Commit

Permalink
chore: using libsql manager
Browse files Browse the repository at this point in the history
  • Loading branch information
darkterminal committed Jul 13, 2024
1 parent 0741d95 commit 9a2048d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/LibSQLDriverServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

class LibSQLDriverServiceProvider extends PackageServiceProvider
{
public function boot(): void
{
parent::boot();
if (config('database.default') !== 'libsql' || config('database.connections.libsql.driver') === 'libsql') {
return;
}
}

public function configurePackage(Package $package): void
{
/*
Expand All @@ -29,14 +37,20 @@ public function register(): void
return new LibSQLConnectionFactory($app);
});

$this->app->scoped(LibSQLManager::class, function () {
return new LibSQLManager(config('database.connections.libsql'));
});

$this->app->resolving('db', function (DatabaseManager $db) {
$db->extend('libsql', function ($config, $name) {
$config = config('database.connections.libsql');
$config['name'] = $name;
if (! isset($config['driver'])) {
$config['driver'] = 'libsql';
}
$db = app()->get(LibSQLConnector::class)->connect($config);

$connector = new LibSQLConnector();
$db = $connector->connect($config);

$connection = new LibSQLConnection($db, $config['database'] ?? ':memory:', $config['prefix'], $config);
app()->instance(LibSQLConnection::class, $connection);
Expand Down
29 changes: 29 additions & 0 deletions src/LibSQLManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Turso\Driver\Laravel;

use BadMethodCallException;
use Illuminate\Support\Collection;
use Turso\Driver\Laravel\Database\LibSQLDatabase;

class LibSQLManager
{
protected LibSQLDatabase $client;

protected Collection $config;

public function __construct(array $config = [])
{
$this->config = new Collection($config);
$this->client = new LibSQLDatabase($config);
}

public function __call(string $method, array $arguments = []): mixed
{
if (! method_exists($this->client, $method)) {
throw new BadMethodCallException('Call to undefined method '.static::class.'::'.$method.'()');
}

return $this->client->$method(...$arguments);
}
}

0 comments on commit 9a2048d

Please sign in to comment.