diff --git a/src/Http/Middleware/TenantRoutes.php b/src/Http/Middleware/TenantRoutes.php index d49c652..b538947 100644 --- a/src/Http/Middleware/TenantRoutes.php +++ b/src/Http/Middleware/TenantRoutes.php @@ -11,6 +11,8 @@ use Sprout\Managers\IdentityResolverManager; use Sprout\Managers\TenancyManager; use Sprout\Sprout; +use Sprout\Support\ResolutionHelper; +use Sprout\Support\ResolutionHook; /** * Tenant Routes Middleware @@ -51,17 +53,12 @@ public function handle(Request $request, Closure $next, string ...$options): Res $resolverName = $tenancyName = null; } - $resolver = $this->sprout->resolvers()->get($resolverName); - $tenancy = $this->sprout->tenancies()->get($tenancyName); - - /** - * @var \Sprout\Contracts\IdentityResolver $resolver - * @var \Sprout\Contracts\Tenancy<\Sprout\Contracts\Tenant> $tenancy - */ - - if (! $tenancy->check()) { - throw NoTenantFound::make($resolver->getName(), $tenancy->getName()); - } + ResolutionHelper::handleResolution( + $request, + ResolutionHook::Middleware, + $resolverName, + $tenancyName + ); // TODO: Decide whether to do anything with the following conditions //if (! $tenancy->wasResolved()) { diff --git a/src/Listeners/IdentifyTenantOnRouting.php b/src/Listeners/IdentifyTenantOnRouting.php index 546ae28..653d33f 100644 --- a/src/Listeners/IdentifyTenantOnRouting.php +++ b/src/Listeners/IdentifyTenantOnRouting.php @@ -7,12 +7,12 @@ use Illuminate\Routing\Route; use Illuminate\Support\Arr; use Illuminate\Support\Str; -use Sprout\Contracts\IdentityResolverUsesParameters; -use Sprout\Exceptions\NoTenantFound; use Sprout\Http\Middleware\TenantRoutes; use Sprout\Managers\IdentityResolverManager; use Sprout\Managers\TenancyManager; use Sprout\Sprout; +use Sprout\Support\ResolutionHelper; +use Sprout\Support\ResolutionHook; final class IdentifyTenantOnRouting { @@ -55,39 +55,13 @@ public function handle(RouteMatched $event): void [$resolverName, $tenancyName] = $options; - $resolver = $this->resolverManager->get($resolverName); - $tenancy = $this->tenancyManager->get($tenancyName); - - $this->sprout->setCurrentTenancy($tenancy); - - /** - * @var \Sprout\Contracts\IdentityResolver $resolver - * @var \Sprout\Contracts\Tenancy<\Sprout\Contracts\Tenant> $tenancy - */ - - // Is the resolver using a parameter, and is the parameter present? - if ( - $resolver instanceof IdentityResolverUsesParameters - && $event->route->hasParameter($resolver->getRouteParameterName($tenancy)) - ) { - // Use the route to resolve the identity from the parameter - $identity = $resolver->resolveFromRoute($event->route, $tenancy, $event->request); - $event->route->forgetParameter($resolver->getRouteParameterName($tenancy)); - } else { - // If we reach here, either the resolver doesn't use parameters, or - // the parameter isn't present in the URL, so we'll default to - // using the request - $identity = $resolver->resolveFromRequest($event->request, $tenancy); - } - - // Make sure the tenancy knows which resolver resolved it - $tenancy->resolvedVia($resolver); - - if ($identity === null || $tenancy->identify($identity) === false) { - throw NoTenantFound::make($resolver->getName(), $tenancy->getName()); - } - - return; + ResolutionHelper::handleResolution( + $event->request, + ResolutionHook::Routing, + $resolverName, + $tenancyName, + false + ); } /** diff --git a/src/Support/ResolutionHelper.php b/src/Support/ResolutionHelper.php new file mode 100644 index 0000000..fc942d2 --- /dev/null +++ b/src/Support/ResolutionHelper.php @@ -0,0 +1,73 @@ +make(Sprout::class); + $resolver = $sprout->resolvers()->get($resolverName); + $tenancy = $sprout->tenancies()->get($tenancyName); + + /** + * @var \Sprout\Contracts\IdentityResolver $resolver + * @var \Sprout\Contracts\Tenancy<\Sprout\Contracts\Tenant> $tenancy + */ + + if ($tenancy->check() || ! $resolver->canResolve($request, $tenancy, $hook)) { + return false; + } + + $sprout->setCurrentTenancy($tenancy); + + /** @var \Illuminate\Routing\Route|null $route */ + $route = $request->route(); + + // Is the resolver using a parameter, and is the parameter present? + if ( + $resolver instanceof IdentityResolverUsesParameters + && $route !== null + && $route->hasParameter($resolver->getRouteParameterName($tenancy)) + ) { + // Use the route to resolve the identity from the parameter + $identity = $resolver->resolveFromRoute($route, $tenancy, $request); + $route->forgetParameter($resolver->getRouteParameterName($tenancy)); + } else { + // If we reach here, either the resolver doesn't use parameters, or + // the parameter isn't present in the URL, so we'll default to + // using the request + $identity = $resolver->resolveFromRequest($request, $tenancy); + } + + // Make sure the tenancy knows which resolver resolved it + $tenancy->resolvedVia($resolver)->resolvedAt($hook); + + if ($identity === null || $tenancy->identify($identity) === false) { + if ($throw) { + throw NoTenantFound::make($resolver->getName(), $tenancy->getName()); + } + + return false; + } + + return true; + } +}