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
Copy file name to clipboardExpand all lines: README.md
+25-23Lines changed: 25 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,36 @@
1
1
# dot-annotated-services
2
2
3
-
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 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.
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
24
21
25
-
composer require dotkernel/dot-annotated-services
26
-
22
+
```shell
23
+
composer require dotkernel/dot-annotated-services
24
+
```
27
25
28
26
After installing, add the `ConfigProvider` class to your configuration aggregate.
29
27
30
28
## Usage
31
29
32
30
### Using the AnnotatedServiceFactory
33
31
34
-
You can register services in the service manager using the `AnnotatedServiceFactory` as below
32
+
You can register services in the service manager using the `AnnotatedServiceFactory` as below.
33
+
35
34
```php
36
35
return [
37
36
'factories' => [
@@ -40,10 +39,10 @@ return [
40
39
];
41
40
```
42
41
43
-
### NOTE
44
-
> You can use only the fully qualified class name as the service key
42
+
> You can use only the fully qualified class name as the service key.
43
+
44
+
The next step is to annotate the service constructor or setters with the service names to inject.
45
45
46
-
The next step is to annotate the service constructor or setters with the service names to inject
47
46
```php
48
47
use Dot\AnnotatedServices\Annotation\Inject;
49
48
@@ -65,7 +64,8 @@ public function __construct(
65
64
The annotation `@Inject` is telling the factory to inject the services between curly braces.
66
65
Valid service names should be provided, as registered in the service manager.
67
66
68
-
To inject an array value from the service manager, you can use dot notation as below
67
+
To inject an array value from the service manager, you can use dot notation as below,
68
+
69
69
```php
70
70
use Dot\AnnotatedServices\Annotation\Inject;
71
71
@@ -76,13 +76,14 @@ use Dot\AnnotatedServices\Annotation\Inject;
76
76
77
77
which will inject `$container->get('config')['debug'];`
78
78
79
-
### NOTE
80
-
> Even if using dot annotation, the annotated factory will check first if a service name exists with that name
79
+
> Even if using dot annotation, the annotated factory will check first if a service name exists with that name.
81
80
82
81
You can use the inject annotation on setters too, they will be called at creation time and injected with the configured dependencies.
83
82
84
-
### Using the AnnotatedRepositoryFactory
85
-
You can register doctrine repositories and inject them using the AnnotatedRepositoryFactory as below:
83
+
### Using the AnnotatedRepositoryFactory
84
+
85
+
You can register doctrine repositories and inject them using the AnnotatedRepositoryFactory as below.
86
+
86
87
```php
87
88
return [
88
89
'factories' => [
@@ -96,6 +97,7 @@ The next step is to add the `@Entity` annotation in the repository class.
96
97
The `name` field has to be the fully qualified class name.
97
98
98
99
Every repository should extend `Doctrine\ORM\EntityRepository`.
100
+
99
101
```php
100
102
use Doctrine\ORM\EntityRepository;
101
103
use Dot\AnnotatedServices\Annotation\Entity;
@@ -105,16 +107,15 @@ use Dot\AnnotatedServices\Annotation\Entity;
105
107
*/
106
108
class ExampleRepository extends EntityRepository
107
109
{
108
-
109
110
}
110
111
```
111
112
112
-
113
113
### Using the abstract factory
114
114
115
115
Using this approach, no service manager configuration is required. It uses the registered abstract factory to create annotated services.
116
116
117
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
+
118
119
```php
119
120
use Dot\AnnotatedServices\Annotation\Service;
120
121
@@ -129,13 +130,14 @@ class ServiceClass
129
130
130
131
And that's it, you don't need to configure the service manager with this class, creation will happen automatically.
131
132
132
-
133
133
## Cache annotations
134
134
135
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.
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.
137
+
See [Cache Drivers](https://github.com/doctrine/cache/tree/master/lib/Doctrine/Common/Cache) for available implementations offered by doctrine.
138
+
139
+
Below, we give an example, as defined in our frontend and admin starter applications:
137
140
138
-
Below, we give an example, as defined in our frontend and admin starter applications
0 commit comments