mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 13:54:03 +00:00
Allows tenants to be initalized with their IDs (#473)
* Allows tenants to be initalized with their IDs * Code style, note Co-authored-by: Samuel Stancl <samuel.stancl@gmail.com>
This commit is contained in:
parent
99dec30512
commit
b77bf9f179
2 changed files with 46 additions and 2 deletions
27
src/Exceptions/TenantCountNotBeIdentifiedById.php
Normal file
27
src/Exceptions/TenantCountNotBeIdentifiedById.php
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Exceptions;
|
||||||
|
|
||||||
|
use Facade\IgnitionContracts\BaseSolution;
|
||||||
|
use Facade\IgnitionContracts\ProvidesSolution;
|
||||||
|
use Facade\IgnitionContracts\Solution;
|
||||||
|
use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException;
|
||||||
|
|
||||||
|
class TenantCountNotBeIdentifiedById extends TenantCouldNotBeIdentifiedException implements ProvidesSolution
|
||||||
|
{
|
||||||
|
public function __construct($tenant_id)
|
||||||
|
{
|
||||||
|
parent::__construct("Tenant could not be identified with tenant_id: $tenant_id");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSolution(): Solution
|
||||||
|
{
|
||||||
|
return BaseSolution::create('Tenant could not be identified with that ID')
|
||||||
|
->setSolutionDescription('Are you sure the ID is correct and the tenant exists?')
|
||||||
|
->setDocumentationLinks([
|
||||||
|
'Initializing Tenants' => 'https://tenancyforlaravel.com/docs/v3/tenants',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Traits\Macroable;
|
use Illuminate\Support\Traits\Macroable;
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Exceptions\TenantCountNotBeIdentifiedById;
|
||||||
|
|
||||||
class Tenancy
|
class Tenancy
|
||||||
{
|
{
|
||||||
|
|
@ -23,17 +23,34 @@ class Tenancy
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public $initialized = false;
|
public $initialized = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the tenant
|
||||||
|
* @param Tenant|int|string $tenant
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
public function initialize(Tenant $tenant): void
|
public function initialize(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
if ($this->initialized && $this->tenant->getTenantKey() === $tenant->getTenantKey()) {
|
if ($this->initialized && $this->tenant->getTenantKey() === $tenant->getTenantKey()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Remove this (so that runForMultiple() is still performant) and make the FS bootstrapper work either way
|
||||||
if ($this->initialized) {
|
if ($this->initialized) {
|
||||||
$this->end();
|
$this->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->tenant = $tenant;
|
if (is_object($tenant)) {
|
||||||
|
$this->tenant = $tenant;
|
||||||
|
} else {
|
||||||
|
$tenantId = $tenant;
|
||||||
|
$tenant = $this->find($tenantId);
|
||||||
|
|
||||||
|
if (! $tenant) {
|
||||||
|
throw new TenantCountNotBeIdentifiedById($tenantId);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->tenant = $tenant;
|
||||||
|
}
|
||||||
|
|
||||||
event(new Events\InitializingTenancy($this));
|
event(new Events\InitializingTenancy($this));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue