mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:34:04 +00:00
Get rid of tenant DB manager connection config
This commit is contained in:
parent
2fedd5ce88
commit
15a7e52208
11 changed files with 58 additions and 41 deletions
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Contracts;
|
||||
|
||||
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
|
||||
|
||||
interface TenantDatabaseManager
|
||||
{
|
||||
/**
|
||||
|
|
@ -35,6 +37,8 @@ interface TenantDatabaseManager
|
|||
|
||||
/**
|
||||
* Set the DB connection that should be used by the tenant database manager.
|
||||
*
|
||||
* @throws NoConnectionSetException
|
||||
*
|
||||
* @param string $connection
|
||||
* @return void
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace Stancl\Tenancy\Exceptions;
|
|||
|
||||
use Exception;
|
||||
|
||||
class ModelNotSyncMaster extends Exception
|
||||
class ModelNotSyncMasterException extends Exception
|
||||
{
|
||||
public function __construct(string $class)
|
||||
{
|
||||
13
src/Exceptions/NoConnectionSetException.php
Normal file
13
src/Exceptions/NoConnectionSetException.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class NoConnectionSetException extends Exception
|
||||
{
|
||||
public function __construct($manager)
|
||||
{
|
||||
parent::__construct("No connection was set on this $manager instance.");
|
||||
}
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Relations\Pivot;
|
|||
use Stancl\Tenancy\Contracts\SyncMaster;
|
||||
use Stancl\Tenancy\Events\SyncedResourceChangedInForeignDatabase;
|
||||
use Stancl\Tenancy\Events\SyncedResourceSaved;
|
||||
use Stancl\Tenancy\Exceptions\ModelNotSyncMaster;
|
||||
use Stancl\Tenancy\Exceptions\ModelNotSyncMasterException;
|
||||
|
||||
class UpdateSyncedResource extends QueueableListener
|
||||
{
|
||||
|
|
@ -31,7 +31,7 @@ class UpdateSyncedResource extends QueueableListener
|
|||
{
|
||||
if (! $centralModel instanceof SyncMaster) {
|
||||
// If we're trying to use a tenant User model instead of the central User model, for example.
|
||||
throw new ModelNotSyncMaster(get_class($centralModel));
|
||||
throw new ModelNotSyncMasterException(get_class($centralModel));
|
||||
}
|
||||
|
||||
/** @var SyncMaster|Model $centralModel */
|
||||
|
|
|
|||
|
|
@ -4,24 +4,23 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
|
||||
|
||||
class MySQLDatabaseManager implements TenantDatabaseManager
|
||||
{
|
||||
/** @var string */
|
||||
protected $connection;
|
||||
|
||||
public function __construct(Repository $config)
|
||||
{
|
||||
$this->connection = $config->get('tenancy.database_manager_connections.mysql');
|
||||
}
|
||||
|
||||
protected function database(): Connection
|
||||
{
|
||||
if ($this->connection === null) {
|
||||
throw new NoConnectionSetException(static::class);
|
||||
}
|
||||
|
||||
return DB::connection($this->connection);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,24 +4,23 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
|
||||
|
||||
class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
||||
{
|
||||
/** @var string */
|
||||
protected $connection;
|
||||
|
||||
public function __construct(Repository $config)
|
||||
{
|
||||
$this->connection = $config->get('tenancy.database_manager_connections.pgsql');
|
||||
}
|
||||
|
||||
protected function database(): Connection
|
||||
{
|
||||
if ($this->connection === null) {
|
||||
throw new NoConnectionSetException(static::class);
|
||||
}
|
||||
|
||||
return DB::connection($this->connection);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,19 +9,19 @@ use Illuminate\Database\Connection;
|
|||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
|
||||
|
||||
class PostgreSQLSchemaManager implements TenantDatabaseManager
|
||||
{
|
||||
/** @var string */
|
||||
protected $connection;
|
||||
|
||||
public function __construct(Repository $config)
|
||||
{
|
||||
$this->connection = $config->get('tenancy.database_manager_connections.pgsql');
|
||||
}
|
||||
|
||||
protected function database(): Connection
|
||||
{
|
||||
if ($this->connection === null) {
|
||||
throw new NoConnectionSetException(static::class);
|
||||
}
|
||||
|
||||
return DB::connection($this->connection);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue