mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 14:14:03 +00:00
Add a hook that should run before trying to fetch tenants
Signed-off-by: michael lundbøl <michael.lundboel@gmail.com>
This commit is contained in:
parent
726a0462eb
commit
39c1e65172
4 changed files with 103 additions and 0 deletions
|
|
@ -13,6 +13,12 @@ trait TenantAwareCommand
|
||||||
/** @return int */
|
/** @return int */
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
|
$callback = $this->runBeforeRunningTenantsHook();
|
||||||
|
|
||||||
|
if ($callback !== 0) {
|
||||||
|
return $callback;
|
||||||
|
}
|
||||||
|
|
||||||
$tenants = $this->getTenants();
|
$tenants = $this->getTenants();
|
||||||
$exitCode = 0;
|
$exitCode = 0;
|
||||||
|
|
||||||
|
|
@ -35,4 +41,13 @@ trait TenantAwareCommand
|
||||||
* @return Tenant[]
|
* @return Tenant[]
|
||||||
*/
|
*/
|
||||||
abstract protected function getTenants(): array;
|
abstract protected function getTenants(): array;
|
||||||
|
|
||||||
|
private function runBeforeRunningTenantsHook()
|
||||||
|
{
|
||||||
|
if (! method_exists($this, 'beforeRunningTenants')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int) $this->laravel->call([$this, 'beforeRunningTenants']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
47
tests/Etc/AddUserConditionally.php
Normal file
47
tests/Etc/AddUserConditionally.php
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests\Etc;
|
||||||
|
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Stancl\Tenancy\Traits\HasATenantsOption;
|
||||||
|
use Stancl\Tenancy\Traits\TenantAwareCommand;
|
||||||
|
|
||||||
|
class AddUserConditionally extends Command
|
||||||
|
{
|
||||||
|
use TenantAwareCommand, HasATenantsOption;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'user:add_conditionally {--stop}';
|
||||||
|
|
||||||
|
public function beforeRunningTenants()
|
||||||
|
{
|
||||||
|
if ($this->option('stop')) {
|
||||||
|
$this->error('You stopped the command conditionally');
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
User::create([
|
||||||
|
'name' => Str::random(10),
|
||||||
|
'email' => Str::random(10) . '@gmail.com',
|
||||||
|
'email_verified_at' => now(),
|
||||||
|
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
|
||||||
|
'remember_token' => Str::random(10),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,5 +16,6 @@ class ConsoleKernel extends Kernel
|
||||||
protected $commands = [
|
protected $commands = [
|
||||||
ExampleCommand::class,
|
ExampleCommand::class,
|
||||||
AddUserCommand::class,
|
AddUserCommand::class,
|
||||||
|
AddUserConditionally::class
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,4 +32,44 @@ class TenantAwareCommandTest extends TestCase
|
||||||
$this->assertNotEmpty(\DB::table('users')->get());
|
$this->assertNotEmpty(\DB::table('users')->get());
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function commands_can_optionally_specify_a_hook_that_should_be_ran_before_running_the_command_within_a_tenant()
|
||||||
|
{
|
||||||
|
$tenant1 = Tenant::new()->save();
|
||||||
|
$tenant2 = Tenant::new()->save();
|
||||||
|
|
||||||
|
\Artisan::call('tenants:migrate', [
|
||||||
|
'--tenants' => [$tenant1['id'], $tenant2['id']],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->withoutMockingConsoleOutput();
|
||||||
|
$response = $this->artisan('user:add_conditionally', [
|
||||||
|
'--tenants' => [$tenant1['id'], $tenant2['id']],
|
||||||
|
'--stop' => true
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(1, $response);
|
||||||
|
|
||||||
|
tenancy()->initializeTenancy($tenant1);
|
||||||
|
$this->assertEmpty(\DB::table('users')->get());
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
tenancy()->initializeTenancy($tenant2);
|
||||||
|
$this->assertEmpty(\DB::table('users')->get());
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
$this->artisan('user:add_conditionally', [
|
||||||
|
'--tenants' => [$tenant1['id'], $tenant2['id']],
|
||||||
|
'--stop' => false
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->initializeTenancy($tenant1);
|
||||||
|
$this->assertNotEmpty(\DB::table('users')->get());
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
tenancy()->initializeTenancy($tenant2);
|
||||||
|
$this->assertNotEmpty(\DB::table('users')->get());
|
||||||
|
tenancy()->end();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue