mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 19:24:03 +00:00
* Resolve delete tenant storage todo * Delete outdated todo (resolved in #25) * Delete resource syncing todo (resolved in #11) * Make it clear that getArgsForTenant() is used during cache invalidation * Delete redundant __call() and __callStatic() annotations * Fix code style (php-cs-fixer) * Revert %tenant_id% to-do removal * Test all cached resolvers instead of just the domain one * Make docblock more concise, delete renaming to-do (the name seems fine) * Fix method in tests * If route is the only resolver arg, use the tenant as the cache key instead of encoded route instance * Resolve to-do * make docblock more clear * Add comments to getResolverArgument() * Rename $id to $tenantKey * Fix code style (php-cs-fixer) * Add regression test for forgetting tenant parameters of cached tenants * Forget route parameter when tenant gets resolved * Add parameter type * Simplify getCacheKey() * Resolvers wip * Resolvers wip * Fix code style (php-cs-fixer) * Bring back the route instance check to getCacheKey, fix test * add todo * add assertion --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> Co-authored-by: Samuel Štancl <samuel@archte.ch>
70 lines
2.3 KiB
PHP
70 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Resolvers;
|
|
|
|
use Illuminate\Routing\Route;
|
|
use Stancl\Tenancy\Contracts\Tenant;
|
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
|
|
use Stancl\Tenancy\PathIdentificationManager;
|
|
|
|
class PathTenantResolver extends Contracts\CachedTenantResolver
|
|
{
|
|
public function resolveWithoutCache(mixed ...$args): Tenant
|
|
{
|
|
/** @var Route $route */
|
|
$route = $args[0];
|
|
|
|
/** @var string $id */
|
|
$id = $route->parameter(static::tenantParameterName());
|
|
|
|
if ($id) {
|
|
// Forget the tenant parameter so that we don't have to accept it in route action methods
|
|
$route->forgetParameter(static::tenantParameterName());
|
|
|
|
if ($tenant = tenancy()->find($id)) {
|
|
return $tenant;
|
|
}
|
|
}
|
|
|
|
throw new TenantCouldNotBeIdentifiedByPathException($id);
|
|
}
|
|
|
|
public function getArgsForTenant(Tenant $tenant): array
|
|
{
|
|
return [
|
|
[$tenant->getTenantKey()],
|
|
];
|
|
}
|
|
|
|
public function resolved(Tenant $tenant, mixed ...$args): void
|
|
{
|
|
/** @var Route $route */
|
|
$route = $args[0];
|
|
|
|
$route->forgetParameter(PathIdentificationManager::getTenantParameterName());
|
|
}
|
|
|
|
public function getCacheKey(mixed ...$args): string
|
|
{
|
|
// todo@samuel: fix the coupling here. when this is called from the cachedresolver, $args are the tenant key. when it's called from within this class, $args are a Route instance
|
|
// the logic shouldn't have to be coupled to where it's being called from
|
|
|
|
// $args[0] can be either a Route instance with the tenant key as a parameter
|
|
// Or the tenant key
|
|
$args = [$args[0] instanceof Route ? $args[0]->parameter(static::tenantParameterName()) : $args[0]];
|
|
|
|
return '_tenancy_resolver:' . static::class . ':' . json_encode($args);
|
|
}
|
|
|
|
public static function tenantParameterName(): string
|
|
{
|
|
return config('tenancy.identification.resolvers.' . static::class . '.tenant_parameter_name') ?? 'tenant';
|
|
}
|
|
|
|
public static function tenantRouteNamePrefix(): string
|
|
{
|
|
return config('tenancy.identification.resolvers.' . static::class . '.tenant_route_name_prefix') ?? static::tenantParameterName() . '.';
|
|
}
|
|
}
|