Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .laminas-ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ignore_php_platform_requirements": {
"8.4": true
},
"backwardCompatibilityCheck": true
}
2 changes: 1 addition & 1 deletion OSSMETADATA
Original file line number Diff line number Diff line change
@@ -1 +1 @@
osslifecycle=active
osslifecycle=maintenance
90 changes: 51 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ DotKernel dependency injection service.

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

![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-annotated-services)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/5.1.0)
![OSS Lifecycle](https://img.shields.io/osslifecycle?file_url=https%3A%2F%2Fgithub.com%2Fdotkernel%2Fdot-annotated-services%2Fblob%2F5.0%2FOSSMETADATA)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/5.2.0)

[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-annotated-services)](https://github.com/dotkernel/dot-annotated-services/issues)
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-annotated-services)](https://github.com/dotkernel/dot-annotated-services/network)
Expand All @@ -16,29 +16,33 @@ This package can clean up your code, by getting rid of all the factories you wri
[![codecov](https://codecov.io/gh/dotkernel/dot-annotated-services/graph/badge.svg?token=ZBZDEA3LY8)](https://codecov.io/gh/dotkernel/dot-annotated-services)
[![docs-build](https://github.com/dotkernel/dot-annotated-services/actions/workflows/docs-build.yml/badge.svg)](https://github.com/dotkernel/dot-annotated-services/actions/workflows/docs-build.yml)

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

## Installation

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

composer require dotkernel/dot-annotated-services
```shell
composer require dotkernel/dot-annotated-services
```

After installing, register `dot-annotated-services` in your project by adding the below line to your configuration aggregate (usually: `config/config.php`):

Dot\AnnotatedServices\ConfigProvider::class,
```php
Dot\AnnotatedServices\ConfigProvider::class,
```

## Usage

### Using the AttributedServiceFactory

You can register services in the service manager using `AttributedServiceFactory` as seen in the below example:

return [
'factories' => [
ServiceClass::class => AttributedServiceFactory::class,
],
];
```php
return [
'factories' => [
ServiceClass::class => AttributedServiceFactory::class,
],
];
```

### NOTE

Expand All @@ -48,17 +52,19 @@ The next step is to add the `#[Inject]` attribute to the service constructor wit

use Dot\AnnotatedServices\Attribute\Inject;

#[Inject(
App\Srevice\Dependency1::class,
App\Srevice\Dependency2::class,
"config",
)]
public function __construct(
protected App\Srevice\Dependency1 $dep1,
protected App\Srevice\Dependency2 $dep2,
protected array $config
) {
}
```php
#[Inject(
App\Srevice\Dependency1::class,
App\Srevice\Dependency2::class,
"config",
)]
public function __construct(
protected App\Srevice\Dependency1 $dep1,
protected App\Srevice\Dependency2 $dep2,
protected array $config
) {
}
```

The `#[Inject]` attribute is telling `AttributedServiceFactory` to inject the services specified as parameters.
Valid service names should be provided, as registered in the service manager.
Expand All @@ -67,9 +73,11 @@ To inject an array value from the service manager, you can use dot notation as b

use Dot\AnnotatedServices\Attribute\Inject;

#[Inject(
"config.debug",
)]
```php
#[Inject(
"config.debug",
)]
```

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

Expand All @@ -81,30 +89,34 @@ which will inject `$container->get('config')['debug'];`.

You can register doctrine repositories and inject them using the `AttributedRepositoryFactory` as below:

return [
'factories' => [
ExampleRepository::class => AttributedRepositoryFactory::class,
],
];
```php
return [
'factories' => [
ExampleRepository::class => AttributedRepositoryFactory::class,
],
];
```

The next step is to add the `#[Entity]` attribute in the repository class.

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

Every repository should extend `Doctrine\ORM\EntityRepository`.

use Api\App\Entity\Example;
use Doctrine\ORM\EntityRepository;
use Dot\AnnotatedServices\Attribute\Entity;

#[Entity(name: Example::class)]
class ExampleRepository extends EntityRepository
{
}
```php
use Api\App\Entity\Example;
use Doctrine\ORM\EntityRepository;
use Dot\AnnotatedServices\Attribute\Entity;

#[Entity(name: Example::class)]
class ExampleRepository extends EntityRepository
{
}
```

### NOTE

Starting from version `5.0` of `dot-annotated-services`:
In version `5.0` of `dot-annotated-services`:

- services can only be injected using the `#[Inject]` attribute (`@Inject` and `@Service` annotations are no longer supported)
- repository-entity relation can only be established using the `#[Entity]` attribute (`@Entity` annotation is no longer supported)
Expand Down
4 changes: 2 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

| Version | Supported | PHP Version |
|---------|--------------------|------------------------------------------------------------------------------------------------------------------------|
| 5.x | :white_check_mark: | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/5.0.0) |
| 4.x | :white_check_mark: | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/4.1.7) |
| 5.x | :white_check_mark: | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/5.2.0) |
| 4.x | :white_check_mark: | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-annotated-services/4.2.0) |
| <= 3.x | :x: | |


Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
"service"
],
"require": {
"php": "~8.2.0 || ~8.3.0",
"php": "~8.2.0 || ~8.3.0 || ~8.4.0",
"doctrine/orm": "^2.9 || ^3.0",
"psr/container": "^1.0 || ^2.0"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
"vimeo/psalm": "^5.20",
"laminas/laminas-coding-standard": "^2.5"
"laminas/laminas-coding-standard": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion docs/book/index.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/book/index.md
2 changes: 0 additions & 2 deletions docs/book/v4/factories/repository.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Inject entity repositories


## Prepare repository

`dot-annotated-services` determines the entity a repository is related to by looking at the `@Entity` annotation, added to the repository class.
Expand All @@ -22,7 +21,6 @@ class ExampleRepository extends Doctrine\ORM\EntityRepository

Each entity repository must extend `Doctrine\ORM\EntityRepository`.


## Register repository

Open the ConfigProvider of the module where your repository resides.
Expand Down
2 changes: 0 additions & 2 deletions docs/book/v4/factories/service.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Inject class dependencies


## Prepare class

`dot-annotated-services` determines the dependencies by looking at the `@Inject` annotation, added to the constructor of a class.
Expand Down Expand Up @@ -49,7 +48,6 @@ If your class needs the value of a specific configuration key, you can specify t
}
```


## Register class

Open the ConfigProvider of the module where your class resides.
Expand Down
2 changes: 0 additions & 2 deletions docs/book/v5/factories/repository.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Inject entity repositories


## Prepare repository

`dot-annotated-services` determines the entity a repository is related to by looking at the `#[Entity]` attribute, added to the repository class.
Expand All @@ -20,7 +19,6 @@ class ExampleRepository extends Doctrine\ORM\EntityRepository

Each entity repository must extend `Doctrine\ORM\EntityRepository`.


## Register repository

Open the ConfigProvider of the module where your repository resides.
Expand Down
2 changes: 0 additions & 2 deletions docs/book/v5/factories/service.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Inject class dependencies


## Prepare class

`dot-annotated-services` determines the dependencies by looking at the `#[Inject]` attribute, added to the constructor of a class.
Expand Down Expand Up @@ -45,7 +44,6 @@ If your class needs the value of a specific configuration key, you can specify t
}
```


## Register class

Open the ConfigProvider of the module where your class resides.
Expand Down
4 changes: 4 additions & 0 deletions docs/book/v5/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
`dot-annotated-services` is DotKernel's dependency injection service.

By providing reusable factories for service and repository injection, it reduces code complexity in projects.

> `dot-annotated-services` v5 has been abandoned in favor of [dot-dependency-injection](https://github.com/dotkernel/dot-dependency-injection), which uses PHP attributes instead of annotations.
>
> Going forward, if you want to use annotations in your projects, use [dot-annotated-services v4](../v4/overview.md), else use `dot-dependency-injection`.
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ docs_dir: docs/book
site_dir: docs/html
extra:
project: Packages
current_version: v5
current_version: v4
versions:
- v5
- v4
Expand All @@ -28,7 +28,7 @@ nav:
- "Inject class dependencies": v4/factories/service.md
- "Inject entity repositories": v4/factories/repository.md
site_name: dot-annotated-services
site_description: "DotKernel's dependency injection service"
site_description: "DotKernel's dependency injection service, using PHP attributes"
repo_url: "https://github.com/dotkernel/dot-annotated-services"
plugins:
- search