Skip to content

Commit bbb072d

Browse files
refactor(kernel): integrate new match-only Router and move controller execution into Kernel
1 parent 452b1de commit bbb072d

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
All notable changes to **codemonster-ru/annabel** will be documented in this file.
44

5+
## [1.8.0] - 2025-11-16
6+
7+
### Changed
8+
9+
- The HTTP Kernel has been redesigned to integrate with the updated Router architecture (match-only).
10+
- The Kernel is now fully responsible for executing controllers, building the middleware pipeline, and generating Responses.
11+
- The handler invocation logic has been moved from the Router and Dispatcher to the Kernel.
12+
- Improved error handling: The Kernel now correctly passes exceptions to the ExceptionHandler, even if they occur within middleware or a controller.
13+
- The structure of the `dispatch()` method has been optimized; it now works only with Routes and delegates execution to `runRoute()`.
14+
15+
### Added
16+
17+
- The `runRoute()` method has been added—a single point of execution for controllers and middleware.
18+
- Support for the Annabel DI container when creating controllers and middleware.
19+
- Support for route-middleware at the Route object level.
20+
21+
### Fixed
22+
23+
- Fixed an issue where Router would return the result of executing handler instead of Route, which would break the Kernel architecture.
24+
- Fixed the middleware execution order (it now correctly wraps the controller, as in Laravel).
25+
- Fixed bugs related to empty Response and rendering errors when there was no content.
26+
527
## [1.7.0] - 2025-11-10
628

729
### Added

src/Bootstrap/Bootstrapper.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ protected function initKernel(): void
106106

107107
protected function initErrorHandler(): void
108108
{
109-
// Avoid installing a global exception handler in CLI/DBG modes
110-
// to prevent PHPUnit from flagging tests as risky.
111109
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
112110
return;
113111
}

src/Http/Kernel.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Codemonster\Annabel\Application;
66
use Codemonster\Errors\Contracts\ExceptionHandlerInterface;
7+
use Codemonster\Router\Route;
78
use Codemonster\Router\Router;
89
use Codemonster\Http\Request;
910
use Codemonster\Http\Response;
@@ -71,13 +72,13 @@ protected function handleException(Throwable $e): Response
7172

7273
protected function dispatch(Request $request): mixed
7374
{
74-
$result = $this->router->dispatch($request->method(), $request->uri());
75+
$route = $this->router->dispatch($request->method(), $request->uri());
7576

76-
if ($result === null) {
77+
if (!$route) {
7778
return $this->handleHttpError(404, 'Page not found');
7879
}
7980

80-
return $result;
81+
return $this->runRoute($route, $request);
8182
}
8283

8384
protected function handleHttpError(int $status, string $message = ''): Response
@@ -135,4 +136,39 @@ public function getRouter(): Router
135136
{
136137
return $this->router;
137138
}
139+
140+
protected function runRoute(Route $route, Request $request): mixed
141+
{
142+
$handler = $route->handler;
143+
$middlewareList = $route->getMiddleware();
144+
145+
$kernel = $this;
146+
147+
$core = function (Request $req) use ($handler, $kernel) {
148+
149+
if (is_array($handler)) {
150+
[$class, $method] = $handler;
151+
152+
$controller = $kernel->app->make($class);
153+
154+
return $controller->$method($req);
155+
}
156+
157+
return $handler($req);
158+
};
159+
160+
$pipeline = array_reduce(
161+
array_reverse($middlewareList),
162+
function ($next, $middlewareClass) use ($kernel) {
163+
return function (Request $req) use ($middlewareClass, $next, $kernel) {
164+
$middleware = $kernel->app->make($middlewareClass);
165+
166+
return $middleware->handle($req, $next);
167+
};
168+
},
169+
$core
170+
);
171+
172+
return $pipeline($request);
173+
}
138174
}

0 commit comments

Comments
 (0)