Autowire attribute
The \Kaspi\DiContainer\Attributes\Autowire attribute registers a PHP class in the container or injects a dependency into a method parameter.
Attribute configuration:
\Attribute::TARGET_CLASS\Attribute::TARGET_PARAMETER\Attribute::IS_REPEATABLE
Attribute signature:
\Kaspi\DiContainer\Attributes\Autowire::__construct(
string $id = '',
?bool $isSingleton = null,
array $arguments = [],
array|\Kaspi\DiContainer\Attributes\Tag|null $tags = null,
?array $setups = null,
callable|false|string $resetter = false,
bool $isLazy = false,
)Parameters:
$id– container identifier [1], or an empty string.$isSingleton– The container will return the same object instance (singleton pattern). If set tonull, the value will be determined based on the container configuration.$arguments– arguments for a PHP class constructor.$tags– tag with metadata.$setups– dependency injection via a setter method[2] [3].$resetter– value for resetting an object's state.$isLazy– declare a "lazy object".
TIP
Read the documentation section on injecting "lazy" objects via the container.
Container identifier for a PHP class
When declaring a PHP class using the \Kaspi\DiContainer\Attributes\Autowire attribute, an empty string in the $id parameter is interpreted by the container as a container identifier represented by the FQCN[4] of the PHP class.
Register the PHP class \App\Services\Foo using an attribute, with the container identifier set to the string 'App\Services\Foo', and configure the injection of the scalar value 'Lorem ipsum' into the class constructor parameter $value.
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
#[Autowire(arguments: ['Lorem ipsum'])]
final class Foo
{
public function __construct(public readonly string $value) {}
}<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->value);Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string(11) "Lorem ipsum"Declaring a PHP class with multiple identifiers
The \Kaspi\DiContainer\Attributes\Autowire attribute can be used to register the same PHP class in the container under different identifiers[1:1].
Declare the PHP class \App\Services\Bar using an attribute, with the container identifier specified as the string 'App\Services\Bar', and define the injection of the scalar value 10 into the constructor parameter $value.
Declare the PHP class \App\Services\Bar using an attribute, with the container identifier specified as the string 'services.bar_20', and define the injection of the scalar value 20 into the constructor parameter $value.
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
#[Autowire(arguments: [10])]
#[Autowire(id: 'services.bar_20', arguments: [20])]
final class Bar
{
public function __construct(public readonly int $value) {}
}<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Bar;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
$bar = $container->get(Bar::class);
var_dump($bar->value);
$bar20 = $container->get('services.bar_20');
var_dump($bar20->value);Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
int(10)
int(20)PHP class constructor arguments
The $arguments parameter of the \Kaspi\DiContainer\Attributes\Autowire attribute passes arguments to the constructor of a PHP class.
When declaring dependency injection for a specific parameter, use the name of that parameter as the array key within $arguments.
Parameters that do not receive arguments via $arguments will be injected by the container automatically, based on the configuration.
You can use classes from other container definitions within the arguments.
Kaspi\DiContainer\DiDefinition\DiDefinitionAutowire– PHP class.Kaspi\DiContainer\DiDefinition\DiDefinitionCallable– injects a dependency by invoking acallabletype.Kaspi\DiContainer\DiDefinition\DiDefinitionFactory– factory for dependency injection.Kaspi\DiContainer\DiDefinition\DiDefinitionGet– dependency injection via a reference to a container identifier.Kaspi\DiContainer\DiDefinition\DiDefinitionParameter– dependency injection from container parameters.Kaspi\DiContainer\DiDefinition\DiDefinitionParameterRutime– dependency injection from runtime container parameters.Kaspi\DiContainer\DiDefinition\DiDefinitionProxyClosure– dependency injection via "lazy loading".Kaspi\DiContainer\DiDefinition\DiDefinitionRuntime– runtime object injection.Kaspi\DiContainer\DiDefinition\DiDefinitionTaggedAs– dependency injection - collection by tag.
Example of declaring arguments for a PHP class constructor
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/FooService.php
namespace App\Services;
use App\Interfaces\QuxInterface;
final class FooService
{
public function __construct(
public readonly BatService $bat,
public readonly QuxInterface $qux,
public readonly string $adminEmail
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/BarService.php
namespace App\Services;
use App\Interfaces\QuxInterface;
final class BarService implements QuxInterface
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/BazService.php
namespace App\Services;
use App\Interfaces\QuxInterface;
final class BazService implements QuxInterface
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/BatService.php
namespace App\Services;
final class BatService
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Interfaces/QuxInterface.php
namespace App\Interfaces;
interface QuxInterface
{
// …
}It is necessary to declare the PHP class \App\Services\FooService with different container identifiers and arguments.
<?php
declare(strict_types=1);
// file: /app/src/Services/FooService.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
use Kaspi\DiContainer\DiDefinition\{
DiDefinitionGet as DiGet,
DiDefinitionParameter as DiParameter
};
use App\Interfaces\QuxInterface;
#[Autowire(
arguments: [
'qux' => new DiGet(BarService::class),
'adminEmail' => new DiParameter('emails.bar'),
]
)]
#[Autowire(
id: 'services.foo_service.with_baz',
arguments: [
'qux' => new DiGet(BazService::class),
'adminEmail' => new DiParameter('emails.baz'),
]
)]
final class FooService
{
public function __construct(
public readonly BatService $bat,
public readonly QuxInterface $qux,
public readonly string $adminEmail
) {}
}<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\{FooService, BarService, BazService, BatService};
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->addParameters([
'emails.bar' => 'manager@example.com',
'emails.baz' => 'admin@example.com',
])
->build();
$fooService = $container->get(FooService::class);
var_dump($fooService->bat::class);
var_dump($fooService->qux::class);
var_dump($fooService->adminEmail);
$fooServiceWithBaz = $container->get('services.foo_service.with_baz');
echo PHP_EOL;
var_dump($fooServiceWithBaz->bat::class);
var_dump($fooServiceWithBaz->qux::class);
var_dump($fooServiceWithBaz->adminEmail);Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string(23) "App\Services\BatService"
string(23) "App\Services\BarService"
string(19) "manager@example.com"
string(23) "App\Services\BatService"
string(23) "App\Services\BazService"
string(16) "admin@example.comDependency injection via setter methods
The $setups parameter of the \Kaspi\DiContainer\Attributes\Autowire attribute declares dependency injection via setter methods of a PHP class, using the \Kaspi\DiContainer\Attributes\Setup and \Kaspi\DiContainer\Attributes\SetupImmutable attributes.
A null value for the $setups parameter instructs the container to inject dependencies via the Setup and SetupImmutable attributes specified on the class methods.
A value of [] (an empty array) for the $setups parameter instructs the container not to inject dependencies via setter methods, even if the class methods are marked with the Setup or SetupImmutable attributes.
<?php
declare(strict_types=1);
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
use Kaspi\DiContainer\Attributes\Setup;
use Kaspi\DiContainer\Attributes\SetupImmutable;
use Kaspi\DiContainer\DiDefinition\DiDefinitionGet as DiGet;
use Kaspi\DiContainer\DiDefinition\DiDefinitionParameter as Parameter;
#[Autowire(
setups: [
'bar' => new Setup(
new Parameter('params.api_key')
),
'baz' => new SetupImmutable(
new DiGet(Baz::class)
),
]
)]
final class Foo
{
// …
public function bar(string $apiKey): void
{
// …
}
public function baz(QuxInterface $qux): self
{
// …
}
}Tags
The $tags parameter of the \Kaspi\DiContainer\Attributes\Autowire attribute sets the tag and metadata for the declared PHP class using the attribute \Kaspi\DiContainer\Attributes\Tag.
A null value for the $tags parameter instructs the container to set the tag via the Tag attribute specified in the PHP class being declared.
A value of [] (an empty array) for the $tags parameter instructs the container not to assign a tag to the PHP class being declared, even if that class has a Tag attribute specified.
<?php
declare(strict_types=1);
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
use Kaspi\DiContainer\Attributes\Tag;
#[Autowire(
tags: [
new Tag('tags.fuze'),
new Tag('tags.internal_service'),
]
)]
final class Foo
{
// …
}Dependency injection into a method parameter or a called type
You can declare the injection of a PHP class into a method parameter or a callable type[5] using the \Kaspi\DiContainer\Attributes\Autowire attribute.
The value for the $id parameter of the Autowire attribute can be specified as an empty string or an FQCN[4:1] string.
An empty string in the $id parameter of the Autowire attribute will be interpreted by the container as the type of the parameter[6] to which the attribute is applied.
Example of declaring an injection into a method parameter
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use App\Interfaces\QuxInterface;
class Foo
{
public function __construct(
public readonly QuxInterface $qux,
public readonly Baz $baz,
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
use App\Interfaces\QuxInterface;
final class Bar implements QuxInterface
{
public function __construct(public readonly string $notifyEmail) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Services;
final class Baz
{
public function __construct(
public readonly string $email = 'baz@localhost'
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Interfaces/QuxInterface.php
namespace App\Interfaces;
interface QuxInterface
{
// …
}For the PHP class \App\Services\Foo, declare the injection of $qux and $baz into the constructor parameters using the Kaspi\DiContainer\Attributes\Autowire attribute.
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use App\Interfaces\QuxInterface;
use Kaspi\DiContainer\Attributes\Autowire;
use Kaspi\DiContainer\DiDefinition\DiDefinitionParameter as DiParameter;
class Foo
{
public function __construct(
#[Autowire(
id: Bar::class,
arguments: [
new DiParameter('emails.bar')
]
)]
public readonly QuxInterface $qux,
#[Autowire(
arguments: [
new DiParameter('emails.baz')
]
)]
public readonly Baz $baz,
) {}
}<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\{Foo, Bar, Baz};
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->addParameters([
'emails.bar' => 'manager@example.com',
'emails.baz' => 'admin@example.com',
])
->build();
$foo = $container->get(Foo::class);
var_dump($foo->qux->notifyEmail);
var_dump($foo->baz->email);Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string(19) "manager@example.com"
string(16) "admin@example.com"A container ID is a string name (or a class/interface name) used to register a service or object within a container and to retrieve it via the
get()method. ↩︎ ↩︎A mutable setter method is a method used to change the value of an object's internal property; it modifies the existing object in place and typically returns
void. ↩︎An immutable setter method is a method that does not modify the current object, but instead creates and returns a new instance of it with an updated value. ↩︎
Fully Qualified Class Name – the full name of a class or interface, including its namespace. ↩︎ ↩︎
The
callabletype represents values or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎Type hints – the specification of an expected data type for method parameters, such as the name of a specific class or interface. ↩︎