dot-dependency-injection is based on two reusable factories - AttributedServiceFactory and AttributedRepositoryFactory - able to inject any dependency into a class.
Both are plain invokable factories, so they work with any PSR-11 container that passes the requested service name to the factory (for example laminas/laminas-servicemanager):
$factory = new Dot\DependencyInjection\Factory\AttributedServiceFactory();
$service = $factory($container, YourApp\Service\Example::class);
Both expose the same logic through createObject(ContainerInterface $container, string $requestedName), which __invoke() simply delegates to.
Use createObject() when you extend one of the factories and need to call the parent implementation explicitly.
Because the service name is also the class to instantiate, you can only register these factories under the fully qualified class name of the service. Aliases still work, as long as the alias points to a service registered under its FQCN.
Injects class dependencies into classes.
The factory looks for the #[Inject] attribute on the constructor of the requested class and resolves each of its parameters, in the declared order, into a constructor argument.
Resolution of a single dependency happens as follows:
ArrayAccess object)new $name()RuntimeException is thrownA class whose constructor has no parameters at all does not need the #[Inject] attribute: if the requested class has no constructor, it is instantiated directly.
Dot\DependencyInjection\Exception\RuntimeException if the requested class does not existDot\DependencyInjection\Exception\RuntimeException if the requested class has a constructor without the #[Inject] attributeDot\DependencyInjection\Exception\RuntimeException if the class tries to inject itselfDot\DependencyInjection\Exception\RuntimeException if a dependency is neither a registered service nor an existing classDot\DependencyInjection\Exception\InvalidArgumentException if a key of a dot-separated dependency cannot be found in the array servicePsr\Container\NotFoundExceptionInterface if a dependency does not exist in the service containerPsr\Container\ContainerExceptionInterface if the service manager is unable to provide an instance of a dependencyInjects entity repositories into a class.
The factory looks for the #[Entity] attribute on the requested repository class and returns $container->get(EntityManagerInterface::class)->getRepository($entityName).
Since Doctrine decides which repository class to build from the mapping of the entity, the entity referenced by the #[Entity] attribute must declare the repository back:
#[ORM\Entity(repositoryClass: ExampleRepository::class)]
If it does not, Doctrine returns its default repository and the factory throws a RuntimeException instead of handing back an object of an unexpected type.
Dot\DependencyInjection\Exception\RuntimeException if the repository class does not existDot\DependencyInjection\Exception\RuntimeException if the repository class does not extend Doctrine\ORM\EntityRepositoryDot\DependencyInjection\Exception\RuntimeException if the repository class does not have the #[Entity] attributeDot\DependencyInjection\Exception\RuntimeException if the class referenced by the #[Entity] attribute does not existDot\DependencyInjection\Exception\RuntimeException if Doctrine returns a repository that is not an instance of the requested classPsr\Container\NotFoundExceptionInterface if Doctrine\ORM\EntityManagerInterface does not exist in the service containerPsr\Container\ContainerExceptionInterface if the service manager is unable to provide an instance of Doctrine\ORM\EntityManagerInterfaceBoth exceptions implement Dot\DependencyInjection\Exception\ExceptionInterface, so you can catch anything thrown by this package with a single catch block:
try {
$service = $container->get(YourApp\Service\Example::class);
} catch (Dot\DependencyInjection\Exception\ExceptionInterface $exception) {
// ...
}
| Exception | Extends | Meaning |
|---|---|---|
RuntimeException |
\RuntimeException |
the class or service graph cannot be built as declared |
InvalidArgumentException |
\InvalidArgumentException |
a dot-separated dependency points to a missing array key |