1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 13:34:04 +00:00
tenancy/src/Commands/Seed.php
Samuel Štancl a3f37b7f77 Fix #71
2019-07-25 23:26:36 +02:00

64 lines
1.4 KiB
PHP

<?php
namespace Stancl\Tenancy\Commands;
use Stancl\Tenancy\DatabaseManager;
use Stancl\Tenancy\Traits\HasATenantsOption;
use Illuminate\Database\Console\Seeds\SeedCommand;
use Illuminate\Database\ConnectionResolverInterface;
class Seed extends SeedCommand
{
use HasATenantsOption;
protected $database;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Seed tenant database(s).';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(ConnectionResolverInterface $resolver, DatabaseManager $database)
{
parent::__construct($resolver);
$this->database = $database;
$this->setName('tenants:seed');
$this->specifyParameters();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (! $this->confirmToProceed()) {
return;
}
$this->input->setOption('database', 'tenant');
tenant()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})");
$this->database->connectToTenant($tenant);
// Seed
parent::handle();
});
if (tenancy()->initialized) {
tenancy()->switchDatabaseConnection();
} else {
$this->database->disconnect();
}
}
}