Skip to content

Commit

Permalink
feat: finish substitute bindings with url routable
Browse files Browse the repository at this point in the history
  • Loading branch information
storyn26383 committed Oct 19, 2023
1 parent c9a614a commit 98a22bd
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/foundation/src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
namespace SwooleTW\Hyperf\Foundation\Model;

use Hyperf\DbConnection\Model\Model as BaseModel;
use SwooleTW\Hyperf\Router\Contracts\UrlRoutable;

abstract class Model extends BaseModel
abstract class Model extends BaseModel implements UrlRoutable
{
protected ?string $connection = null;

public function resolveRouteBinding($value)
{
return $this->where($this->getRouteKeyName(), $value)->firstOrFail();
}
}
29 changes: 18 additions & 11 deletions src/router/src/Contracts/UrlRoutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@

namespace SwooleTW\Hyperf\Router\Contracts;

use Hyperf\Database\Model\Model;

interface UrlRoutable
{
/**
* Get the value of the model's route key.
*
* @return string
*/
public function getRouteKey(): string;
public function getRouteKey();

// TODO: Route model binding
// /**
// * Get the route key for the model.
// */
// public function getRouteKeyName(): string;
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName();

// TODO: Route model binding
// /**
// * Retrieve the model for a bound value.
// */
// public function resolveRouteBinding(string $value, ?string $field = null): Hyperf\Database\Model\Model;
/**
* Retrieve the model for a bound value.
*
* @param string $value
* @return null|Model
*/
public function resolveRouteBinding($value);
}
18 changes: 18 additions & 0 deletions src/router/src/Exceptions/UrlRoutableNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace SwooleTW\Hyperf\Router\Exceptions;

use RuntimeException;

class UrlRoutableNotFoundException extends RuntimeException
{
/**
* Create a new exception instance.
*/
public function __construct(string $class, string $routeKey)
{
parent::__construct("No query results for url routable [{$class}] {$routeKey}.");
}
}
21 changes: 21 additions & 0 deletions src/router/src/Middleware/SubstituteBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use SwooleTW\Hyperf\Router\Contracts\UrlRoutable;
use SwooleTW\Hyperf\Router\Exceptions\BackedEnumCaseNotFoundException;
use SwooleTW\Hyperf\Router\Exceptions\UrlRoutableNotFoundException;

use function Hyperf\Support\make;

Expand Down Expand Up @@ -109,6 +111,10 @@ protected function resolveBinding(ReflectionType $definition, array $params, str
{
$class = $definition->getName();

if (is_a($class, UrlRoutable::class, true)) {
return $this->resolveUrlRoutable($class, $params[$name]);
}

if (is_a($class, Model::class, true)) {
return $this->resolveModel($class, $params[$name]);
}
Expand All @@ -118,6 +124,21 @@ protected function resolveBinding(ReflectionType $definition, array $params, str
}
}

/**
* @param class-string<UrlRoutable> $class
* @throws ModelNotFoundException
*/
protected function resolveUrlRoutable(string $class, string $routeKey): UrlRoutable
{
$urlRoutable = make($class)->resolveRouteBinding($routeKey);

if (is_null($urlRoutable)) {
throw new UrlRoutableNotFoundException($class, $routeKey);
}

return $urlRoutable;
}

/**
* @param class-string<Model> $class
* @throws ModelNotFoundException
Expand Down
13 changes: 12 additions & 1 deletion tests/Router/Stub/UrlRoutableStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@

namespace SwooleTW\Hyperf\Tests\Router\Stub;

use Hyperf\Database\Model\Model;
use SwooleTW\Hyperf\Router\Contracts\UrlRoutable;

class UrlRoutableStub implements UrlRoutable
{
public function getRouteKey(): string
public function getRouteKey()
{
return '1';
}

public function getRouteKeyName()
{
return 'id';
}

public function resolveRouteBinding($value)
{
return new Model();
}
}

0 comments on commit 98a22bd

Please sign in to comment.