You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DotKernel component used to create services through [Laminas Service Manager](https://github.com/laminas/laminas-servicemanager) 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.
3
+
DotKernel dependency injection service.
4
4
5
5
This package can clean up your code, by getting rid of all the factories you write, sometimes just to inject a dependency or two.
Run the following command in your project directory
23
+
Install `dot-annotated-services` by running the following command in your project directory:
24
24
25
25
composer require dotkernel/dot-annotated-services
26
26
27
27
28
-
After installing, add the `ConfigProvider` class to your configuration aggregate.
28
+
After installing, register `dot-annotated-services` in your project by adding the below line to your configuration aggregate (usually: `config/config.php`):
29
+
30
+
Dot\AnnotatedServices\ConfigProvider::class,
31
+
29
32
30
33
## Usage
31
34
32
35
### Using the AnnotatedServiceFactory
33
36
34
-
You can register services in the service manager using the `AnnotatedServiceFactory` as below
37
+
You can register services in the service manager using `AnnotatedServiceFactory` as seen in the below example:
38
+
35
39
```php
36
40
return [
37
41
'factories' => [
@@ -40,20 +44,20 @@ return [
40
44
];
41
45
```
42
46
47
+
43
48
### NOTE
44
49
> You can use only the fully qualified class name as the service key
45
50
46
-
The next step is to annotate the service constructor or setters with the service names to inject
51
+
The next step is to add the `#[Inject]` attribute to the service constructor with the service FQCNs to inject:
52
+
47
53
```php
48
-
use Dot\AnnotatedServices\Annotation\Inject;
49
-
50
-
/**
51
-
* @Inject({
52
-
* Dependency1::class,
53
-
* Dependency2::class,
54
-
* "config"
55
-
* })
56
-
*/
54
+
use Dot\AnnotatedServices\Attribute\Inject;
55
+
56
+
#[Inject(
57
+
Dependency1::class,
58
+
Dependency2::class,
59
+
"config",
60
+
)]
57
61
public function __construct(
58
62
protected Dependency1 $dep1,
59
63
protected Dependency2 $dep2,
@@ -62,105 +66,54 @@ public function __construct(
62
66
}
63
67
```
64
68
65
-
The annotation `@Inject`is telling the factory to inject the services between curly braces.
69
+
The `#[Inject]` attribute is telling `AnnotatedServiceFactory`to inject the services specified as parameters.
66
70
Valid service names should be provided, as registered in the service manager.
67
71
68
72
To inject an array value from the service manager, you can use dot notation as below
73
+
69
74
```php
70
-
use Dot\AnnotatedServices\Annotation\Inject;
75
+
use Dot\AnnotatedServices\Attribute\Inject;
71
76
72
-
/**
73
-
* @Inject({"config.debug"})
74
-
*/
77
+
#[Inject(
78
+
"config.debug",
79
+
)]
75
80
```
81
+
which will inject `$container->get('config')['debug'];`.
76
82
77
-
which will inject `$container->get('config')['debug'];`
78
83
79
84
### NOTE
80
-
> Even if using dot annotation, the annotated factory will check first if a service name exists with that name
85
+
> Even if using dot notation, `AnnotatedServiceFactory`will check first if a service name exists with that name.
81
86
82
-
You can use the inject annotation on setters too, they will be called at creation time and injected with the configured dependencies.
83
87
84
-
### Using the AnnotatedRepositoryFactory
85
-
You can register doctrine repositories and inject them using the AnnotatedRepositoryFactory as below:
88
+
### Using the AttributedRepositoryFactory
89
+
You can register doctrine repositories and inject them using the `AttributedRepositoryFactory` as below:
The next step is to add the `@Entity` annotation in the repository class.
98
+
The next step is to add the `#[Entity]` attribute in the repository class.
95
99
96
100
The `name` field has to be the fully qualified class name.
97
101
98
102
Every repository should extend `Doctrine\ORM\EntityRepository`.
99
103
```php
104
+
use Api\App\Entity\Example;
100
105
use Doctrine\ORM\EntityRepository;
101
-
use Dot\AnnotatedServices\Annotation\Entity;
106
+
use Dot\AnnotatedServices\Attribute\Entity;
102
107
103
-
/**
104
-
* @Entity(name="App\Entity\Example")
105
-
*/
108
+
#[Entity(name: Example::class)]
106
109
class ExampleRepository extends EntityRepository
107
110
{
108
-
109
-
}
110
-
```
111
-
112
-
113
-
### Using the abstract factory
114
-
115
-
Using this approach, no service manager configuration is required. It uses the registered abstract factory to create annotated services.
116
-
117
-
In order to tell the abstract factory which services are to be created, you need to annotate the service class with the `@Service` annotation.
118
-
```php
119
-
use Dot\AnnotatedServices\Annotation\Service;
120
-
121
-
/*
122
-
* @Service
123
-
*/
124
-
class ServiceClass
125
-
{
126
-
// configure injections as described in the previous section
127
111
}
128
112
```
129
113
130
-
And that's it, you don't need to configure the service manager with this class, creation will happen automatically.
131
-
132
-
133
-
## Cache annotations
134
-
135
-
This package is built on top of `doctrine/annotation` and `doctrine/cache`.
136
-
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](https://github.com/doctrine/cache/tree/master/lib/Doctrine/Common/Cache) for available implementations offered by doctrine.
137
-
138
-
Below, we give an example, as defined in our frontend and admin starter applications
0 commit comments