diff --git a/src/Dic/Configuration/ObjectConfig.php b/src/Dic/Configuration/ObjectConfig.php index 5d94c60..0ff2732 100644 --- a/src/Dic/Configuration/ObjectConfig.php +++ b/src/Dic/Configuration/ObjectConfig.php @@ -22,6 +22,7 @@ use Thesis\Dic\Ref; use function Thesis\Dic\Internal\caller; use function Thesis\Formatter\formatReflectedClass; +use function Typhoon\Type\objectT; /** * @api @@ -179,6 +180,14 @@ public function doNotAutowire(): static return $this; } + /** + * Binds this service to its declared class so it is injected wherever that class is required. + */ + public function bindSelf(): static + { + return $this->bind(objectT($this->reflection->name)); + } + /** * Sets a single constructor / factory parameter by position or name. */ diff --git a/tests/DicTest.php b/tests/DicTest.php index 111da09..205efb0 100644 --- a/tests/DicTest.php +++ b/tests/DicTest.php @@ -323,6 +323,17 @@ public function singletonInstanceIsSharedBetweenDependents(): void Assert::same($pair->first, $pair->second); } + public function bindSelfAutowiresByDeclaredClass(): void + { + $pair = self::build(static function (Dic $dic) { + $dic->object(Holder::class)->bindSelf(); + + return $dic->object(Pair::class); + }); + + Assert::same($pair->first, $pair->second); + } + public function defaultValueIsUsedWhenNotAutowirable(): void { $object = self::build(