Skip to content

Commit

Permalink
added some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyzhu committed Dec 28, 2020
1 parent b66ad6f commit e12511f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,10 @@ $route = Router::getRoute('users.comments.index');
#### 通过当前请求获取当前路由

```php
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\Utils\ApplicationContext;
use Hyperf\HttpServer\Router\Router;

/** @var \HyperfExt\HttpServer\Router\Route $route */
$route = ApplicationContext::getContainer()
->get(RequestInterface::class)
->getAttribute(Dispatched::class)
->handler
->routeInstance; // 为避免过多修改原始组件,该组件将路由实例放到了 Handler 中
$route = Router::getCurrentRoute();
```

### 生成指定路由的 URI
Expand Down
11 changes: 10 additions & 1 deletion src/DispatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@
use FastRoute\DataGenerator\GroupCountBased as DataGenerator;
use FastRoute\RouteParser\Std;
use Hyperf\HttpServer\Router\DispatcherFactory as HyperfDispatcherFactory;
use Psr\Container\ContainerInterface;

class DispatcherFactory extends HyperfDispatcherFactory
{
protected ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
parent::__construct();
$this->container = $container;
}

public function getRouter(string $serverName): RouteCollector
{
if (isset($this->routers[$serverName])) {
Expand All @@ -24,6 +33,6 @@ public function getRouter(string $serverName): RouteCollector

$parser = new Std();
$generator = new DataGenerator();
return $this->routers[$serverName] = new RouteCollector($parser, $generator, $serverName);
return $this->routers[$serverName] = new RouteCollector($this->container, $parser, $generator, $serverName);
}
}
42 changes: 41 additions & 1 deletion src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,51 @@ class Route

public ?string $name;

public function __construct(string $rule, array $data, ?string $name = null)
private array $defaults;

public function __construct(string $rule, array $data, ?string $name = null, array $defaults = [])
{
$this->rule = $rule;
$this->data = $data;
$this->name = empty($name) ? null : $name;
$this->defaults = $defaults;
}

public function getRule(): string
{
return $this->rule;
}

public function getData(): array
{
return $this->data;
}

public function getName(): ?string
{
return $this->name;
}

public function setDefault(string $key, $value): self
{
$this->defaults[$key] = $value;
return $this;
}

public function getDefault(string $key, $default = null)
{
return Arr::get($this->defaults, $key, $default);
}

public function setDefaults(array $defaults): self
{
$this->defaults = $defaults;
return $this;
}

public function getDefaults(): array
{
return $this->defaults;
}

public function createUri(array $parameters = []): Uri
Expand Down
22 changes: 22 additions & 0 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@
*/
namespace HyperfExt\HttpServer\Router;

use FastRoute\DataGenerator;
use FastRoute\RouteParser;
use Hyperf\HttpServer\MiddlewareManager;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\HttpServer\Router\RouteCollector as BaseRouteCollector;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;

class RouteCollector extends BaseRouteCollector
{
protected ContainerInterface $container;

protected string $currentGroupName = '';

/**
* @var \HyperfExt\HttpServer\Router\Route[]
*/
protected array $namedRoutes = [];

public function __construct(ContainerInterface $container, RouteParser $routeParser, DataGenerator $dataGenerator, string $server = 'http')
{
parent::__construct($routeParser, $dataGenerator, $server);
$this->container = $container;
}

public function addRoute($httpMethod, string $route, $handler, array $options = []): Route
{
if (isset($options['name'])) {
Expand Down Expand Up @@ -116,4 +129,13 @@ public function getRoute(string $name): Route

throw new RouteNotFoundException("Route [{$name}] not defined.");
}

/**
* Get a route instance by current request.
*/
public function getCurrentRoute(): ?Route
{
$dispatched = $this->container->get(ServerRequestInterface::class)->getAttribute(Dispatched::class);
return $dispatched ? $dispatched->handler->routeInstance : null;
}
}

0 comments on commit e12511f

Please sign in to comment.