2025-12-14 23:10:01 +00:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Phred\Http;
|
|
|
|
|
|
|
|
|
|
use DI\Container;
|
|
|
|
|
use DI\ContainerBuilder;
|
|
|
|
|
use FastRoute\Dispatcher;
|
|
|
|
|
use FastRoute\RouteCollector;
|
|
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory;
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
|
|
|
|
|
use Relay\Relay;
|
|
|
|
|
|
|
|
|
|
use function FastRoute\simpleDispatcher;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Core HTTP Kernel builds container, routes, and PSR-15 pipeline and processes requests.
|
|
|
|
|
*/
|
|
|
|
|
final class Kernel
|
|
|
|
|
{
|
|
|
|
|
private Container $container;
|
|
|
|
|
private Dispatcher $dispatcher;
|
|
|
|
|
|
|
|
|
|
public function __construct(?Container $container = null, ?Dispatcher $dispatcher = null)
|
|
|
|
|
{
|
|
|
|
|
$this->container = $container ?? $this->buildContainer();
|
Implement M5 service providers, M6 MVC bases, and URL extension negotiation; update docs and tests
• M5: Add ServiceProviderInterface and ProviderRepository; integrate providers into Kernel (register before container build, boot after); add RouteRegistry with clear(); add default core providers (Routing, Template, ORM, Flags, Testing) and AppServiceProvider; add contracts and default drivers (Template/Eyrie, Orm/Pairity, Flags/Flagpole, Testing/Codeception)
• Routing: allow providers to contribute routes; add ProviderRouteTest
• Config: add config/providers.php; extend config/app.php with driver keys; document env keys
• M6: Introduce MVC bases: Controller, APIController (JSON helpers), ViewController (html + renderView helpers), View (transformData + renderer); add ViewWithDefaultTemplate and default-template flow; adjust method signatures to data-first and delegate template override to View
• HTTP: Add UrlExtensionNegotiationMiddleware (opt-in via URL_EXTENSION_NEGOTIATION, whitelist via URL_EXTENSION_WHITELIST with default json|php|none); wire before ContentNegotiationMiddleware
• Tests: add UrlExtensionNegotiationTest and MvcViewTest; ensure RouteRegistry::clear prevents duplicate routes in tests
• Docs: Update README with M5 provider usage, M6 MVC examples and template selection conventions, and URL extension negotiation; mark M5 complete in MILESTONES; add M12 task to provide XML support and enable xml in whitelist by default
2025-12-15 22:08:57 +00:00
|
|
|
// Providers may contribute routes during boot; ensure dispatcher is built after container init
|
2025-12-14 23:10:01 +00:00
|
|
|
$this->dispatcher = $dispatcher ?? $this->buildDispatcher();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function container(): Container
|
|
|
|
|
{
|
|
|
|
|
return $this->container;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dispatcher(): Dispatcher
|
|
|
|
|
{
|
|
|
|
|
return $this->dispatcher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle(ServerRequest $request): ResponseInterface
|
|
|
|
|
{
|
|
|
|
|
$psr17 = new Psr17Factory();
|
|
|
|
|
$middleware = [
|
2025-12-15 15:15:49 +00:00
|
|
|
new Middleware\ProblemDetailsMiddleware(
|
|
|
|
|
\Phred\Support\Config::get('APP_DEBUG', 'false') === 'true',
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
filter_var(\Phred\Support\Config::get('API_PROBLEM_DETAILS', 'true'), FILTER_VALIDATE_BOOLEAN)
|
|
|
|
|
),
|
Implement M5 service providers, M6 MVC bases, and URL extension negotiation; update docs and tests
• M5: Add ServiceProviderInterface and ProviderRepository; integrate providers into Kernel (register before container build, boot after); add RouteRegistry with clear(); add default core providers (Routing, Template, ORM, Flags, Testing) and AppServiceProvider; add contracts and default drivers (Template/Eyrie, Orm/Pairity, Flags/Flagpole, Testing/Codeception)
• Routing: allow providers to contribute routes; add ProviderRouteTest
• Config: add config/providers.php; extend config/app.php with driver keys; document env keys
• M6: Introduce MVC bases: Controller, APIController (JSON helpers), ViewController (html + renderView helpers), View (transformData + renderer); add ViewWithDefaultTemplate and default-template flow; adjust method signatures to data-first and delegate template override to View
• HTTP: Add UrlExtensionNegotiationMiddleware (opt-in via URL_EXTENSION_NEGOTIATION, whitelist via URL_EXTENSION_WHITELIST with default json|php|none); wire before ContentNegotiationMiddleware
• Tests: add UrlExtensionNegotiationTest and MvcViewTest; ensure RouteRegistry::clear prevents duplicate routes in tests
• Docs: Update README with M5 provider usage, M6 MVC examples and template selection conventions, and URL extension negotiation; mark M5 complete in MILESTONES; add M12 task to provide XML support and enable xml in whitelist by default
2025-12-15 22:08:57 +00:00
|
|
|
// Perform extension-based content negotiation hinting before standard negotiation
|
|
|
|
|
new Middleware\UrlExtensionNegotiationMiddleware(),
|
2025-12-15 15:15:49 +00:00
|
|
|
new Middleware\ContentNegotiationMiddleware(),
|
2025-12-14 23:10:01 +00:00
|
|
|
new Middleware\RoutingMiddleware($this->dispatcher, $psr17),
|
2025-12-15 15:15:49 +00:00
|
|
|
new Middleware\DispatchMiddleware($psr17),
|
2025-12-14 23:10:01 +00:00
|
|
|
];
|
|
|
|
|
$relay = new Relay($middleware);
|
|
|
|
|
return $relay->handle($request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function buildContainer(): Container
|
|
|
|
|
{
|
|
|
|
|
$builder = new ContainerBuilder();
|
Implement M5 service providers, M6 MVC bases, and URL extension negotiation; update docs and tests
• M5: Add ServiceProviderInterface and ProviderRepository; integrate providers into Kernel (register before container build, boot after); add RouteRegistry with clear(); add default core providers (Routing, Template, ORM, Flags, Testing) and AppServiceProvider; add contracts and default drivers (Template/Eyrie, Orm/Pairity, Flags/Flagpole, Testing/Codeception)
• Routing: allow providers to contribute routes; add ProviderRouteTest
• Config: add config/providers.php; extend config/app.php with driver keys; document env keys
• M6: Introduce MVC bases: Controller, APIController (JSON helpers), ViewController (html + renderView helpers), View (transformData + renderer); add ViewWithDefaultTemplate and default-template flow; adjust method signatures to data-first and delegate template override to View
• HTTP: Add UrlExtensionNegotiationMiddleware (opt-in via URL_EXTENSION_NEGOTIATION, whitelist via URL_EXTENSION_WHITELIST with default json|php|none); wire before ContentNegotiationMiddleware
• Tests: add UrlExtensionNegotiationTest and MvcViewTest; ensure RouteRegistry::clear prevents duplicate routes in tests
• Docs: Update README with M5 provider usage, M6 MVC examples and template selection conventions, and URL extension negotiation; mark M5 complete in MILESTONES; add M12 task to provide XML support and enable xml in whitelist by default
2025-12-15 22:08:57 +00:00
|
|
|
|
|
|
|
|
// Allow service providers to register definitions before defaults
|
|
|
|
|
$configAdapter = new \Phred\Support\DefaultConfig();
|
|
|
|
|
$providers = new \Phred\Support\ProviderRepository($configAdapter);
|
|
|
|
|
$providers->load();
|
|
|
|
|
$providers->registerAll($builder);
|
|
|
|
|
|
|
|
|
|
// Add core definitions/bindings
|
2025-12-15 15:15:49 +00:00
|
|
|
$builder->addDefinitions([
|
|
|
|
|
\Phred\Support\Contracts\ConfigInterface::class => \DI\autowire(\Phred\Support\DefaultConfig::class),
|
|
|
|
|
\Phred\Http\Contracts\ErrorFormatNegotiatorInterface::class => \DI\autowire(\Phred\Http\Support\DefaultErrorFormatNegotiator::class),
|
|
|
|
|
\Phred\Http\Contracts\RequestIdProviderInterface::class => \DI\autowire(\Phred\Http\Support\DefaultRequestIdProvider::class),
|
|
|
|
|
\Phred\Http\Contracts\ExceptionToStatusMapperInterface::class => \DI\autowire(\Phred\Http\Support\DefaultExceptionToStatusMapper::class),
|
|
|
|
|
\Phred\Http\Contracts\ApiResponseFactoryInterface::class => \DI\autowire(\Phred\Http\Responses\DelegatingApiResponseFactory::class),
|
|
|
|
|
\Phred\Http\Responses\RestResponseFactory::class => \DI\autowire(\Phred\Http\Responses\RestResponseFactory::class),
|
|
|
|
|
\Phred\Http\Responses\JsonApiResponseFactory::class => \DI\autowire(\Phred\Http\Responses\JsonApiResponseFactory::class),
|
|
|
|
|
]);
|
Implement M5 service providers, M6 MVC bases, and URL extension negotiation; update docs and tests
• M5: Add ServiceProviderInterface and ProviderRepository; integrate providers into Kernel (register before container build, boot after); add RouteRegistry with clear(); add default core providers (Routing, Template, ORM, Flags, Testing) and AppServiceProvider; add contracts and default drivers (Template/Eyrie, Orm/Pairity, Flags/Flagpole, Testing/Codeception)
• Routing: allow providers to contribute routes; add ProviderRouteTest
• Config: add config/providers.php; extend config/app.php with driver keys; document env keys
• M6: Introduce MVC bases: Controller, APIController (JSON helpers), ViewController (html + renderView helpers), View (transformData + renderer); add ViewWithDefaultTemplate and default-template flow; adjust method signatures to data-first and delegate template override to View
• HTTP: Add UrlExtensionNegotiationMiddleware (opt-in via URL_EXTENSION_NEGOTIATION, whitelist via URL_EXTENSION_WHITELIST with default json|php|none); wire before ContentNegotiationMiddleware
• Tests: add UrlExtensionNegotiationTest and MvcViewTest; ensure RouteRegistry::clear prevents duplicate routes in tests
• Docs: Update README with M5 provider usage, M6 MVC examples and template selection conventions, and URL extension negotiation; mark M5 complete in MILESTONES; add M12 task to provide XML support and enable xml in whitelist by default
2025-12-15 22:08:57 +00:00
|
|
|
$container = $builder->build();
|
|
|
|
|
|
|
|
|
|
// Reset provider-registered routes to avoid duplicates across multiple kernel instantiations (e.g., tests)
|
|
|
|
|
\Phred\Http\Routing\RouteRegistry::clear();
|
|
|
|
|
// Boot providers after container is available
|
|
|
|
|
$providers->bootAll($container);
|
|
|
|
|
|
|
|
|
|
return $container;
|
2025-12-14 23:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function buildDispatcher(): Dispatcher
|
|
|
|
|
{
|
|
|
|
|
$routesPath = dirname(__DIR__, 2) . '/routes';
|
|
|
|
|
$collector = static function (RouteCollector $r) use ($routesPath): void {
|
|
|
|
|
// Load user-defined routes if present
|
|
|
|
|
$router = new Router($r);
|
|
|
|
|
foreach (['web.php', 'api.php'] as $file) {
|
|
|
|
|
$path = $routesPath . '/' . $file;
|
|
|
|
|
if (is_file($path)) {
|
|
|
|
|
/** @noinspection PhpIncludeInspection */
|
|
|
|
|
(static function ($router) use ($path) { require $path; })($router);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Implement M5 service providers, M6 MVC bases, and URL extension negotiation; update docs and tests
• M5: Add ServiceProviderInterface and ProviderRepository; integrate providers into Kernel (register before container build, boot after); add RouteRegistry with clear(); add default core providers (Routing, Template, ORM, Flags, Testing) and AppServiceProvider; add contracts and default drivers (Template/Eyrie, Orm/Pairity, Flags/Flagpole, Testing/Codeception)
• Routing: allow providers to contribute routes; add ProviderRouteTest
• Config: add config/providers.php; extend config/app.php with driver keys; document env keys
• M6: Introduce MVC bases: Controller, APIController (JSON helpers), ViewController (html + renderView helpers), View (transformData + renderer); add ViewWithDefaultTemplate and default-template flow; adjust method signatures to data-first and delegate template override to View
• HTTP: Add UrlExtensionNegotiationMiddleware (opt-in via URL_EXTENSION_NEGOTIATION, whitelist via URL_EXTENSION_WHITELIST with default json|php|none); wire before ContentNegotiationMiddleware
• Tests: add UrlExtensionNegotiationTest and MvcViewTest; ensure RouteRegistry::clear prevents duplicate routes in tests
• Docs: Update README with M5 provider usage, M6 MVC examples and template selection conventions, and URL extension negotiation; mark M5 complete in MILESTONES; add M12 task to provide XML support and enable xml in whitelist by default
2025-12-15 22:08:57 +00:00
|
|
|
// Allow providers to contribute routes
|
|
|
|
|
\Phred\Http\Routing\RouteRegistry::apply($r, $router);
|
|
|
|
|
|
2025-12-15 15:15:49 +00:00
|
|
|
// Ensure default demo routes exist for acceptance/demo
|
2025-12-14 23:10:01 +00:00
|
|
|
$r->addRoute('GET', '/_phred/health', [Controllers\HealthController::class, '__invoke']);
|
2025-12-15 15:15:49 +00:00
|
|
|
$r->addRoute('GET', '/_phred/format', [Controllers\FormatController::class, '__invoke']);
|
2025-12-14 23:10:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return simpleDispatcher($collector);
|
|
|
|
|
}
|
|
|
|
|
}
|