Skip to content
Merged
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
17 changes: 17 additions & 0 deletions docs/error/Unbound.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 . '-');
}
}
}
}
}
37 changes: 37 additions & 0 deletions tests/Fake/MultiBindings/FakeUnboundTargetMultiBindingsModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Ray\Compiler\Fake\MultiBindings;

use Ray\Compiler\MultiBindings\FakeEngine;
use Ray\Compiler\MultiBindings\FakeEngineInterface;
use Ray\Compiler\MultiBindings\FakeRobotInterface;
use Ray\Compiler\MultiBindings\FakeRobotProvider;
use Ray\Di\AbstractModule;
use Ray\Di\MultiBinder;

/** Adds a multibinding target without binding it */
final class FakeUnboundTargetMultiBindingsModule extends AbstractModule
{
public function __construct(
private bool $provider,
private bool $qualifiedOnly = false
) {
parent::__construct();
}

protected function configure(): void
{
if ($this->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);
}
}
}
33 changes: 33 additions & 0 deletions tests/MultiBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
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;
use Ray\Compiler\MultiBindings\FakeMultiBindingAnnotation;
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;
Expand Down Expand Up @@ -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<string, array{bool, bool, string}> */
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;
}
}
Loading