diff --git a/CHANGELOG.md b/CHANGELOG.md index dd31836a..d4b2499c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Changed +- `compile()` throws `Unbound` for a class added with `MultiBinder` `to()` or `toProvider()` that has no unnamed binding. Bind it explicitly, e.g. `$this->bind(FooStep::class)`, or the provider class for `toProvider()` [#148] +- Conflict with bear/qiq-module 2.1.0–2.1.2 and madapaja/twig-module 2.8.0, which do not bind their compile step; update them together with ray/compiler (`composer update ray/compiler bear/qiq-module`) [#148] + ## [1.17.1] - 2026-11-23 ### Fixed @@ -114,6 +120,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Previous releases (prior to 1.11.0) are not documented in this changelog. +[Unreleased]: https://github.com/ray-di/Ray.Compiler/compare/1.17.1...HEAD [1.17.1]: https://github.com/ray-di/Ray.Compiler/compare/1.17.0...1.17.1 [1.17.0]: https://github.com/ray-di/Ray.Compiler/compare/1.16.0...1.17.0 [1.16.0]: https://github.com/ray-di/Ray.Compiler/compare/1.15.0...1.16.0 diff --git a/composer.json b/composer.json index 7295ef98..9970300e 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,10 @@ "bamarni/composer-bin-plugin": "^1.4", "phpunit/phpunit": "^11.5" }, + "conflict": { + "bear/qiq-module": ">=2.1.0 <2.1.3", + "madapaja/twig-module": ">=2.8.0 <2.8.1" + }, "config": { "sort-packages": true, "allow-plugins": { diff --git a/docs/error/Unbound.md b/docs/error/Unbound.md index 006ffb1f..66511f12 100644 --- a/docs/error/Unbound.md +++ b/docs/error/Unbound.md @@ -88,12 +88,29 @@ $this->bind(BarInterface::class)->to(Bar::class); class BarInterface_Bar implements BarInterface ``` +### 4. Unbound Multibinding Target + +`compile()` throws `Unbound` when a class added with `MultiBinder::addBinding()->to()` or `->toProvider()` has no unnamed binding: + +```php +MultiBinder::newInstance($this, StepInterface::class)->addBinding('foo')->to(FooStep::class); +``` + +#### Solution: + +Bind the class itself (the provider class for `toProvider()`): + +```php +$this->bind(FooStep::class); +``` + ## Quick Checklist 1. ✓ Required bindings are defined in your module 2. ✓ Compilation was successful 3. ✓ Compiled files exist in the correct location 4. ✓ Paths are consistent between environments +5. ✓ Every `MultiBinder` `to()` / `toProvider()` class is bound ## Related Errors diff --git a/src/Compiler.php b/src/Compiler.php index 8f791979..867a1e1c 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -6,13 +6,18 @@ use Ray\Compiler\Exception\CompileLockFailed; use Ray\Compiler\Exception\SingletonRequiresInjectionPoint; +use Ray\Compiler\Exception\Unbound; use Ray\Di\AbstractModule; use Ray\Di\AcceptInterface; use Ray\Di\Annotation\ScriptDir; +use Ray\Di\Container; use Ray\Di\ContainerFactory; use Ray\Di\Dependency; use Ray\Di\DependencyInterface; use Ray\Di\DependencyProvider; +use Ray\Di\MultiBinding\LazyProvider; +use Ray\Di\MultiBinding\LazyTo; +use ReflectionProperty; use function assert; use function fclose; @@ -51,6 +56,7 @@ final class Compiler public function compile(AbstractModule $module, string $scriptDir): Scripts { $module->override(new CompilerModule($scriptDir)); + $this->assertMultiBindingTargetsBound($module->getContainer()); $fp = fopen($scriptDir . '/compile.lock', 'a+'); if ($fp === false || ! flock($fp, LOCK_EX)) { @@ -111,4 +117,23 @@ private static function isSingletonDependency(DependencyInterface $dependency): { return ($dependency instanceof Dependency || $dependency instanceof DependencyProvider) && $dependency->isSingleton(); } + + /** LazyTo and LazyProvider resolve their target through the injector at runtime, which has no just-in-time binding */ + private function assertMultiBindingTargetsBound(Container $container): void + { + $bindings = $container->getContainer(); + foreach ($container->multiBindings as $lazyBindings) { + foreach ($lazyBindings as $lazy) { + if (! $lazy instanceof LazyTo && ! $lazy instanceof LazyProvider) { + continue; + } + + /** @var class-string $target */ + $target = (new ReflectionProperty($lazy, 'class'))->getValue($lazy); + if (! isset($bindings[$target . '-'])) { + throw new Unbound($target . '-'); + } + } + } + } } diff --git a/tests/Fake/MultiBindings/FakeUnboundTargetMultiBindingsModule.php b/tests/Fake/MultiBindings/FakeUnboundTargetMultiBindingsModule.php new file mode 100644 index 00000000..6c7ad118 --- /dev/null +++ b/tests/Fake/MultiBindings/FakeUnboundTargetMultiBindingsModule.php @@ -0,0 +1,37 @@ +provider) { + MultiBinder::newInstance($this, FakeRobotInterface::class)->addBinding('provider')->toProvider(FakeRobotProvider::class); + + return; + } + + MultiBinder::newInstance($this, FakeEngineInterface::class)->addBinding('one')->to(FakeEngine::class); + if ($this->qualifiedOnly) { + $this->bind(FakeEngine::class)->annotatedWith('one')->to(FakeEngine::class); + } + } +} diff --git a/tests/MultiBindingTest.php b/tests/MultiBindingTest.php index 735971a1..b21c1c24 100644 --- a/tests/MultiBindingTest.php +++ b/tests/MultiBindingTest.php @@ -5,9 +5,12 @@ namespace Ray\Compiler; use LogicException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\TestCase; +use Ray\Compiler\Exception\Unbound; use Ray\Compiler\Fake\MultiBindings\FakeMultiBindingsModule; +use Ray\Compiler\Fake\MultiBindings\FakeUnboundTargetMultiBindingsModule; use Ray\Compiler\MultiBindings\FakeEngine; use Ray\Compiler\MultiBindings\FakeEngine2; use Ray\Compiler\MultiBindings\FakeEngineInterface; @@ -15,6 +18,7 @@ use Ray\Compiler\MultiBindings\FakeMultiBindingConsumer; use Ray\Compiler\MultiBindings\FakeRobot; use Ray\Compiler\MultiBindings\FakeRobotInterface; +use Ray\Compiler\MultiBindings\FakeRobotProvider; use Ray\Compiler\MultiBindings\FakeSetNotFoundWithMap; use Ray\Compiler\MultiBindings\FakeSetNotFoundWithProvider; use Ray\Di\AbstractModule; @@ -142,4 +146,33 @@ public function testSetNotFoundInProvider(): void $this->expectException(SetNotFound::class); $this->injector->getInstance(FakeSetNotFoundWithProvider::class); } + + /** CompiledInjector has no just-in-time binding, so an unbound target would fail only when the Map is read */ + #[DataProvider('unboundTarget')] + public function testUnboundTargetIsRejectedAtCompileTime(bool $provider, bool $qualifiedOnly, string $index): void + { + $this->expectException(Unbound::class); + $this->expectExceptionMessage($index); + (new Compiler())->compile(new FakeUnboundTargetMultiBindingsModule($provider, $qualifiedOnly), $this->scriptDir('multi-bindings-unbound-target')); + } + + /** @return array */ + public static function unboundTarget(): array + { + return [ + 'to' => [false, false, FakeEngine::class . '-'], + 'toProvider' => [true, false, FakeRobotProvider::class . '-'], + 'bound only with a qualifier' => [false, true, FakeEngine::class . '-'], + ]; + } + + /** @return non-empty-string */ + private function scriptDir(string $name): string + { + $scriptDir = __DIR__ . '/tmp/' . $name; + deleteFiles($scriptDir); + @mkdir($scriptDir); + + return $scriptDir; + } }