mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 17:24:03 +00:00
[4.x] Add tenant parameter to defaults() in UrlGeneratorBootstrapper (#1311)
* Pass tenant parameter using defaults in UrlGeneratorBootstrapper, update tests accordingly (wip) * Fix code style (php-cs-fixer) * Update bootstrapper * Improve TenancyUrlGenerator docblocks * Improve bootstrapper/TenancyUrlGenerator tests (WIP) * Improve route() name prefixing test * Keep `UrlGeneratorBootstrapper::$addTenantParameterToDefaults` disabled by default * Add `$override` functionality to TenancyUrlGenerator * Test $override functionality, update new defaults in the bootstrapper tests * Fix code style (php-cs-fixer) * Update comments * Update routeNameOverride() * cleanup --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
parent
fffaf7c58c
commit
cecf07a8c9
3 changed files with 170 additions and 89 deletions
|
|
@ -10,6 +10,7 @@ use Illuminate\Support\Facades\URL;
|
|||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
|
||||
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||||
|
||||
/**
|
||||
* Makes the app use TenancyUrlGenerator (instead of Illuminate\Routing\UrlGenerator) which:
|
||||
|
|
@ -19,10 +20,20 @@ use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
|
|||
* Used with path and query string identification.
|
||||
*
|
||||
* @see TenancyUrlGenerator
|
||||
* @see \Stancl\Tenancy\Resolvers\PathTenantResolver
|
||||
* @see PathTenantResolver
|
||||
*/
|
||||
class UrlGeneratorBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/**
|
||||
* Should the tenant route parameter get added to TenancyUrlGenerator::defaults().
|
||||
*
|
||||
* This is recommended when using path identification since defaults() generally has better support in integrations,
|
||||
* namely Ziggy, compared to TenancyUrlGenerator::$passTenantParameterToRoutes.
|
||||
*
|
||||
* With query string identification, this has no effect since URL::defaults() only works for route paramaters.
|
||||
*/
|
||||
public static bool $addTenantParameterToDefaults = true;
|
||||
|
||||
public function __construct(
|
||||
protected Application $app,
|
||||
protected UrlGenerator $originalUrlGenerator,
|
||||
|
|
@ -32,7 +43,7 @@ class UrlGeneratorBootstrapper implements TenancyBootstrapper
|
|||
{
|
||||
URL::clearResolvedInstances();
|
||||
|
||||
$this->useTenancyUrlGenerator();
|
||||
$this->useTenancyUrlGenerator($tenant);
|
||||
}
|
||||
|
||||
public function revert(): void
|
||||
|
|
@ -45,7 +56,7 @@ class UrlGeneratorBootstrapper implements TenancyBootstrapper
|
|||
*
|
||||
* @see \Illuminate\Routing\RoutingServiceProvider registerUrlGenerator()
|
||||
*/
|
||||
protected function useTenancyUrlGenerator(): void
|
||||
protected function useTenancyUrlGenerator(Tenant $tenant): void
|
||||
{
|
||||
$newGenerator = new TenancyUrlGenerator(
|
||||
$this->app['router']->getRoutes(),
|
||||
|
|
@ -53,7 +64,16 @@ class UrlGeneratorBootstrapper implements TenancyBootstrapper
|
|||
$this->app['config']->get('app.asset_url'),
|
||||
);
|
||||
|
||||
$newGenerator->defaults($this->originalUrlGenerator->getDefaultParameters());
|
||||
$defaultParameters = $this->originalUrlGenerator->getDefaultParameters();
|
||||
|
||||
if (static::$addTenantParameterToDefaults) {
|
||||
$defaultParameters = array_merge(
|
||||
$defaultParameters,
|
||||
[PathTenantResolver::tenantParameterName() => $tenant->getTenantKey()]
|
||||
);
|
||||
}
|
||||
|
||||
$newGenerator->defaults($defaultParameters);
|
||||
|
||||
$newGenerator->setSessionResolver(function () {
|
||||
return $this->app['session'] ?? null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue