| name | autowire |
| description | Automatically bind and configure interfaces to class implementations. |
Autowire
When to use this skill
When an interface needs to be bound to a class one of these ways:
$this->app->bind(HelloInterface::class, WorldClass::class);
$this->app->when(Greeting::class)->needs(GoodbyeInterface::class)->give(GoodbyeInterface::class);
$this->app->when(Greeting::class)->needs(GoodbyeInterface::class)->giveTagged(GoodbyeInterface::class);
$this->app->when(Greeting::class)->needs(HelloInterface::class)->giveTagged('myTag');
It MUST be replaced with the usage of the following PHP Attributes:
JeroenG\Autowire\Attribute\Autowire
JeroenG\Autowire\Attribute\Configure
Examples
Autowiring interface to class
use JeroenG\Autowire\Attribute\Autowire;
#[Autowire]
interface HelloInterface
{
public function hello(): string;
}
Configuring constructor arguments
use JeroenG\Autowire\Attribute\Configure;
#[Configure(['$message' => '%app.message%'])]
#[Configure(['$message' => '@App\Domain\Message'])]
#[Configure(['$messages' => '#messages'])]
#[Configure(['$message' => '%app.message%', '$logger' => '@Psr\Log\LoggerInterface'])]
class WorldClass
{
private $message;
private $logger;
public function __construct($message, $logger)
{
$this->message = $message;
$this->logger = $logger;
}
}
Tagging an interface
Use this only when there are multiple implementations of the same interface.
use JeroenG\Autowire\Attribute\Tag;
#[Tag]
interface GoodbyeInterface
{
public function goodbye(): string;
}
Autowiring event listeners
use JeroenG\Autowire\Attribute\Listen;
#[Listen(Registered::class)]
#[Listen(Login::class)]
class UpdateLastLoginListener {
}