Files
pinecore/.claude/kb/bootstrap.md
2026-04-06 15:43:45 +03:00

57 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Ядро, Config, Environment, ContainerFactory
## Kernel (`src/Kernel.php`)
Статический класс, хранит единственный инстанс контейнера.
```php
Kernel::boot(string $basePath): Container // ENV → Config → ContainerFactory
Kernel::container(): Container // бросает RuntimeException если не вызван boot()
```
## Environment (`src/Environment.php`)
```php
Environment::detect(): self // getenv('APP_ENV') ?: 'dev'
$env->name(): string
$env->is(string $name): bool
$env->isProd(): bool // name === 'prod'
```
## Config (`src/Config.php`)
**Загрузка** (`Config::load($configDir, $env)`):
1. Все `$configDir/*.php` — ключ = имя файла без расширения
2. Deep-merge `$configDir/env/{envName}.php` (если есть)
3. Deep-merge `$configDir/env/local.php` (если есть, всегда поверх)
**Доступ:**
```php
$config->get('section.key', $default) // dot-notation, deep lookup
$config->get('app.cors') // возвращает array
```
Merge: рекурсивный для assoc-массивов, replace для списков (array_values === array).
## ContainerFactory (`src/ContainerFactory.php`)
```php
ContainerFactory::build(Environment $env, Config $config, string $basePath): Container
```
- Autowiring включён всегда
- В prod: `$builder->enableCompilation($basePath . '/var/cache/prod/')`
- Загружает `$basePath/config/services.php` — файл должен вернуть `callable($builder, $config, $basePath)`
**Пример `config/services.php`:**
```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]),
]),
]);
};
```