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

Make RootUrlBootstrapper only have an effect in CLI (#58)

* Interrupt RootUrlBootstrapper's bootstrap() execution if the app isn't running in console, add docblock

* Improve overrideUrlInTenantContext() documentation [ci skip]

* Improve RootUrlBootstrapper [ci skip]
This commit is contained in:
lukinovec 2024-08-28 00:42:36 +02:00 committed by GitHub
parent 6b74589d76
commit 2c0f61585d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 8 deletions

View file

@ -137,6 +137,12 @@ class TenancyServiceProvider extends ServiceProvider
];
}
/**
* Set \Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper::$rootUrlOverride here
* to override the root URL used in CLI while in tenant context.
*
* @see \Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper
*/
protected function overrideUrlInTenantContext(): void
{
/**
@ -149,12 +155,12 @@ class TenancyServiceProvider extends ServiceProvider
*
* $scheme = str($originalRootUrl)->before('://');
*
* // If you're using subdomain identification:
* // $originalDomain = str($originalRootUrl)->after($scheme . '://');
* // return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/';
*
* // If you're using domain identification:
* return $scheme . '://' . $tenantDomain . '/';
*
* // If you're using subdomain identification:
* $originalDomain = str($originalRootUrl)->after($scheme . '://');
* return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/';
* };
*/
}

View file

@ -6,25 +6,47 @@ namespace Stancl\Tenancy\Bootstrappers;
use Closure;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Routing\UrlGenerator;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
/**
* While using CLI, automatically alter the root URL used by the URL generator (affects calls like url('/') and route('foo')).
*
* Example:
* Your app's URL (env('APP_URL') / config('app.url') -- the root URL) is http://localhost,
* you have a tenant with a single subdomain ('acme'),
* and you want to use that domain as the tenant's 'primary' domain.
*
* Using a closure like the one provided in the overrideUrlInTenantContext() method example in TenancyServiceProvider
* as the $rootUrlOverride property of this class, you can make the URL generator use
* http://acme.localhost instead of http://localhost as the root URL during the URL generation while in the tenant's context.
* Meaning, `url('/foo')` (or `URL::to('/foo')`) will return http://acme.localhost/foo.
*/
class RootUrlBootstrapper implements TenancyBootstrapper
{
/**
* A closure that accepts the tenant and the original root URL and returns the new root URL.
* When null, the root URL is not altered in any way.
*
* We recommend setting this property in the TenancyServiceProvider's overrideUrlInTenantContext() method.
*/
public static Closure|null $rootUrlOverride = null;
protected string|null $originalRootUrl = null;
public function __construct(
protected UrlGenerator $urlGenerator,
protected Repository $config,
protected Application $app,
) {}
public function bootstrap(Tenant $tenant): void
{
if ($this->app->runningInConsole() && static::$rootUrlOverride) {
$this->originalRootUrl = $this->urlGenerator->to('/');
if (static::$rootUrlOverride) {
$newRootUrl = (static::$rootUrlOverride)($tenant, $this->originalRootUrl);
$this->urlGenerator->forceRootUrl($newRootUrl);
@ -34,7 +56,9 @@ class RootUrlBootstrapper implements TenancyBootstrapper
public function revert(): void
{
if ($this->originalRootUrl) {
$this->urlGenerator->forceRootUrl($this->originalRootUrl);
$this->config->set('app.url', $this->originalRootUrl);
}
}
}