Skip to content

Commit 291a7c5

Browse files
authored
Refactor routes (#236)
1 parent a13153a commit 291a7c5

8 files changed

Lines changed: 56 additions & 75 deletions

File tree

composer-dependency-analyser.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
->addPathToScan($root . '/public/index.php', isDev: false)
1515
->addPathToScan($root . '/yii', isDev: false)
1616
->addPathToScan($root . '/tests', isDev: true)
17-
->ignoreErrorsOnPackage('psr/container', [ErrorType::PROD_DEPENDENCY_ONLY_IN_DEV])
18-
->ignoreErrorsOnPackage('yiisoft/di', [ErrorType::UNUSED_DEPENDENCY])
19-
->ignoreErrorsOnPackage('yiisoft/config', [ErrorType::PROD_DEPENDENCY_ONLY_IN_DEV])
20-
->ignoreErrorsOnPackage('yiisoft/yii-event', [ErrorType::PROD_DEPENDENCY_ONLY_IN_DEV])
21-
->ignoreErrorsOnPackage('yiisoft/router-fastroute', [ErrorType::UNUSED_DEPENDENCY]);
17+
->ignoreErrorsOnPackages(
18+
['psr/container', 'yiisoft/config', 'yiisoft/yii-event', 'yiisoft/yii-event'],
19+
[ErrorType::PROD_DEPENDENCY_ONLY_IN_DEV],
20+
)
21+
->ignoreErrorsOnPackages(
22+
['yiisoft/di', 'yiisoft/aliases', 'yiisoft/request-provider', 'yiisoft/router-fastroute'],
23+
[ErrorType::UNUSED_DEPENDENCY],
24+
);

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@
3939
"psr/http-server-middleware": "^1.0.2",
4040
"psr/log": "^3.0.2",
4141
"symfony/console": "^7.3.3",
42+
"yiisoft/aliases": "^3.1",
4243
"yiisoft/config": "^1.6",
4344
"yiisoft/data": "^1.0.1",
4445
"yiisoft/data-response": "^2.1.1",
4546
"yiisoft/definitions": "^3.4",
4647
"yiisoft/di": "^1.4",
47-
"yiisoft/error-handler": "^4.2",
48+
"yiisoft/error-handler": "^4.3",
4849
"yiisoft/http": "^1.2",
4950
"yiisoft/hydrator": "^1.6.1",
5051
"yiisoft/input-http": "^1.0.1",
@@ -57,8 +58,7 @@
5758
"yiisoft/router-fastroute": "^4.0.1",
5859
"yiisoft/yii-console": "^2.3",
5960
"yiisoft/yii-event": "^2.1",
60-
"yiisoft/yii-http": "^1.0.4",
61-
"yiisoft/yii-middleware": "^1.1",
61+
"yiisoft/yii-http": "^1.1.0",
6262
"yiisoft/yii-runner-console": "^2.2",
6363
"yiisoft/yii-runner-http": "^3.2"
6464
},
@@ -69,11 +69,11 @@
6969
"codeception/module-asserts": "^3.2",
7070
"codeception/module-cli": "^2.0.1",
7171
"codeception/module-db": "^3.2.2",
72-
"codeception/module-phpbrowser": "^3.0.1",
72+
"codeception/module-phpbrowser": "^3.0.2",
7373
"codeception/module-rest": "^3.4.1",
74-
"friendsofphp/php-cs-fixer": "^3.87",
75-
"phpunit/phpunit": "^11.5.35",
76-
"rector/rector": "^2.1.4",
74+
"friendsofphp/php-cs-fixer": "^3.87.1",
75+
"phpunit/phpunit": "^11.5.36",
76+
"rector/rector": "^2.1.6",
7777
"roave/infection-static-analysis-plugin": "^1.39",
7878
"shipmonk/composer-dependency-analyser": "^1.8.3",
7979
"vimeo/psalm": "^6.13.1",

config/common/di/router.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,16 @@
22

33
declare(strict_types=1);
44

5-
use App\Http\ExceptionMiddleware;
65
use Yiisoft\Config\Config;
7-
use Yiisoft\DataResponse\Middleware\FormatDataResponse;
8-
use Yiisoft\Request\Body\RequestBodyParser;
96
use Yiisoft\Router\RouteCollection;
107
use Yiisoft\Router\RouteCollectionInterface;
118
use Yiisoft\Router\RouteCollectorInterface;
129

1310
/** @var Config $config */
1411

1512
return [
16-
RouteCollectionInterface::class => static function (RouteCollectorInterface $collector) use ($config) {
17-
$collector
18-
->middleware(RequestBodyParser::class)
19-
->middleware(FormatDataResponse::class)
20-
->middleware(ExceptionMiddleware::class)
21-
->addRoute(...$config->get('routes'));
22-
23-
return new RouteCollection($collector);
24-
},
13+
RouteCollectionInterface::class =>
14+
static fn(RouteCollectorInterface $collector) => new RouteCollection(
15+
$collector->addRoute(...$config->get('routes')),
16+
),
2517
];

config/configuration.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434
'bootstrap' => [],
3535
'bootstrap-console' => '$bootstrap',
3636
'bootstrap-web' => '$bootstrap',
37-
'routes' => [
38-
'common/routes.php',
39-
],
37+
'routes' => 'common/routes.php',
4038
],
4139
'config-plugin-environments' => [
4240
Environment::DEV => [

config/web/di/application.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
declare(strict_types=1);
44

5-
use App\Http\NotFoundHandler;
5+
use App\Http\ApiResponseFormatter;
6+
use App\Http\ExceptionMiddleware;
7+
use App\Http\NotFoundMiddleware;
8+
use Yiisoft\DataResponse\Middleware\FormatDataResponse;
69
use Yiisoft\Definitions\DynamicReference;
710
use Yiisoft\Definitions\Reference;
811
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
@@ -11,10 +14,9 @@
1114
use Yiisoft\Middleware\Dispatcher\CompositeParametersResolver;
1215
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
1316
use Yiisoft\Middleware\Dispatcher\ParametersResolverInterface;
14-
use Yiisoft\RequestProvider\RequestCatcherMiddleware;
17+
use Yiisoft\Request\Body\RequestBodyParser;
1518
use Yiisoft\Router\Middleware\Router;
1619
use Yiisoft\Yii\Http\Application;
17-
use Yiisoft\Yii\Middleware\Subfolder;
1820

1921
/** @var array $params */
2022

@@ -25,14 +27,15 @@
2527
'class' => MiddlewareDispatcher::class,
2628
'withMiddlewares()' => [
2729
[
28-
RequestCatcherMiddleware::class,
30+
static fn(ApiResponseFormatter $formatter) => new FormatDataResponse($formatter),
31+
RequestBodyParser::class,
32+
ExceptionMiddleware::class,
2933
ErrorCatcher::class,
30-
Subfolder::class,
3134
Router::class,
35+
NotFoundMiddleware::class,
3236
],
3337
],
3438
]),
35-
'fallbackHandler' => Reference::to(NotFoundHandler::class),
3639
],
3740
],
3841

config/web/di/data-response.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Http/NotFoundHandler.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Http/NotFoundMiddleware.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
use Psr\Http\Server\MiddlewareInterface;
10+
use Psr\Http\Server\RequestHandlerInterface;
11+
use Yiisoft\DataResponse\DataResponseFactoryInterface;
12+
use Yiisoft\Http\Status;
13+
14+
final readonly class NotFoundMiddleware implements MiddlewareInterface
15+
{
16+
public function __construct(
17+
private DataResponseFactoryInterface $dataResponseFactory,
18+
) {}
19+
20+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
21+
{
22+
return $this->dataResponseFactory->createResponse(
23+
'Not found.',
24+
Status::NOT_FOUND,
25+
);
26+
}
27+
}

0 commit comments

Comments
 (0)