-
-
Notifications
You must be signed in to change notification settings - Fork 1
Contextual binding
github-actions[bot] edited this page Jun 28, 2026
·
1 revision
Contextual binding — привязка зависимости к контексту класса-потребителя: when(A)->needs(B)->give(C).
Аналог Laravel when()->needs()->give() и PHP-DI contextual definitions.
| Этап | Статус | MR / релиз |
|---|---|---|
| 1. Контракты | ✅ v1.10.0 | #61 |
| 2. Runtime | ✅ v1.11.0 |
Container::when(), registry, autowirer |
| 3. Config | ✅ v1.12.0 | секция contextual в PHP/JSON/YAML/XML |
| 4. Compiler | ✅ v1.13.0 | compile snapshot + contextualGive()
|
Runtime, config и compiled container поддерживают contextual binding (#25 завершён).
$container->enableAutowiring();
$container->bind(LoggerInterface::class, 'default.logger');
$container->set('memory.logger', new MemoryLogger());
$container->when(ReportService::class)
->needs(LoggerInterface::class)
->give('memory.logger');
$report = $container->get(ReportService::class); // MemoryLogger
$other = $container->get(AuditService::class); // default bindingcontextual:
App\ReportService:
Psr\Log\LoggerInterface: memory.logger(new ContainerConfigurator())->configure($container, [__DIR__ . '/container.yaml']);Value object: consumerClass (when), need (needs), give (id или FQCN).
$container->when(ReportController::class)
->needs(LoggerInterface::class)
->give(FileLogger::class);| Интерфейс | Метод |
|---|---|
ContextualBindingConfiguratorInterface |
when(string $consumerClass) |
ContextualBindingNeedsInterface |
needs(string $need) |
ContextualBindingGiveInterface |
give(string $serviceId) |
| Метод | Описание |
|---|---|
register(ContextualBinding $binding) |
Добавить правило |
bindingsFor(string $consumerClass) |
Список правил для класса |
resolve(string $consumerClass, string $need) |
id для give или null
|
- Issue #25 — milestone v2.0 (runtime + config + compiler)
- Декомпозиция: комментарий в #25
- Comparison — contextual vs PHP-DI / Laravel
- Upgrading — 1.9.0 → 1.10.0
- FAQ
CloudCastle DI · PHP 8.1+ · PSR-11
GitHub · Packagist · Releases · Discussions · Сравнение
Исходники Wiki — каталог wiki/ в репозитории (синхронизация через GitHub Actions)
- 🏗️ Архитектура
- ⚡ Быстрый старт
- 📊 Сравнение — 5 аналогов, таблица + победители
- 🧪 Bootstrap
- 🚀 Compiled container
- 🔗 Contextual binding
- 🤖 Autowiring
- 📄 Конфигурация
- 📖 Справочник конфигурации
- 📂 Сканирование классов
- 🌍 Глобальный реестр
- 🏷️ Теги и декораторы
- 🔗 call(), bind(), hooks
- 🔄 Прототипы, alias, lazy
- 📋 Справочник API
- 🏭 Фабрики и singleton