diff --git a/.claude/kb/bootstrap.md b/.claude/kb/bootstrap.md index 7be57f2..2c2ac04 100644 --- a/.claude/kb/bootstrap.md +++ b/.claude/kb/bootstrap.md @@ -41,16 +41,27 @@ ContainerFactory::build(Environment $env, Config $config, string $basePath): Con - Autowiring включён всегда - В prod: `$builder->enableCompilation($basePath . '/var/cache/prod/')` -- Загружает `$basePath/config/services.php` — файл должен вернуть `callable($builder, $config, $basePath)` +- Загружает `$basePath/config/routes.php` (если есть) — файл возвращает `RouteDefinition[]`, + фреймворк автоматически создаёт `Router` и регистрирует его в контейнере +- Загружает `$basePath/config/services.php` (если есть) — файл возвращает + `callable($builder, $config, $basePath)`; загружается после routes.php и может переопределить + любые определения, включая `Router::class` -**Пример `config/services.php`:** +**`config/routes.php`** (конвенция, предпочтительный способ): +```php +use Pronchev\Pinecore\Http\RouteDefinition; + +return [ + new RouteDefinition('GET', '/users/{id}', GetUserController::class, [AuthMiddleware::class]), + new RouteDefinition('POST', '/users', CreateUserController::class, [AuthMiddleware::class]), +]; +``` + +**`config/services.php`** (для DI-биндингов и переопределений): ```php return function (ContainerBuilder $builder, Config $config, string $basePath): void { $builder->addDefinitions([ UserProviderInterface::class => fn($c) => $c->get(UserRepository::class), - Router::class => fn() => new Router([ - new RouteDefinition('GET', '/users/{id}', GetUserController::class, [AuthMiddleware::class]), - ]), ]); }; ``` diff --git a/src/ContainerFactory.php b/src/ContainerFactory.php index e7411e6..3b91daf 100644 --- a/src/ContainerFactory.php +++ b/src/ContainerFactory.php @@ -4,6 +4,7 @@ namespace Pronchev\Pinecore; use DI\Container; use DI\ContainerBuilder; +use Pronchev\Pinecore\Http\Router; class ContainerFactory { @@ -21,6 +22,14 @@ class ContainerFactory $builder->useAutowiring(true); + $routesFile = $basePath . '/config/routes.php'; + if (file_exists($routesFile)) { + $routes = require $routesFile; + $builder->addDefinitions([ + Router::class => fn() => new Router($routes), + ]); + } + $servicesFile = $basePath . '/config/services.php'; if (file_exists($servicesFile)) { $definitions = require $servicesFile;