1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 17:54:03 +00:00
tenancy/src/Bootstrappers/RootUrlBootstrapper.php
Samuel Štancl 657e165cc8
[4.x] Cleanup (#1317)
* cleanup, resolve todos, add immediate todos

* Improve path_identification_middleware docblock

* rename leave() method in tests

* wip fix hardcoded values making assumptions about the parameters used in routing

* defaultParameterNames

* fix CreatesDatabaseUsers return values

* $tenant -> tenant()

* resolve more todos

* make comment block a complete block

* Correct useTenantRoutesInFortify(), delete unused import

* test fixes

* remove todos

* remove JobPipeline todo

* simplify comment example

* remove todo

* fix VERSION_PREFIX in queue.yml

---------

Co-authored-by: lukinovec <lukinovec@gmail.com>
2025-02-20 20:49:09 +01:00

78 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Closure;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Foundation\Application;
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;
/**
* Overriding the root url may cause issues in *some* tests, so you can disable
* the behavior by setting this property to false.
*/
public static bool $rootUrlOverrideInTests = true;
public function __construct(
protected Repository $config,
protected Application $app,
) {}
public function bootstrap(Tenant $tenant): void
{
if (static::$rootUrlOverride === null) {
return;
}
if (! $this->app->runningInConsole()) {
return;
}
if ($this->app->runningUnitTests() && ! static::$rootUrlOverrideInTests) {
return;
}
$this->originalRootUrl = $this->app['url']->to('/');
$newRootUrl = (static::$rootUrlOverride)($tenant, $this->originalRootUrl);
$this->app['url']->forceRootUrl($newRootUrl);
$this->config->set('app.url', $newRootUrl);
}
public function revert(): void
{
if ($this->originalRootUrl) {
$this->app['url']->forceRootUrl($this->originalRootUrl);
$this->config->set('app.url', $this->originalRootUrl);
}
}
}