1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-08-06 04:54:04 +00:00
tenancy/src/Commands/Seed.php
2026-08-04 20:57:48 -07:00

74 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\Console\Seeds\SeedCommand;
use Stancl\Tenancy\Concerns\HasATenantsOption;
use Stancl\Tenancy\Events\DatabaseSeeded;
use Stancl\Tenancy\Events\SeedingDatabase;
class Seed extends SeedCommand
{
use HasATenantsOption;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Seed tenant database(s).';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(ConnectionResolverInterface $resolver)
{
// See https://github.com/archtechx/tenancy/issues/1474
if (version_compare(app()->version(), '13.24.0', '>=')) {
$this->signature = 'tenants:seed
{class? : The class name of the root seeder}
{--class=Database\\Seeders\\DatabaseSeeder : The class name of the root seeder}
{--database= : The database connection to seed}
{--force : Force the operation to run when in production}';
parent::__construct($resolver);
$this->specifyParameters();
} else {
$this->name = 'tenants:seed';
parent::__construct($resolver);
}
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
foreach (config('tenancy.seeder_parameters') as $parameter => $value) {
if (! $this->input->hasParameterOption($parameter)) {
$this->input->setOption(ltrim($parameter, '-'), $value);
}
}
if (! $this->confirmToProceed()) {
return;
}
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
$this->line("Tenant: {$tenant->getTenantKey()}");
event(new SeedingDatabase($tenant));
// Seed
parent::handle();
event(new DatabaseSeeded($tenant));
});
}
}