Download the PHP package dotkernel/dot-annotated-services without Composer
On this page you can find all versions of the php package dotkernel/dot-annotated-services. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dotkernel/dot-annotated-services
More information about dotkernel/dot-annotated-services
Files in dotkernel/dot-annotated-services
Package dot-annotated-services
Short Description DotKernel dependency injection component using class attributes.
License MIT
Homepage https://github.com/dotkernel/dot-annotated-services
Informations about the package dot-annotated-services
dot-annotated-services
Dotkernel component used to create services through Laminas Service Manager and inject them with dependencies just using method annotations. It can also create services without the need to write factories. Annotation parsing can be cached, to improve performance.
This package can clean up your code, by getting rid of all the factories you write, sometimes just to inject a dependency or two.
Installation
Run the following command in your project directory
After installing, add the ConfigProvider
class to your configuration aggregate.
Usage
Using the AnnotatedServiceFactory
You can register services in the service manager using the AnnotatedServiceFactory
as below.
You can use only the fully qualified class name as the service key.
The next step is to annotate the service constructor or setters with the service names to inject.
The annotation @Inject
is telling the factory to inject the services between curly braces.
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,
which will inject $container->get('config')['debug'];
Even if using dot annotation, the annotated factory will check first if a service name exists with that name.
You can use the inject annotation on setters too, they will be called at creation time and injected with the configured dependencies.
Using the AnnotatedRepositoryFactory
You can register doctrine repositories and inject them using the AnnotatedRepositoryFactory as below.
The next step is to add the @Entity
annotation in the repository class.
The name
field has to be the fully qualified class name.
Every repository should extend Doctrine\ORM\EntityRepository
.
Using the abstract factory
Using this approach, no service manager configuration is required. It uses the registered abstract factory to create annotated services.
In order to tell the abstract factory which services are to be created, you need to annotate the service class with the @Service
annotation.
And that's it, you don't need to configure the service manager with this class, creation will happen automatically.
Cache annotations
This package is built on top of doctrine/annotation
and doctrine/cache
.
In order to cache annotations, you should register a service factory at key AbstractAnnotatedFactory::CACHE_SERVICE
that should return a valid Doctrine\Common\Cache\Cache
cache driver.
See Cache Drivers for available implementations offered by doctrine.
Below, we give an example, as defined in our frontend and admin starter applications:
All versions of dot-annotated-services with dependencies
doctrine/orm Version ^2.9 || ^3.0
psr/container Version ^1.0 || ^2.0