1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 02:44:02 +00:00

path identification wip

This commit is contained in:
Samuel Štancl 2020-05-09 03:56:41 +02:00
parent 7ad93adde5
commit 5e6d82be57
8 changed files with 215 additions and 3 deletions

View 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 TenantCouldNotBeIdentifiedByPathException extends TenantCouldNotBeIdentifiedException implements ProvidesSolution
{
public function __construct($tenant_id)
{
parent::__construct("Tenant could not be identified on path with tenant_id: $tenant_id");
}
public function getSolution(): Solution
{
return BaseSolution::create('Tenant could not be identified on this path')
->setSolutionDescription('Did you forget to create a tenant for this path?')
->setDocumentationLinks([
'Creating Tenants' => 'https://tenancyforlaravel.com/docs/v2/creating-tenants/', // todo update link for v3
]);
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Stancl\Tenancy\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Stancl\Tenancy\Resolvers\PathTenantResolver;
use Stancl\Tenancy\Tenancy;
class InitializeTenancyByPath
{
/** @var Tenancy */
protected $tenancy;
/** @var PathTenantResolver */
protected $resolver;
public function __construct(Tenancy $tenancy, PathTenantResolver $resolver)
{
$this->tenancy = $tenancy;
$this->resolver = $resolver;
}
public function handle(Request $request, Closure $next)
{
/** @var Route $route */
$route = $request->route();
// Only initialize tenancy if tenant is the first parameter
// We don't want to initialize tenancy if the tenant is
// simply injected into some route controller action.
if ($route->parameterNames()[0] === 'tenant') {
$this->tenancy->initialize(
$this->resolver->resolve($route)
);
}
return $next($request);
}
}

View file

@ -4,14 +4,13 @@ namespace Stancl\Tenancy\Resolvers;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Contracts\TenantResolver;
use Stancl\Tenancy\Database\Models\Domain;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
class DomainTenantResolver implements TenantResolver
{
public function resolve(...$args): Tenant
{
$domain = app(config('tenancy.domain_model'))->where('domain', $args[0])->first();
$domain = config('tenancy.domain_model')::where('domain', $args[0])->first();
if ($domain) {
return $domain->tenant;

View file

@ -0,0 +1,27 @@
<?php
namespace Stancl\Tenancy\Resolvers;
use Illuminate\Routing\Route;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Contracts\TenantResolver;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
class PathTenantResolver implements TenantResolver
{
public function resolve(...$args): Tenant
{
/** @var Route $route */
$route = $args[0];
if ($id = $route->parameter('tenant')) {
$route->forgetParameter('tenant');
if ($tenant = config('tenancy.tenant_model')::find($id)) {
return $tenant;
}
}
throw new TenantCouldNotBeIdentifiedByPathException($id);
}
}

View file

@ -13,15 +13,22 @@ class Tenancy
/** @var callable|null */
public static $getBootstrappers = null;
/** @var bool */
public $initialized = false;
public function initialize(Tenant $tenant): void
{
$this->tenant = $tenant;
$this->initialized = true;
event(new Events\TenancyInitialized($tenant));
}
public function end(): void
{
$this->initialized = false;
event(new Events\TenancyEnded($this->tenant));
$this->tenant = null;

View file

@ -26,7 +26,7 @@ if (! function_exists('tenant')) {
return app(Tenant::class);
}
return optional(app(Tenant::class))->get($key) ?? null;
return optional(app(Tenant::class))->getAttribute($key) ?? null;
}
}