ByTIC HTTP component - A Symfony-compliant HTTP kernel with PSR-7/PSR-15 middleware support
- Symfony-compliant HTTP Kernel - Implements Symfony's HttpKernelInterface with full event dispatching
- PSR-7/PSR-15 Middleware Support - Backward compatible with existing PSR middleware
- Event-Driven Architecture - Dispatch kernel events (REQUEST, RESPONSE, EXCEPTION, TERMINATE)
- Request Stack Management - Track main and sub-requests like Symfony
- Controller Resolution - Symfony-style controller and argument resolvers
- Exception Handling - Flexible exception handling with event subscribers
- Fully Backward Compatible - Works with existing middleware-based code
composer require bytic/httpuse Nip\Http\Kernel\Kernel;
use Nip\Application\Application;
use Nip\Router\Router;
use Symfony\Component\HttpFoundation\Request;
// Create kernel
$app = new Application();
$router = new Router();
$kernel = new Kernel($app, $router);
// Handle request
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
// Terminate kernel
$kernel->terminate($request, $response);use Nip\Http\Kernel\Event\ResponseEvent;
use Nip\Http\Kernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ResponseHeadersSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => ['onKernelResponse', 0],
];
}
public function onKernelResponse(ResponseEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$response = $event->getResponse();
$response->headers->set('X-Powered-By', 'ByTIC HTTP Kernel');
}
}
// Register subscriber
$kernel->getEventDispatcher()->addSubscriber(new ResponseHeadersSubscriber());KernelEvents::REQUEST- Dispatched at the start of request handlingKernelEvents::CONTROLLER- Dispatched before controller executionKernelEvents::RESPONSE- Dispatched before sending the responseKernelEvents::EXCEPTION- Dispatched when an exception occursKernelEvents::TERMINATE- Dispatched after response is sent
The kernel still supports PSR-15 middleware:
$kernel->pushMiddleware(MyMiddleware::class);
$kernel->prependMiddleware(AnotherMiddleware::class);Access the current request anywhere:
$currentRequest = $kernel->getRequestStack()->getCurrentRequest();
$mainRequest = $kernel->getRequestStack()->getMainRequest();The new event system runs alongside the existing middleware, so you can migrate gradually:
class MyMiddleware implements ServerMiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// Modify request
$request = $request->withAttribute('foo', 'bar');
// Get response
$response = $handler->handle($request);
// Modify response
$response = $response->withHeader('X-Custom', 'value');
return $response;
}
}class MyEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onRequest', 10],
KernelEvents::RESPONSE => ['onResponse', 10],
];
}
public function onRequest(RequestEvent $event): void
{
$request = $event->getRequest();
$request->attributes->set('foo', 'bar');
}
public function onResponse(ResponseEvent $event): void
{
$response = $event->getResponse();
$response->headers->set('X-Custom', 'value');
}
}This package follows Symfony's kernel architecture:
- Request Event - Early request processing, can short-circuit with a response
- Controller Resolution - Determine which controller to execute
- Middleware Stack - Execute PSR-15 middleware (backward compatible)
- Response Event - Modify the response before sending
- Terminate Event - Clean up after response is sent
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
