Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Jun 23, 2024
1 parent 5fa2ee9 commit 2ee4008
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
43 changes: 20 additions & 23 deletions docs/guide/en/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Yii File Router

> **Notes:**
>
>- The router can be used along with the [`yiisoft/router`](https://github.com/yiisoft/router) package.
>- Once the router found a matching route, it will interrupt the middleware queue and execute the controller action.
> Note:
> - You can use the router along with the [`yiisoft/router`](https://github.com/yiisoft/router) package.
> - Once the router found a matching route, it will interrupt the middleware queue and execute the controller action.
## General usage

Expand Down Expand Up @@ -35,41 +34,37 @@ return [

### `withBaseControllerDirectory(string $directory): self`

Sets the directory where controllers are located.

By default, it is set to `Controller`.
The method sets the directory where the router locates controllers.
By default, it's `Controller`.

### `withClassPostfix(string $postfix): self`

Sets the postfix for controller class names.

By default, it is set to `Controller`.
The method sets the postfix for controller class names.
By default, it's `Controller`.

### `withNamespace(string $namespace): self`

Sets the namespace for controller classes.

By default, it is set to `App`.
The method sets the namespace for controller classes.
By default, it's `App`.

### `withDefaultControllerName(string $name): self`

Sets the default controller name.

By default, it is set to `Index`.
The method sets default controller name.
By default, it's `Index`.

### `withRoutePrefix(string $prefix): self`

Sets the route prefix.

The method sets the route prefix.
By default, it is empty.

It could be useful if you want to add a prefix to all routes or to separate routes from different [modules](#modularity).
It could be useful if you want to add a prefix to all routes or to separate routes
from different [modules](#modularity).

## Middlewares

`\Yiisoft\FileRouter\FileRouter` supports adding middlewares to the routes.

To add a middleware, add the static property `$middlewares` to the controller class:
To add middleware, add the static property `$middlewares` to the controller class:

```php
class UserController
Expand All @@ -87,7 +82,8 @@ class UserController
}
```

Where `index` is the method name and the value is an array of middleware class names, or middleware definitions.
Where `index` is the method name and the value is an array of middleware class names
or middleware definitions.

Look at all supported middleware definitions formats in
the [Middleware Dispatcher](https://github.com/yiisoft/middleware-dispatcher#general-usage) package.
Expand Down Expand Up @@ -128,7 +124,7 @@ class UserController
}
```

> Note: Once you override the actions map, the router will only look for the actions specified in the map.
> Note: Once you override the action map, the router will only look for the actions specified in the map.
> In the example above, the router will fail to find the `index` / `delete`, etc. actions
### Route collision
Expand Down Expand Up @@ -192,4 +188,5 @@ return [
];
```

As a usual middleware, each router will be executed one by one. The first router that matches the route will be used.
Each router is a middleware executed sequentially.
If the first router finds the match, the second one doesn't run.
8 changes: 4 additions & 4 deletions src/FileRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ public function withRoutePrefix(string $prefix): self

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$possibleEntrypoints = $this->parseRequestPath($request);
$possibleEntryPoints = $this->parseRequestPath($request);

foreach ($possibleEntrypoints as $possibleEntrypoint) {
if (!is_array($possibleEntrypoint)) {
foreach ($possibleEntryPoints as $possibleEntryPoint) {
if (!is_array($possibleEntryPoint)) {
continue;
}
/**
* @psalm-var class-string $controllerClass
* @psalm-var string|null $possibleAction
*/
[$controllerClass, $possibleAction] = $possibleEntrypoint;
[$controllerClass, $possibleAction] = $possibleEntryPoint;
if (!class_exists($controllerClass)) {
continue;
}
Expand Down
19 changes: 7 additions & 12 deletions tests/FileRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\FileRouter\Tests;

use Exception;
use HttpSoft\Message\ServerRequest;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -45,9 +46,6 @@ public function testMiddleware(): void

public function testTrailingSlash(): void
{
/**
* @var FileRouter $router
*/
$router = $this->createRouter();
$router = $router
->withNamespace('Yiisoft\FileRouter\Tests\Support\App1');
Expand All @@ -67,9 +65,6 @@ public function testTrailingSlash(): void
#[DataProvider('dataRouter')]
public function testRouter(string $method, string $uri, string $expectedResponse): void
{
/**
* @var FileRouter $router
*/
$router = $this->createRouter();
$router = $router
->withNamespace('Yiisoft\FileRouter\Tests\Support\App1');
Expand Down Expand Up @@ -122,7 +117,7 @@ public function testUnsupportedMethod(): void
uri: '/',
);

$this->expectException(\Exception::class);
$this->expectException(Exception::class);
$this->expectExceptionMessage('Not implemented from tests.');
$router->process($request, $handler);
}
Expand All @@ -139,7 +134,7 @@ public function testNotImplementedAction(): void
uri: '/',
);

$this->expectException(\Exception::class);
$this->expectException(Exception::class);
$this->expectExceptionMessage('Not implemented from tests.');
$router->process($request, $handler);
}
Expand All @@ -156,7 +151,7 @@ public function testUnknownController(): void
uri: '/test/123',
);

$this->expectException(\Exception::class);
$this->expectException(Exception::class);
$this->expectExceptionMessage('Not implemented from tests.');
$router->process($request, $handler);
}
Expand All @@ -173,7 +168,7 @@ public function testIncorrectUrl(): void
uri: '/test//123///',
);

$this->expectException(\Exception::class);
$this->expectException(Exception::class);
$this->expectExceptionMessage('Not implemented from tests.');
$router->process($request, $handler);
}
Expand Down Expand Up @@ -391,7 +386,7 @@ public function testModularityFastPath(): void
uri: '/module2',
);

$this->expectException(\Exception::class);
$this->expectException(Exception::class);
$this->expectExceptionMessage('Not implemented from tests.');
$router->process($request, $handler);
}
Expand Down Expand Up @@ -427,7 +422,7 @@ private function createExceptionHandler(): RequestHandlerInterface
return new class () implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
throw new \Exception('Not implemented from tests.');
throw new Exception('Not implemented from tests.');
}
};
}
Expand Down

0 comments on commit 2ee4008

Please sign in to comment.