The documentation you are viewing is for an older version of this component.
Switch to the latest (v4) version.
dot-annotated-services
determines the dependencies by looking at the #[Inject]
attribute, added to the constructor of a class.
Dependencies are specified as separate parameters of the #[Inject] attribute.
<?php
declare(strict_types=1);
namespace YourApp\Service;
class Example
{
#[Dot\AnnotatedServices\Attribute\Inject(
YourApp\Repository\Dependency1::class,
YourApp\Helper\Dependency2::class,
"config",
)]
public function __construct(
protected YourApp\Repository\Dependency1 $dependency1,
protected YourApp\Helper\Dependency2 $dependency2,
protected array $config
) {
}
}
If your class needs the value of a specific configuration key, you can specify the path using dot notation:
#[Dot\AnnotatedServices\Attribute\Inject(
YourApp\Repository\Dependency1::class,
YourApp\Helper\Dependency2::class,
"config.example",
)]
public function __construct(
protected YourApp\Repository\Dependency1 $dependency1,
protected YourApp\Helper\Dependency2 $dependency2,
protected array $exampleConfig,
) {
}
Open the ConfigProvider of the module where your class resides.
Add a new entry under factories
, where the key is your class FQCN and the value is Dot\AnnotatedServices\Factory\AttributedServiceFactory::class
.
See below example for a better understanding of the file structure.
<?php
declare(strict_types=1);
namespace YourApp;
class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
];
}
public function getDependencies(): array
{
return [
'factories' => [
YourApp\Service\Example::class => Dot\AnnotatedServices\Factory\AttributedServiceFactory::class,
],
];
}
}