1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 15:54:03 +00:00
This commit is contained in:
Abrar Ahmad 2022-11-25 11:00:22 +05:00
parent a6c0fa21c4
commit c0d0dc99de
10 changed files with 38 additions and 36 deletions

View file

@ -283,9 +283,6 @@ return [
// Stancl\Tenancy\Features\TelescopeTags::class, // Stancl\Tenancy\Features\TelescopeTags::class,
// Stancl\Tenancy\Features\UniversalRoutes::class, // Stancl\Tenancy\Features\UniversalRoutes::class,
// Stancl\Tenancy\Features\TenantConfig::class, // https://tenancyforlaravel.com/docs/v3/features/tenant-config // Stancl\Tenancy\Features\TenantConfig::class, // https://tenancyforlaravel.com/docs/v3/features/tenant-config
],
'tenant_unaware_features' => [
// Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancyforlaravel.com/docs/v3/features/cross-domain-redirect // Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancyforlaravel.com/docs/v3/features/cross-domain-redirect
], ],

View file

@ -4,10 +4,8 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Contracts; namespace Stancl\Tenancy\Contracts;
use Stancl\Tenancy\Tenancy;
/** Additional features, like Telescope tags and tenant redirects. */ /** Additional features, like Telescope tags and tenant redirects. */
interface Feature interface Feature
{ {
public function bootstrap(Tenancy $tenancy): void; public function bootstrap(): void;
} }

View file

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Contracts;
interface TenantUnwareFeature
{
public function bootstrap(): void;
}

View file

@ -5,10 +5,16 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Features; namespace Stancl\Tenancy\Features;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Stancl\Tenancy\Contracts\TenantUnwareFeature; use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Tenancy;
class CrossDomainRedirect implements TenantUnwareFeature class CrossDomainRedirect implements Feature
{ {
public function __construct(
protected Tenancy $tenancy
) {
}
public function bootstrap(): void public function bootstrap(): void
{ {
RedirectResponse::macro('domain', function (string $domain) { RedirectResponse::macro('domain', function (string $domain) {

View file

@ -7,11 +7,10 @@ namespace Stancl\Tenancy\Features;
use Laravel\Telescope\IncomingEntry; use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope; use Laravel\Telescope\Telescope;
use Stancl\Tenancy\Contracts\Feature; use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Tenancy;
class TelescopeTags implements Feature class TelescopeTags implements Feature
{ {
public function bootstrap(Tenancy $tenancy): void public function bootstrap(): void
{ {
if (! class_exists(Telescope::class)) { if (! class_exists(Telescope::class)) {
return; return;

View file

@ -12,7 +12,6 @@ use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Events\RevertedToCentralContext; use Stancl\Tenancy\Events\RevertedToCentralContext;
use Stancl\Tenancy\Events\TenancyBootstrapped; use Stancl\Tenancy\Events\TenancyBootstrapped;
use Stancl\Tenancy\Tenancy;
class TenantConfig implements Feature class TenantConfig implements Feature
{ {
@ -28,7 +27,7 @@ class TenantConfig implements Feature
) { ) {
} }
public function bootstrap(Tenancy $tenancy): void public function bootstrap(): void
{ {
Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) { Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) {
/** @var Tenant $tenant */ /** @var Tenant $tenant */

View file

@ -9,7 +9,6 @@ use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Route as Router; use Illuminate\Support\Facades\Route as Router;
use Stancl\Tenancy\Contracts\Feature; use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Middleware; use Stancl\Tenancy\Middleware;
use Stancl\Tenancy\Tenancy;
class UniversalRoutes implements Feature class UniversalRoutes implements Feature
{ {
@ -22,7 +21,7 @@ class UniversalRoutes implements Feature
Middleware\InitializeTenancyBySubdomain::class, Middleware\InitializeTenancyBySubdomain::class,
]; ];
public function bootstrap(Tenancy $tenancy): void public function bootstrap(): void
{ {
foreach (static::$identificationMiddlewares as $middleware) { foreach (static::$identificationMiddlewares as $middleware) {
$originalOnFail = $middleware::$onFail; $originalOnFail = $middleware::$onFail;

View file

@ -16,9 +16,9 @@ class UserImpersonation implements Feature
/** The lifespan of impersonation tokens (in seconds). */ /** The lifespan of impersonation tokens (in seconds). */
public static int $ttl = 60; public static int $ttl = 60;
public function bootstrap(Tenancy $tenancy): void public function bootstrap(): void
{ {
$tenancy->macro('impersonate', function (Tenant $tenant, string $userId, string $redirectUrl, string $authGuard = null): ImpersonationToken { Tenancy::macro('impersonate', function (Tenant $tenant, string $userId, string $redirectUrl, string $authGuard = null): ImpersonationToken {
return ImpersonationToken::create([ return ImpersonationToken::create([
Tenancy::tenantKeyColumn() => $tenant->getTenantKey(), Tenancy::tenantKeyColumn() => $tenant->getTenantKey(),
'user_id' => $userId, 'user_id' => $userId,

View file

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Listeners;
use Illuminate\Contracts\Foundation\Application;
use Stancl\Tenancy\Events\TenancyInitialized;
class BootstrapFeatures
{
public function __construct(
protected Application $app
) {
}
public function handle(TenancyInitialized $event): void
{
foreach ($this->app['config']['tenancy.features'] ?? [] as $feature) {
$this->app[$feature]->bootstrap($event->tenancy);
}
}
}

View file

@ -27,16 +27,7 @@ class TenancyServiceProvider extends ServiceProvider
// Make sure Tenancy is stateful. // Make sure Tenancy is stateful.
$this->app->singleton(Tenancy::class); $this->app->singleton(Tenancy::class);
// Make sure features are bootstrapped as soon as Tenancy is instantiated. foreach ($this->app['config']['tenancy.features'] ?? [] as $feature) {
$this->app->extend(Tenancy::class, function (Tenancy $tenancy) {
foreach ($this->app['config']['tenancy.features'] ?? [] as $feature) {
$this->app[$feature]->bootstrap($tenancy);
}
return $tenancy;
});
foreach ($this->app['config']['tenancy.tenant_unaware_features'] ?? [] as $feature) {
$this->app[$feature]->bootstrap(); $this->app[$feature]->bootstrap();
} }