Permalink
Browse files

added the ability to override the connection of any arbitrary model u…

…sing the booted event
  • Loading branch information...
luceos committed Apr 17, 2018
1 parent 48b4dbc commit cc08069c639af9db686412f576163f268a4a8202
@@ -246,6 +246,19 @@
* @info Only deletes in case tenant database is set to be deleted.
*/
'auto-delete-tenant-database-user' => env('TENANCY_DATABASE_AUTO_DELETE_USER', false),
/**
* Define a list of classes that you wish to force onto the tenant or system connection.
* The connection will be forced when the Model has booted.
*
* @info Useful for overriding the connection of third party packages.
*/
'force-tenant-connection-of-models' => [
// \App\User::class
],
'force-system-connection-of-models' => [
// \App\User::class
],
],
'folders' => [
'config' => [
@@ -1,5 +1,6 @@
- 5.1.9
- Added Macroable trait for easier extending to Environment, Directory and Connection.
- Added two new configuration items `tenancy.db.force-tenant-connection-of-models` and `tenancy.db.force-system-connection-of-models` to force models from third party packages you have little control over onto the tenant or system connection.
- 5.1.8
- Removed all directory separators, this is unnecessary as the filesystem driver takes care of that.
- 5.1.7
@@ -0,0 +1,43 @@
<?php
namespace Hyn\Tenancy\Listeners\Database;
use Hyn\Tenancy\Database\Connection;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Str;
class OverridesConnection
{
/**
* @var Connection
*/
private $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public function subscribe(Dispatcher $events)
{
$events->listen('eloquent.booted:*', [$this, 'override']);
}
public function override(string $event, $model)
{
$class = Str::replaceFirst('eloquent.booted: ', null, $event);
// Somehow on booting/booted the argument is an array.
if (is_array($model)) {
$model = array_first($model);
}
if (in_array($class, config('tenancy.db.force-tenant-connection-of-models', []))) {
$model->setConnection($this->connection->tenantName());
}
if (in_array($class, config('tenancy.db.force-system-connection-of-models', []))) {
$model->setConnection($this->connection->systemName());
}
}
}
@@ -35,6 +35,8 @@ class EventProvider extends ServiceProvider
Listeners\Database\SeedsTenants::class,
// Sets the connection used to manage tenant.
Listeners\Database\FillsConnectionUsed::class,
// Forces the connection on configured models.
Listeners\Database\OverridesConnection::class,
// Manages the directories for the tenants.
Generators\Filesystem\DirectoryGenerator::class,
// Sets the uuid value on a website based on tenancy configuration.
@@ -0,0 +1,21 @@
<?php
/*
* This file is part of the hyn/multi-tenant package.
*
* (c) Daniël Klabbers <daniel@klabbers.email>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see https://laravel-tenancy.com
* @see https://github.com/hyn/multi-tenant
*/
namespace Hyn\Tenancy\Tests\Extend;
use Illuminate\Database\Eloquent\Model;
class NonExtend extends Model
{
}
@@ -14,6 +14,7 @@
namespace Hyn\Tenancy\Tests\Database;
use Hyn\Tenancy\Tests\Extend\NonExtend;
use Hyn\Tenancy\Tests\Test;
use Illuminate\Database\Connection as DatabaseConnection;
@@ -81,4 +82,24 @@ public function can_migrate_the_tenant()
$this->assertTrue($this->connection->get()->getSchemaBuilder()->hasTable('samples'));
}
/**
* @test
*/
public function override_to_tenant_connection()
{
config(['tenancy.db.force-tenant-connection-of-models' => [NonExtend::class]]);
$this->assertEquals($this->connection->tenantName(), (new NonExtend())->getConnectionName());
}
/**
* @test
*/
public function override_to_system_connection()
{
config(['tenancy.db.force-system-connection-of-models' => [NonExtend::class]]);
$this->assertEquals($this->connection->systemName(), (new NonExtend())->getConnectionName());
}
}

0 comments on commit cc08069

Please sign in to comment.