Skip to content

Commit 69ef3f7

Browse files
authored
Merge pull request #36 from dotkernel/issue-32
Issue #32: Replaced annotations with attributes.
2 parents 1de7ba3 + 4589ffd commit 69ef3f7

34 files changed

Lines changed: 729 additions & 688 deletions

.github/workflows/codecov.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ jobs:
1515
- ubuntu-latest
1616

1717
php:
18-
- "8.1"
1918
- "8.2"
2019
- "8.3"
2120

.github/workflows/cs-tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ jobs:
1515
- ubuntu-latest
1616

1717
php:
18-
- "8.1"
1918
- "8.2"
2019
- "8.3"
2120

.github/workflows/static-analysis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ jobs:
1515
- ubuntu-latest
1616

1717
php:
18-
- "8.1"
1918
- "8.2"
2019
- "8.3"
2120

.github/workflows/unit-test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ jobs:
1515
- ubuntu-latest
1616

1717
php:
18-
- "8.1"
1918
- "8.2"
2019
- "8.3"
2120

OSSMETADATA

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
osslifecycle=active
1+
osslifecycle=active

README.md

Lines changed: 42 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
# dot-annotated-services
22

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 dependency injection service.
44

55
This package can clean up your code, by getting rid of all the factories you write, sometimes just to inject a dependency or two.
66

77
![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-annotated-services)
8-
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/4.1.7)
8+
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/5.0.0)
99

1010
[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-annotated-services)](https://github.com/dotkernel/dot-annotated-services/issues)
1111
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-annotated-services)](https://github.com/dotkernel/dot-annotated-services/network)
1212
[![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-annotated-services)](https://github.com/dotkernel/dot-annotated-services/stargazers)
13-
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-annotated-services)](https://github.com/dotkernel/dot-annotated-services/blob/4.0/LICENSE.md)
13+
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-annotated-services)](https://github.com/dotkernel/dot-annotated-services/blob/5.0/LICENSE.md)
1414

15-
[![Build Static](https://github.com/dotkernel/dot-annotated-services/actions/workflows/static-analysis.yml/badge.svg?branch=4.0)](https://github.com/dotkernel/dot-annotated-services/actions/workflows/static-analysis.yml)
15+
[![Build Static](https://github.com/dotkernel/dot-annotated-services/actions/workflows/static-analysis.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/dot-annotated-services/actions/workflows/static-analysis.yml)
1616
[![codecov](https://codecov.io/gh/dotkernel/dot-annotated-services/graph/badge.svg?token=ZBZDEA3LY8)](https://codecov.io/gh/dotkernel/dot-annotated-services)
1717

1818
[![SymfonyInsight](https://insight.symfony.com/projects/a0d7016e-fc3f-46b8-9b36-571ff060d744/big.svg)](https://insight.symfony.com/projects/a0d7016e-fc3f-46b8-9b36-571ff060d744)
1919

2020

2121
## Installation
2222

23-
Run the following command in your project directory
23+
Install `dot-annotated-services` by running the following command in your project directory:
2424

2525
composer require dotkernel/dot-annotated-services
2626

2727

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+
2932

3033
## Usage
3134

3235
### Using the AnnotatedServiceFactory
3336

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+
3539
```php
3640
return [
3741
'factories' => [
@@ -40,20 +44,20 @@ return [
4044
];
4145
```
4246

47+
4348
### NOTE
4449
> You can use only the fully qualified class name as the service key
4550
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+
4753
```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+
)]
5761
public function __construct(
5862
protected Dependency1 $dep1,
5963
protected Dependency2 $dep2,
@@ -62,105 +66,54 @@ public function __construct(
6266
}
6367
```
6468

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.
6670
Valid service names should be provided, as registered in the service manager.
6771

6872
To inject an array value from the service manager, you can use dot notation as below
73+
6974
```php
70-
use Dot\AnnotatedServices\Annotation\Inject;
75+
use Dot\AnnotatedServices\Attribute\Inject;
7176

72-
/**
73-
* @Inject({"config.debug"})
74-
*/
77+
#[Inject(
78+
"config.debug",
79+
)]
7580
```
81+
which will inject `$container->get('config')['debug'];`.
7682

77-
which will inject `$container->get('config')['debug'];`
7883

7984
### 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.
8186
82-
You can use the inject annotation on setters too, they will be called at creation time and injected with the configured dependencies.
8387

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:
8690
```php
8791
return [
8892
'factories' => [
89-
ExampleRepository::class => AnnotatedRepositoryFactory::class,
93+
ExampleRepository::class => AttributedRepositoryFactory::class,
9094
],
9195
];
9296
```
9397

94-
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.
9599

96100
The `name` field has to be the fully qualified class name.
97101

98102
Every repository should extend `Doctrine\ORM\EntityRepository`.
99103
```php
104+
use Api\App\Entity\Example;
100105
use Doctrine\ORM\EntityRepository;
101-
use Dot\AnnotatedServices\Annotation\Entity;
106+
use Dot\AnnotatedServices\Attribute\Entity;
102107

103-
/**
104-
* @Entity(name="App\Entity\Example")
105-
*/
108+
#[Entity(name: Example::class)]
106109
class ExampleRepository extends EntityRepository
107110
{
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
127111
}
128112
```
129113

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
139-
```php
140-
return [
141-
'annotations_cache_dir' => __DIR__ . '/../../data/cache/annotations',
142-
'dependencies' => [
143-
'factories' => [
144-
// used by dot-annotated-services to cache annotations
145-
// needs to return a cache instance from Doctrine\Common\Cache
146-
AbstractAnnotatedFactory::CACHE_SERVICE => AnnotationsCacheFactory::class,
147-
]
148-
],
149-
];
150-
```
151-
152-
```php
153-
namespace Frontend\App\Factory;
154-
155-
use Doctrine\Common\Cache\FilesystemCache;
156-
use Psr\Container\ContainerInterface;
157-
158-
class AnnotationsCacheFactory
159-
{
160-
public function __invoke(ContainerInterface $container)
161-
{
162-
//change this to suite your caching needs
163-
return new FilesystemCache($container->get('config')['annotations_cache_dir']);
164-
}
165-
}
166-
```
114+
### NOTE
115+
Starting from version `5.0` of `dot-annotated-services`:
116+
- services can only be injected using the `#[Inject]` attribute (`@Inject` and `@Service` annotations are no longer supported)
117+
- repository-entity relation can only be established using the `#[Entity]` attribute (`@Entity` annotation is no longer supported)
118+
- dependencies injected via the`#[Entity]`/`#[Inject]` attributes are not cached
119+
- injecting dependencies into property setters is no longer supported

SECURITY.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Security Policy
2+
3+
## Supported Versions
4+
5+
6+
| Version | Supported | PHP Version |
7+
|---------|--------------------|------------------------------------------------------------------------------------------------------------------------|
8+
| 5.x | :white_check_mark: | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/5.0.0) |
9+
| 4.x | :white_check_mark: | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/4.0.0) |
10+
| <= 3.x | :x: | |
11+
12+
13+
## Reporting Potential Security Issues
14+
15+
If you have encountered a potential security vulnerability in this project,
16+
please report it to us at <security@dotkernel.com>. We will work with you to
17+
verify the vulnerability and patch it.
18+
19+
When reporting issues, please provide the following information:
20+
21+
- Component(s) affected
22+
- A description indicating how to reproduce the issue
23+
- A summary of the security vulnerability and impact
24+
25+
We request that you contact us via the email address above and give the
26+
project contributors a chance to resolve the vulnerability and issue a new
27+
release prior to any public exposure; this helps protect the project's
28+
users, and provides them with a chance to upgrade and/or update in order to
29+
protect their applications.
30+
31+
32+
## Policy
33+
34+
If we verify a reported security vulnerability, our policy is:
35+
36+
- We will patch the current release branch, as well as the immediate prior minor
37+
release branch.
38+
39+
- After patching the release branches, we will immediately issue new security
40+
fix releases for each patched release branch.
41+

composer.json

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "dotkernel/dot-annotated-services",
33
"type": "library",
4-
"description": "DotKernel service creation component through laminas-servicemanager and annotations",
4+
"description": "DotKernel dependency injection component using class attributes.",
55
"license": "MIT",
66
"homepage": "https://github.com/dotkernel/dot-annotated-services",
77
"authors": [
@@ -11,23 +11,21 @@
1111
}
1212
],
1313
"keywords": [
14-
"annotations",
15-
"services",
16-
"factories",
14+
"attribute",
1715
"container",
18-
"laminas",
19-
"mezzio",
20-
"service-manager"
16+
"dependency",
17+
"di",
18+
"factory",
19+
"inject",
20+
"service"
2121
],
2222
"require": {
23-
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
24-
"laminas/laminas-servicemanager": "^3.22.1",
25-
"doctrine/annotations": "^1.14.3",
26-
"doctrine/cache": "^1.12.1 || ^2.1.1",
27-
"doctrine/orm" : "^2.17.3"
23+
"php": "~8.2.0 || ~8.3.0",
24+
"doctrine/orm": "^2.0 || ^3.0",
25+
"psr/container": "^1.0 || ^2.0"
2826
},
2927
"require-dev": {
30-
"phpunit/phpunit": "^10.5.9",
28+
"phpunit/phpunit": "^10.5",
3129
"vimeo/psalm": "^5.20",
3230
"laminas/laminas-coding-standard": "^2.5"
3331
},

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<testsuites>
44
<testsuite name="dot-annotated-services Test Suite">
55
<directory>./test</directory>
6+
<exclude>./test/TestData</exclude>
67
</testsuite>
78
</testsuites>
89
<coverage/>

psalm-baseline.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<files psalm-version="5.22.2@d768d914152dbbf3486c36398802f74e80cfde48">
3+
<file src="test/Factory/AttributedRepositoryFactoryTest.php">
4+
<MissingTemplateParam>
5+
<code>class ($entityManager, $metadata) extends EntityRepository {</code>
6+
</MissingTemplateParam>
7+
</file>
8+
</files>

0 commit comments

Comments
 (0)