Autoload routes & services
This commit is contained in:
@@ -41,16 +41,27 @@ ContainerFactory::build(Environment $env, Config $config, string $basePath): Con
|
|||||||
|
|
||||||
- Autowiring включён всегда
|
- Autowiring включён всегда
|
||||||
- В prod: `$builder->enableCompilation($basePath . '/var/cache/prod/')`
|
- В 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
|
```php
|
||||||
return function (ContainerBuilder $builder, Config $config, string $basePath): void {
|
return function (ContainerBuilder $builder, Config $config, string $basePath): void {
|
||||||
$builder->addDefinitions([
|
$builder->addDefinitions([
|
||||||
UserProviderInterface::class => fn($c) => $c->get(UserRepository::class),
|
UserProviderInterface::class => fn($c) => $c->get(UserRepository::class),
|
||||||
Router::class => fn() => new Router([
|
|
||||||
new RouteDefinition('GET', '/users/{id}', GetUserController::class, [AuthMiddleware::class]),
|
|
||||||
]),
|
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Pronchev\Pinecore;
|
|||||||
|
|
||||||
use DI\Container;
|
use DI\Container;
|
||||||
use DI\ContainerBuilder;
|
use DI\ContainerBuilder;
|
||||||
|
use Pronchev\Pinecore\Http\Router;
|
||||||
|
|
||||||
class ContainerFactory
|
class ContainerFactory
|
||||||
{
|
{
|
||||||
@@ -21,6 +22,14 @@ class ContainerFactory
|
|||||||
|
|
||||||
$builder->useAutowiring(true);
|
$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';
|
$servicesFile = $basePath . '/config/services.php';
|
||||||
if (file_exists($servicesFile)) {
|
if (file_exists($servicesFile)) {
|
||||||
$definitions = require $servicesFile;
|
$definitions = require $servicesFile;
|
||||||
|
|||||||
Reference in New Issue
Block a user