Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Dic/Configuration/ObjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/DicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down