|
| 1 | +# Factories |
| 2 | + |
| 3 | +`dot-annotated-services` is based on 3 reusable factories - `AnnotatedRepositoryFactory`, `AnnotatedServiceFactory` and `AnnotatedServiceAbstractFactory` - able to inject any dependency into a class. |
| 4 | + |
| 5 | +## AttributedRepositoryFactory |
| 6 | + |
| 7 | +Injects entity repositories into a class. |
| 8 | + |
| 9 | + |
| 10 | +### Exceptions thrown |
| 11 | +- `Dot\AnnotatedServices\Exception\RuntimeException` if repository does not exist |
| 12 | +- `Dot\AnnotatedServices\Exception\RuntimeException` if repository does not extend `Doctrine\ORM\EntityRepository` |
| 13 | +- `Dot\AnnotatedServices\Exception\RuntimeException` if repository does not have `@Entity` annotation |
| 14 | +- `Psr\Container\NotFoundExceptionInterface` if `Doctrine\ORM\EntityManagerInterface` does not exist in the service container |
| 15 | +- `Psr\Container\ContainerExceptionInterface` if service manager is unable to provide an instance of `Doctrine\ORM\EntityManagerInterface` |
| 16 | + |
| 17 | + |
| 18 | +## AttributedServiceFactory |
| 19 | + |
| 20 | +Injects class dependencies into classes. |
| 21 | + |
| 22 | +If a dependency is specified using the dot notation, `AttributedServiceFactory` will try to load a service having that specific alias. |
| 23 | +If it does not find one, it will try to load the dependency as a config tree, checking each segment if it's available in the service container. |
| 24 | + |
| 25 | +You can use the inject annotation on setters too, they will be called at creation time and injected with the configured dependencies. |
| 26 | + |
| 27 | + |
| 28 | +### Exceptions thrown |
| 29 | +- `Dot\AnnotatedServices\Exception\RuntimeException` if service does not exist |
| 30 | +- `Dot\AnnotatedServices\Exception\RuntimeException` if service does not have `@Inject` annotation on it's constructor |
| 31 | +- `ReflectionException` on failure of creating a ReflectionClass of the dependency |
| 32 | +- `Psr\Container\NotFoundExceptionInterface` if a dependency does not exist in the service container |
| 33 | +- `Psr\Container\ContainerExceptionInterface` if service manager is unable to provide an instance of a dependency |
| 34 | + |
| 35 | + |
| 36 | +## AnnotatedServiceAbstractFactory |
| 37 | + |
| 38 | +Using this approach, no service manager configuration is required. It uses the registered abstract factory to create annotated services. |
| 39 | + |
| 40 | +In order to tell the abstract factory which services are to be created, you need to annotate the service class with the `@Service` annotation. |
| 41 | + |
| 42 | +```php |
| 43 | +<?php |
| 44 | + |
| 45 | +declare(strict_types=1); |
| 46 | + |
| 47 | +namespace YourApp\Service; |
| 48 | + |
| 49 | +/** |
| 50 | + * @Dot\AnnotatedServices\Annotation\Service |
| 51 | + */ |
| 52 | +class Example |
| 53 | +{ |
| 54 | + /** |
| 55 | + * @Dot\AnnotatedServices\Annotation\Inject({ |
| 56 | + * YourApp\Repository\Dependency1::class, |
| 57 | + * YourApp\Repository\Dependency2::class, |
| 58 | + * "config.example" |
| 59 | + * }) |
| 60 | + */ |
| 61 | + public function __construct( |
| 62 | + protected YourApp\Repository\Dependency1 $dependency1, |
| 63 | + protected YourApp\Helper\Dependency2 $dependency2, |
| 64 | + protected array $exampleConfig, |
| 65 | + ) { |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | +And that's it, you don't need to configure the service manager with this class, creation will happen automatically. |
0 commit comments