mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 08:14:02 +00:00
Add UrlTenancyBootstrapper
This commit is contained in:
parent
21d55ef472
commit
1b00f5f1ae
3 changed files with 49 additions and 1 deletions
|
|
@ -118,7 +118,20 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
|
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
//
|
/**
|
||||||
|
* Example of CLI tenant URL root override:
|
||||||
|
*
|
||||||
|
* UrlTenancyBootstrapper::$rootUrlOverride = function (Tenant $tenant) {
|
||||||
|
* $baseUrl = URL::to('/');
|
||||||
|
* $scheme = str($baseUrl)->before('://') . '://';
|
||||||
|
*
|
||||||
|
* return str($baseUrl)
|
||||||
|
* ->after($scheme)
|
||||||
|
* ->prepend($tenant->getTenantKey() . '.')
|
||||||
|
* ->prepend($scheme)
|
||||||
|
* ->toString();
|
||||||
|
*};
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public function boot()
|
public function boot()
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,7 @@ return [
|
||||||
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
|
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
|
||||||
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
|
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
|
||||||
Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
|
Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
|
||||||
|
// Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper::class,
|
||||||
// Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper::class, // Queueing mail requires using QueueTenancyBootstrapper with $forceRefresh set to true
|
// Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper::class, // Queueing mail requires using QueueTenancyBootstrapper with $forceRefresh set to true
|
||||||
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
|
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
|
||||||
],
|
],
|
||||||
|
|
|
||||||
34
src/Bootstrappers/UrlTenancyBootstrapper.php
Normal file
34
src/Bootstrappers/UrlTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Bootstrappers;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
use Illuminate\Contracts\Routing\UrlGenerator;
|
||||||
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
|
|
||||||
|
class UrlTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
{
|
||||||
|
public static Closure|null $rootUrlOverride = null;
|
||||||
|
protected string|null $originalRootUrl = null;
|
||||||
|
|
||||||
|
public function __construct(protected UrlGenerator $urlGenerator)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function bootstrap(Tenant $tenant): void
|
||||||
|
{
|
||||||
|
$this->originalRootUrl = $this->urlGenerator->to('/');
|
||||||
|
|
||||||
|
if (static::$rootUrlOverride) {
|
||||||
|
$this->urlGenerator->forceRootUrl((static::$rootUrlOverride)($tenant));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function revert(): void
|
||||||
|
{
|
||||||
|
$this->urlGenerator->forceRootUrl($this->originalRootUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue