dotkernel/dot-annotated-services

DotKernel dependency injection component using class attributes.

5.0.0 2024-03-08 10:43 UTC

README

DotKernel dependency injection service.

This package can clean up your code, by getting rid of all the factories you write, sometimes just to inject a dependency or two.

OSS Lifecycle PHP from Packagist (specify version)

GitHub issues GitHub forks GitHub stars GitHub license

Build Static codecov

SymfonyInsight

Installation

Install dot-annotated-services by running the following command in your project directory:

composer require dotkernel/dot-annotated-services

After installing, register dot-annotated-services in your project by adding the below line to your configuration aggregate (usually: config/config.php):

 Dot\AnnotatedServices\ConfigProvider::class,

Usage

Using the AttributedServiceFactory

You can register services in the service manager using AttributedServiceFactory as seen in the below example:

return [
    'factories' => [
        ServiceClass::class => AttributedServiceFactory::class,
    ],
];

NOTE

You can use only the fully qualified class name as the service key

The next step is to add the #[Inject] attribute to the service constructor with the service FQCNs to inject:

use Dot\AnnotatedServices\Attribute\Inject;

#[Inject(
    App\Srevice\Dependency1::class,
    App\Srevice\Dependency2::class,
    "config",
)]
public function __construct(
    protected App\Srevice\Dependency1 $dep1,
    protected App\Srevice\Dependency2 $dep2,
    protected array $config
) {
}

The #[Inject] attribute is telling AttributedServiceFactory to inject the services specified as parameters. Valid service names should be provided, as registered in the service manager.

To inject an array value from the service manager, you can use dot notation as below

use Dot\AnnotatedServices\Attribute\Inject;

#[Inject(
    "config.debug",
)]

which will inject $container->get('config')['debug'];.

NOTE

Even if using dot notation, AttributedServiceFactory will check first if a service name exists with that name.

Using the AttributedRepositoryFactory

You can register doctrine repositories and inject them using the AttributedRepositoryFactory as below:

return [
    'factories' => [
        ExampleRepository::class => AttributedRepositoryFactory::class,
    ],
];

The next step is to add the #[Entity] attribute in the repository class.

The name field has to be the fully qualified class name.

Every repository should extend Doctrine\ORM\EntityRepository.

use Api\App\Entity\Example;
use Doctrine\ORM\EntityRepository;
use Dot\AnnotatedServices\Attribute\Entity;

#[Entity(name: Example::class)]
class ExampleRepository extends EntityRepository
{
}

NOTE

Starting from version 5.0 of dot-annotated-services:

  • services can only be injected using the #[Inject] attribute (@Inject and @Service annotations are no longer supported)
  • repository-entity relation can only be established using the #[Entity] attribute (@Entity annotation is no longer supported)
  • dependencies injected via the#[Entity]/#[Inject] attributes are not cached
  • injecting dependencies into property setters is no longer supported