1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 15:04:04 +00:00

Remove obsolete files, restructure

This commit is contained in:
Samuel Štancl 2020-05-21 15:54:35 +02:00
parent fbe43fbb04
commit 4f8d892481
20 changed files with 19 additions and 83 deletions

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Concerns;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
trait CreatesDatabaseUsers
{
public function createDatabase(TenantWithDatabase $tenant): bool
{
return $this->database()->transaction(function () use ($tenant) {
parent::createDatabase($tenant);
return $this->createUser($tenant->database());
});
}
public function deleteDatabase(TenantWithDatabase $tenant): bool
{
return $this->database()->transaction(function () use ($tenant) {
parent::deleteDatabase($tenant);
return $this->deleteUser($tenant->database());
});
}
}

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Concerns;
trait DealsWithMigrations
{
protected function getMigrationPaths()
{
if ($this->input->hasOption('path') && $this->input->getOption('path')) {
return parent::getMigrationPaths();
}
return database_path('migrations/tenant');
}
}

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Concerns;
use Symfony\Component\Console\Input\InputArgument;
trait HasATenantArgument
{
protected function getArguments()
{
return array_merge([
['tenant', InputArgument::REQUIRED, 'Tenant id', null],
], parent::getArguments());
}
protected function getTenants(): array
{
return [tenancy()->find($this->argument('tenant'))];
}
public function __construct()
{
parent::__construct();
$this->specifyParameters();
}
}

View file

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Concerns;
use Illuminate\Support\LazyCollection;
use Symfony\Component\Console\Input\InputOption;
trait HasATenantsOption
{
protected function getOptions()
{
return array_merge([
['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null],
], parent::getOptions());
}
protected function getTenants(): LazyCollection
{
return tenancy()
->query()
->when($this->option('tenants'), function ($query) {
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
})
->cursor();
}
public function __construct()
{
parent::__construct();
$this->specifyParameters();
}
}

View file

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Concerns;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Stancl\Tenancy\Contracts\Tenant;
trait TenantAwareCommand
{
/** @return int */
protected function execute(InputInterface $input, OutputInterface $output)
{
$tenants = $this->getTenants();
$exitCode = 0;
foreach ($tenants as $tenant) {
$result = (int) $tenant->run(function () {
return $this->laravel->call([$this, 'handle']);
});
if ($result !== 0) {
$exitCode = $result;
}
}
return $exitCode;
}
/**
* Get an array of tenants for which the command should be executed.
*
* @return Tenant[]|mixed
*/
abstract protected function getTenants();
}