mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 09:34:04 +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 => [],
|
||||
|
||||
// Database events
|
||||
// todo: let -ing events cacnel the operations
|
||||
Events\DatabaseCreated::class => [],
|
||||
Events\DatabaseMigrated::class => [],
|
||||
Events\DatabaseSeeded::class => [],
|
||||
|
|
@ -63,7 +62,6 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
Events\DatabaseDeleted::class => [],
|
||||
|
||||
// Tenancy events
|
||||
// todo: let -ing events cacnel the operations
|
||||
Events\InitializingTenancy::class => [],
|
||||
Events\TenancyInitialized::class => [
|
||||
Listeners\BootstrapTenancy::class,
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ use Stancl\Tenancy\DatabaseManager;
|
|||
use Stancl\Tenancy\Events\DatabaseMigrated;
|
||||
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Events\MigratingDatabase;
|
||||
|
||||
class Migrate extends MigrateCommand
|
||||
{
|
||||
|
|
@ -59,6 +60,8 @@ class Migrate extends MigrateCommand
|
|||
|
||||
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']}");
|
||||
|
||||
event(new MigratingDatabase($tenant));
|
||||
|
||||
// Migrate
|
||||
parent::handle();
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
|||
use Stancl\Tenancy\DatabaseManager;
|
||||
use Stancl\Tenancy\Events\DatabaseSeeded;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Events\SeedingDatabase;
|
||||
|
||||
class Seed extends SeedCommand
|
||||
{
|
||||
|
|
@ -58,6 +59,8 @@ class Seed extends SeedCommand
|
|||
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']}");
|
||||
|
||||
event(new SeedingDatabase($tenant));
|
||||
|
||||
// Seed
|
||||
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\Tenant;
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
use Stancl\Tenancy\Events\CreatingDatabase;
|
||||
use Stancl\Tenancy\Events\DatabaseCreated;
|
||||
|
||||
class CreateDatabase implements ShouldQueue
|
||||
|
|
@ -29,6 +30,8 @@ class CreateDatabase implements ShouldQueue
|
|||
|
||||
public function handle(DatabaseManager $databaseManager)
|
||||
{
|
||||
event(new CreatingDatabase($this->tenant));
|
||||
|
||||
if ($this->tenant->getInternal('create_database') !== false) {
|
||||
$databaseManager->ensureTenantCanBeCreated($this->tenant);
|
||||
$this->tenant->database()->makeCredentials();
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ class Tenancy
|
|||
public $tenant;
|
||||
|
||||
/** @var callable|null */
|
||||
public static $getBootstrappers = null;
|
||||
public $getBootstrappersUsing = null;
|
||||
|
||||
/** @var bool */
|
||||
public $initialized = false;
|
||||
|
|
@ -55,11 +55,12 @@ class Tenancy
|
|||
public function getBootstrappers(): array
|
||||
{
|
||||
// If no callback for getting bootstrappers is set, we just return all of them.
|
||||
$resolve = static::$getBootstrappers ?? function (Tenant $tenant) {
|
||||
return array_map('app', config('tenancy.bootstrappers'));
|
||||
$resolve = $this->getBootstrappersUsing ?? function (Tenant $tenant) {
|
||||
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
|
||||
|
|
|
|||
|
|
@ -5,9 +5,21 @@ namespace Stancl\Tenancy\Tests;
|
|||
use Illuminate\Events\CallQueuedListener;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
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\Listeners\QueueableListener;
|
||||
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;
|
||||
|
||||
class EventListenerTest extends TestCase
|
||||
|
|
@ -41,6 +53,80 @@ class EventListenerTest extends TestCase
|
|||
|
||||
$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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue