mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:44:03 +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:
parent
6b74589d76
commit
2c0f61585d
2 changed files with 38 additions and 8 deletions
|
|
@ -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
|
protected function overrideUrlInTenantContext(): void
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
@ -149,12 +155,12 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
*
|
*
|
||||||
* $scheme = str($originalRootUrl)->before('://');
|
* $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:
|
* // If you're using domain identification:
|
||||||
* return $scheme . '://' . $tenantDomain . '/';
|
* return $scheme . '://' . $tenantDomain . '/';
|
||||||
|
*
|
||||||
|
* // If you're using subdomain identification:
|
||||||
|
* $originalDomain = str($originalRootUrl)->after($scheme . '://');
|
||||||
|
* return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/';
|
||||||
* };
|
* };
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,25 +6,47 @@ namespace Stancl\Tenancy\Bootstrappers;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Config\Repository;
|
use Illuminate\Config\Repository;
|
||||||
|
use Illuminate\Contracts\Foundation\Application;
|
||||||
use Illuminate\Routing\UrlGenerator;
|
use Illuminate\Routing\UrlGenerator;
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
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
|
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;
|
public static Closure|null $rootUrlOverride = null;
|
||||||
|
|
||||||
protected string|null $originalRootUrl = null;
|
protected string|null $originalRootUrl = null;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected UrlGenerator $urlGenerator,
|
protected UrlGenerator $urlGenerator,
|
||||||
protected Repository $config,
|
protected Repository $config,
|
||||||
|
protected Application $app,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function bootstrap(Tenant $tenant): void
|
public function bootstrap(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->originalRootUrl = $this->urlGenerator->to('/');
|
if ($this->app->runningInConsole() && static::$rootUrlOverride) {
|
||||||
|
$this->originalRootUrl = $this->urlGenerator->to('/');
|
||||||
|
|
||||||
if (static::$rootUrlOverride) {
|
|
||||||
$newRootUrl = (static::$rootUrlOverride)($tenant, $this->originalRootUrl);
|
$newRootUrl = (static::$rootUrlOverride)($tenant, $this->originalRootUrl);
|
||||||
|
|
||||||
$this->urlGenerator->forceRootUrl($newRootUrl);
|
$this->urlGenerator->forceRootUrl($newRootUrl);
|
||||||
|
|
@ -34,7 +56,9 @@ class RootUrlBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
public function revert(): void
|
public function revert(): void
|
||||||
{
|
{
|
||||||
$this->urlGenerator->forceRootUrl($this->originalRootUrl);
|
if ($this->originalRootUrl) {
|
||||||
$this->config->set('app.url', $this->originalRootUrl);
|
$this->urlGenerator->forceRootUrl($this->originalRootUrl);
|
||||||
|
$this->config->set('app.url', $this->originalRootUrl);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue