mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-14 05:24:03 +00:00
-ing event behavior
This commit is contained in:
parent
c40dba4e02
commit
33d6fd82da
9 changed files with 120 additions and 6 deletions
|
|
@ -55,7 +55,6 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
Events\DomainDeleted::class => [],
|
Events\DomainDeleted::class => [],
|
||||||
|
|
||||||
// Database events
|
// Database events
|
||||||
// todo: let -ing events cacnel the operations
|
|
||||||
Events\DatabaseCreated::class => [],
|
Events\DatabaseCreated::class => [],
|
||||||
Events\DatabaseMigrated::class => [],
|
Events\DatabaseMigrated::class => [],
|
||||||
Events\DatabaseSeeded::class => [],
|
Events\DatabaseSeeded::class => [],
|
||||||
|
|
@ -63,7 +62,6 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
Events\DatabaseDeleted::class => [],
|
Events\DatabaseDeleted::class => [],
|
||||||
|
|
||||||
// Tenancy events
|
// Tenancy events
|
||||||
// todo: let -ing events cacnel the operations
|
|
||||||
Events\InitializingTenancy::class => [],
|
Events\InitializingTenancy::class => [],
|
||||||
Events\TenancyInitialized::class => [
|
Events\TenancyInitialized::class => [
|
||||||
Listeners\BootstrapTenancy::class,
|
Listeners\BootstrapTenancy::class,
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ use Stancl\Tenancy\DatabaseManager;
|
||||||
use Stancl\Tenancy\Events\DatabaseMigrated;
|
use Stancl\Tenancy\Events\DatabaseMigrated;
|
||||||
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
||||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||||
|
use Stancl\Tenancy\Events\MigratingDatabase;
|
||||||
|
|
||||||
class Migrate extends MigrateCommand
|
class Migrate extends MigrateCommand
|
||||||
{
|
{
|
||||||
|
|
@ -60,6 +61,8 @@ class Migrate extends MigrateCommand
|
||||||
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
|
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
|
||||||
$this->line("Tenant: {$tenant['id']}");
|
$this->line("Tenant: {$tenant['id']}");
|
||||||
|
|
||||||
|
event(new MigratingDatabase($tenant));
|
||||||
|
|
||||||
// Migrate
|
// Migrate
|
||||||
parent::handle();
|
parent::handle();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||||
use Stancl\Tenancy\DatabaseManager;
|
use Stancl\Tenancy\DatabaseManager;
|
||||||
use Stancl\Tenancy\Events\DatabaseSeeded;
|
use Stancl\Tenancy\Events\DatabaseSeeded;
|
||||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||||
|
use Stancl\Tenancy\Events\SeedingDatabase;
|
||||||
|
|
||||||
class Seed extends SeedCommand
|
class Seed extends SeedCommand
|
||||||
{
|
{
|
||||||
|
|
@ -58,6 +59,8 @@ class Seed extends SeedCommand
|
||||||
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
|
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
|
||||||
$this->line("Tenant: {$tenant['id']}");
|
$this->line("Tenant: {$tenant['id']}");
|
||||||
|
|
||||||
|
event(new SeedingDatabase($tenant));
|
||||||
|
|
||||||
// Seed
|
// Seed
|
||||||
parent::handle();
|
parent::handle();
|
||||||
|
|
||||||
|
|
|
||||||
6
src/Events/CreatingDatabase.php
Normal file
6
src/Events/CreatingDatabase.php
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Events;
|
||||||
|
|
||||||
|
class CreatingDatabase extends Contracts\TenantEvent
|
||||||
|
{}
|
||||||
7
src/Events/MigratingDatabase.php
Normal file
7
src/Events/MigratingDatabase.php
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Events;
|
||||||
|
|
||||||
|
class MigratingDatabase extends Contracts\TenantEvent
|
||||||
|
{
|
||||||
|
}
|
||||||
7
src/Events/SeedingDatabase.php
Normal file
7
src/Events/SeedingDatabase.php
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Events;
|
||||||
|
|
||||||
|
class SeedingDatabase extends Contracts\TenantEvent
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,7 @@ use Illuminate\Queue\SerializesModels;
|
||||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
use Stancl\Tenancy\DatabaseManager;
|
use Stancl\Tenancy\DatabaseManager;
|
||||||
|
use Stancl\Tenancy\Events\CreatingDatabase;
|
||||||
use Stancl\Tenancy\Events\DatabaseCreated;
|
use Stancl\Tenancy\Events\DatabaseCreated;
|
||||||
|
|
||||||
class CreateDatabase implements ShouldQueue
|
class CreateDatabase implements ShouldQueue
|
||||||
|
|
@ -29,6 +30,8 @@ class CreateDatabase implements ShouldQueue
|
||||||
|
|
||||||
public function handle(DatabaseManager $databaseManager)
|
public function handle(DatabaseManager $databaseManager)
|
||||||
{
|
{
|
||||||
|
event(new CreatingDatabase($this->tenant));
|
||||||
|
|
||||||
if ($this->tenant->getInternal('create_database') !== false) {
|
if ($this->tenant->getInternal('create_database') !== false) {
|
||||||
$databaseManager->ensureTenantCanBeCreated($this->tenant);
|
$databaseManager->ensureTenantCanBeCreated($this->tenant);
|
||||||
$this->tenant->database()->makeCredentials();
|
$this->tenant->database()->makeCredentials();
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ class Tenancy
|
||||||
public $tenant;
|
public $tenant;
|
||||||
|
|
||||||
/** @var callable|null */
|
/** @var callable|null */
|
||||||
public static $getBootstrappers = null;
|
public $getBootstrappersUsing = null;
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public $initialized = false;
|
public $initialized = false;
|
||||||
|
|
@ -55,11 +55,12 @@ class Tenancy
|
||||||
public function getBootstrappers(): array
|
public function getBootstrappers(): array
|
||||||
{
|
{
|
||||||
// If no callback for getting bootstrappers is set, we just return all of them.
|
// If no callback for getting bootstrappers is set, we just return all of them.
|
||||||
$resolve = static::$getBootstrappers ?? function (Tenant $tenant) {
|
$resolve = $this->getBootstrappersUsing ?? function (Tenant $tenant) {
|
||||||
return array_map('app', config('tenancy.bootstrappers'));
|
return config('tenancy.bootstrappers');
|
||||||
};
|
};
|
||||||
|
|
||||||
return $resolve($this->tenant);
|
// Here We instantiate the bootstrappers and return them.
|
||||||
|
return array_map('app', $resolve($this->tenant));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function query(): Builder
|
public function query(): Builder
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,21 @@ namespace Stancl\Tenancy\Tests;
|
||||||
use Illuminate\Events\CallQueuedListener;
|
use Illuminate\Events\CallQueuedListener;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Illuminate\Support\Facades\Queue;
|
use Illuminate\Support\Facades\Queue;
|
||||||
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Database\Models\Domain;
|
||||||
|
use Stancl\Tenancy\Events\BootstrappingTenancy;
|
||||||
|
use Stancl\Tenancy\Events\CreatingDatabase;
|
||||||
|
use Stancl\Tenancy\Events\CreatingTenant;
|
||||||
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use Stancl\Tenancy\Listeners\QueueableListener;
|
use Stancl\Tenancy\Listeners\QueueableListener;
|
||||||
use Stancl\Tenancy\Events\TenantCreated;
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
|
use Stancl\Tenancy\Events\UpdatingDomain;
|
||||||
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
|
use Stancl\Tenancy\Tenancy;
|
||||||
use Stancl\Tenancy\Tests\TestCase;
|
use Stancl\Tenancy\Tests\TestCase;
|
||||||
|
|
||||||
class EventListenerTest extends TestCase
|
class EventListenerTest extends TestCase
|
||||||
|
|
@ -41,6 +53,80 @@ class EventListenerTest extends TestCase
|
||||||
|
|
||||||
$this->assertFalse(app()->bound('foo'));
|
$this->assertFalse(app()->bound('foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function ing_events_can_be_used_to_cancel_tenant_model_actions()
|
||||||
|
{
|
||||||
|
Event::listen(CreatingTenant::class, function () {
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertSame(false, Tenant::create()->exists);
|
||||||
|
$this->assertSame(0, Tenant::count());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function ing_events_can_be_used_to_cancel_domain_model_actions()
|
||||||
|
{
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
|
Event::listen(UpdatingDomain::class, function () {
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
$domain = $tenant->domains()->create([
|
||||||
|
'domain' => 'acme',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$domain->update([
|
||||||
|
'domain' => 'foo',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertSame('acme', $domain->refresh()->domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function ing_events_can_be_used_to_cancel_db_creation()
|
||||||
|
{
|
||||||
|
Event::listen(CreatingDatabase::class, function (CreatingDatabase $event) {
|
||||||
|
$event->tenant->setInternal('create_database', false);
|
||||||
|
});
|
||||||
|
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
dispatch_now(new CreateDatabase($tenant));
|
||||||
|
|
||||||
|
$this->assertFalse($tenant->database()->manager()->databaseExists(
|
||||||
|
$tenant->database()->getName()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function ing_events_can_be_used_to_cancel_tenancy_bootstrapping()
|
||||||
|
{
|
||||||
|
config(['tenancy.bootstrappers' => [
|
||||||
|
DatabaseTenancyBootstrapper::class,
|
||||||
|
RedisTenancyBootstrapper::class,
|
||||||
|
]]);
|
||||||
|
|
||||||
|
Event::listen(
|
||||||
|
TenantCreated::class,
|
||||||
|
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener()
|
||||||
|
);
|
||||||
|
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
|
||||||
|
Event::listen(BootstrappingTenancy::class, function (BootstrappingTenancy $event) {
|
||||||
|
$event->tenancy->getBootstrappersUsing = function () {
|
||||||
|
return [DatabaseTenancyBootstrapper::class];
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
|
$this->assertSame([DatabaseTenancyBootstrapper::class], array_map('get_class', tenancy()->getBootstrappers()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FooListener extends QueueableListener
|
class FooListener extends QueueableListener
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue