Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 28 additions & 38 deletions src/router/src/Middleware/SubstituteBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use Closure;
use Hyperf\Database\Model\Model;
use Hyperf\Database\Model\ModelNotFoundException;
use Hyperf\Di\ClosureDefinitionCollectorInterface;
use Hyperf\Di\MethodDefinitionCollectorInterface;
use Hyperf\Di\ReflectionType;
use Hyperf\HttpServer\Router\Dispatched;
use Hypervel\Http\RouteDependency;
use Hypervel\Router\Contracts\UrlRoutable;
use Hypervel\Router\Exceptions\BackedEnumCaseNotFoundException;
use Hypervel\Router\Exceptions\UrlRoutableNotFoundException;
use Hypervel\Router\Router;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -25,8 +25,16 @@

class SubstituteBindings implements MiddlewareInterface
{
public function __construct(protected ContainerInterface $container)
{
/**
* All of the resolved url routables.
*/
protected array $resolvedUrlRoutables = [];

public function __construct(
protected ContainerInterface $container,
protected RouteDependency $routeDependency,
protected Router $router,
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Expand All @@ -38,13 +46,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $handler->handle($request);
}

if (strpos($dispatched->handler->route, '{') === false) {
if (! $params = $dispatched->params) {
return $handler->handle($request);
}

$definitions = $this->getDefinitions($dispatched->handler->callback);
$params = $dispatched->params;

$dispatched->params = $this->substituteBindings($definitions, $params);

return $handler->handle($request);
Expand All @@ -56,37 +62,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
protected function getDefinitions(array|Closure|string $callback): array
{
if ($callback instanceof Closure) {
return $this->getClosureDefinitions($callback);
return $this->routeDependency->getClosureDefinitions($callback);
}

if (is_string($callback)) {
$callback = explode('@', $callback);
}

return $this->getMethodDefinitions($callback);
}

/**
* @return ReflectionType[]
*/
protected function getClosureDefinitions(Closure $callback): array
{
if (! $this->container->has(ClosureDefinitionCollectorInterface::class)) {
return [];
}

return $this->container->get(ClosureDefinitionCollectorInterface::class)->getParameters($callback);
}

/**
* @return ReflectionType[]
*/
protected function getMethodDefinitions(array $callback): array
{
$controller = $callback[0];
$action = $callback[1];

return $this->container->get(MethodDefinitionCollectorInterface::class)->getParameters($controller, $action);
return $this->routeDependency->getMethodDefinitions($callback[0], $callback[1]);
}

/**
Expand All @@ -100,7 +83,10 @@ protected function substituteBindings(array $definitions, array $params): array
if (! array_key_exists($name, $params)) {
continue;
}

if ($binding = $this->router->getExplicitBinding($name)) {
$params[$name] = $binding($params[$name]);
continue;
}
if ($binding = $this->resolveBinding($definition, $params, $name)) {
$params[$name] = $binding;
}
Expand All @@ -113,9 +99,10 @@ protected function substituteBindings(array $definitions, array $params): array
* @throws ModelNotFoundException
* @throws BackedEnumCaseNotFoundException
*/
protected function resolveBinding(ReflectionType $definition, array $params, string $name)
protected function resolveBinding(ReflectionType $definition, array $params, string $name): mixed
{
$class = $definition->getName();
$class = $this->router->getModelBinding($name)
?: $definition->getName();

if (is_a($class, UrlRoutable::class, true)) {
return $this->resolveUrlRoutable($class, $params[$name]);
Expand All @@ -128,6 +115,8 @@ protected function resolveBinding(ReflectionType $definition, array $params, str
if (is_a($class, BackedEnum::class, true)) {
return $this->resolveBackedEnum($class, $params[$name]);
}

return null;
}

/**
Expand All @@ -136,14 +125,15 @@ protected function resolveBinding(ReflectionType $definition, array $params, str
*/
protected function resolveUrlRoutable(string $class, string $routeKey): UrlRoutable
{
$urlRoutable = make($class)->resolveRouteBinding($routeKey);
$urlRoutable = $this->resolvedUrlRoutables[$class]
?? $this->resolvedUrlRoutables[$class] = make($class);

if (is_null($urlRoutable)) {
if (! $result = $urlRoutable->resolveRouteBinding($routeKey)) {
throw new UrlRoutableNotFoundException($class, $routeKey);
}

/* @phpstan-ignore-next-line */
return $urlRoutable;
return $result;
}

/**
Expand Down
46 changes: 45 additions & 1 deletion src/router/src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Closure;
use Hyperf\Context\ApplicationContext;
use Hyperf\Database\Model\Model;
use Hyperf\HttpServer\Router\DispatcherFactory;
use Hyperf\HttpServer\Router\RouteCollector;
use RuntimeException;

/**
Expand All @@ -16,6 +18,20 @@ class Router
{
protected string $serverName = 'http';

/**
* Customized route parameters for model bindings.
*
* @var array<string, class-string>
*/
protected array $modelBindings = [];

/**
* Customized route parameters for explicit bindings.
*
* @var array<string, Closure>
*/
protected array $explicitBindings = [];

public function __construct(protected DispatcherFactory $dispatcherFactory)
{
}
Expand Down Expand Up @@ -59,12 +75,40 @@ protected function registerRouteFile(string $routeFile): Closure
return fn () => require $routeFile;
}

public function getRouter()
public function getRouter(): RouteCollector
{
return $this->dispatcherFactory
->getRouter($this->serverName);
}

public function model(string $param, string $modelClass): void
{
if (! class_exists($modelClass)) {
throw new RuntimeException("Model class `{$modelClass}` does not exist.");
}

if (! is_subclass_of($modelClass, Model::class)) {
throw new RuntimeException("Model class `{$modelClass}` must be a subclass of `Model`.");
}

$this->modelBindings[$param] = $modelClass;
}

public function bind(string $param, Closure $callback): void
{
$this->explicitBindings[$param] = $callback;
}

public function getModelBinding(string $param): ?string
{
return $this->modelBindings[$param] ?? null;
}

public function getExplicitBinding(string $param): ?Closure
{
return $this->explicitBindings[$param] ?? null;
}

public static function __callStatic(string $name, array $arguments)
{
return ApplicationContext::getContainer()
Expand Down