mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
Improve file structure (#5)
* Add Enums and Overrides folders * Fix code style (php-cs-fixer) --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
parent
1d0ca27bc8
commit
e3b59ae2b5
26 changed files with 33 additions and 32 deletions
|
|
@ -1,118 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy; // todo new Overrides namespace?
|
||||
|
||||
use Illuminate\Routing\UrlGenerator;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
/**
|
||||
* This class is used in place of the default UrlGenerator when UrlBindingBootstrapper is enabled.
|
||||
*
|
||||
* TenancyUrlGenerator does two extra things:
|
||||
* 1. Autofill the {tenant} parameter in the tenant context with the current tenant if $passTenantParameterToRoutes is enabled (enabled by default)
|
||||
* 2. Prepend the route name with `tenant.` (or the configured prefix) if $prefixRouteNames is enabled (disabled by default)
|
||||
*
|
||||
* Both of these can be skipped by passing the $bypassParameter (`['central' => true]` by default)
|
||||
*/
|
||||
class TenancyUrlGenerator extends UrlGenerator
|
||||
{
|
||||
/**
|
||||
* Parameter which bypasses the behavior modification of route() and temporarySignedRoute().
|
||||
*
|
||||
* E.g. route('tenant') => app.test/{tenant}/tenant (or app.test/tenant?tenant=tenantKey if the route doesn't accept the tenant parameter)
|
||||
* route('tenant', [$bypassParameter => true]) => app.test/tenant.
|
||||
*/
|
||||
public static string $bypassParameter = 'central';
|
||||
|
||||
/**
|
||||
* Determine if the route names of routes generated using
|
||||
* `route()` or `temporarySignedRoute()` should get prefixed with the tenant route name prefix.
|
||||
*
|
||||
* Set this to true when using path identification.
|
||||
*/
|
||||
public static bool $prefixRouteNames = false;
|
||||
|
||||
/**
|
||||
* Determine if the tenant parameter should get passed
|
||||
* to the links generated by `route()` or `temporarySignedRoute()`.
|
||||
*/
|
||||
public static bool $passTenantParameterToRoutes = true;
|
||||
|
||||
/**
|
||||
* Override the route() method so that the route name gets prefixed
|
||||
* and the tenant parameter gets added when in tenant context.
|
||||
*/
|
||||
public function route($name, $parameters = [], $absolute = true)
|
||||
{
|
||||
[$name, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters));
|
||||
|
||||
return parent::route($name, $parameters, $absolute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the temporarySignedRoute() method so that the route name gets prefixed
|
||||
* and the tenant parameter gets added when in tenant context.
|
||||
*/
|
||||
public function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true)
|
||||
{
|
||||
[$name, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters));
|
||||
|
||||
return parent::temporarySignedRoute($name, $expiration, $parameters, $absolute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return bool indicating if the bypass parameter was in $parameters.
|
||||
*/
|
||||
protected function routeBehaviorModificationBypassed(mixed $parameters): bool
|
||||
{
|
||||
if (isset($parameters[static::$bypassParameter])) {
|
||||
return (bool) $parameters[static::$bypassParameter];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a route name and an array of parameters to return the prefixed route name
|
||||
* and the route parameters with the tenant parameter added.
|
||||
*
|
||||
* To skip these modifications, pass the bypass parameter in route parameters.
|
||||
* Before returning the modified route inputs, the bypass parameter is removed from the parameters.
|
||||
*/
|
||||
protected function prepareRouteInputs(string $name, array $parameters): array
|
||||
{
|
||||
if (! $this->routeBehaviorModificationBypassed($parameters)) {
|
||||
$name = $this->prefixRouteName($name);
|
||||
$parameters = $this->addTenantParameter($parameters);
|
||||
}
|
||||
|
||||
// Remove bypass parameter from the route parameters
|
||||
unset($parameters[static::$bypassParameter]);
|
||||
|
||||
return [$name, $parameters];
|
||||
}
|
||||
|
||||
/**
|
||||
* If $prefixRouteNames is true, prefix the passed route name.
|
||||
*/
|
||||
protected function prefixRouteName(string $name): string
|
||||
{
|
||||
$tenantPrefix = PathIdentificationManager::getTenantRouteNamePrefix();
|
||||
|
||||
if (static::$prefixRouteNames && ! str($name)->startsWith($tenantPrefix)) {
|
||||
$name = str($name)->after($tenantPrefix)->prepend($tenantPrefix)->toString();
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* If `tenant()` isn't null, add tenant paramter to the passed parameters.
|
||||
*/
|
||||
protected function addTenantParameter(array $parameters): array
|
||||
{
|
||||
return tenant() && static::$passTenantParameterToRoutes ? array_merge($parameters, [PathIdentificationManager::getTenantParameterName() => tenant()->getTenantKey()]) : $parameters;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue