1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-07 00:04:04 +00:00

Simplify TenantWithDatabase interface, move tenantConfig() logic

This commit is contained in:
Samuel Štancl 2024-03-01 10:48:33 +01:00
parent 8a269f8dd8
commit e8c3c75d7c
5 changed files with 68 additions and 59 deletions

View file

@ -4,15 +4,38 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\DatabaseConfig;
/**
* @mixin Model
*/
trait HasDatabase
{
use HasInternalKeys;
public function database(): DatabaseConfig
{
/** @var TenantWithDatabase $this */
/** @var TenantWithDatabase&Model $this */
$databaseConfig = [];
return new DatabaseConfig($this);
foreach ($this->getAttributes() as $key => $value) {
if (str($key)->startsWith($this->internalPrefix() . 'db_')) {
if ($key === $this->internalPrefix() . 'db_name') {
// Remove DB name because we set that separately
continue;
}
if ($key === $this->internalPrefix() . 'db_connection') {
// Remove DB connection because that's not used here
continue;
}
$databaseConfig[str($key)->after($this->internalPrefix() . 'db_')->toString()] = $value;
}
}
return new DatabaseConfig($this, $databaseConfig);
}
}

View file

@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
/**
* @mixin \Illuminate\Database\Eloquent\Model
*/
trait HasInternalKeys
{
/** Get the internal prefix. */