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
11 changes: 11 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: "Continuous Integration"

on:
pull_request:
push:
branches:
tags:

jobs:
ci:
uses: laminas/workflow-continuous-integration/.github/workflows/continuous-integration.yml@1.x
46 changes: 0 additions & 46 deletions .github/workflows/cs-tests.yml

This file was deleted.

46 changes: 0 additions & 46 deletions .github/workflows/static-analysis.yml

This file was deleted.

47 changes: 0 additions & 47 deletions .github/workflows/unit-test.yml

This file was deleted.

86 changes: 39 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,102 +17,94 @@ This package can clean up your code, by getting rid of all the factories you wri

[![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


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,


## Usage

### Using the AttributedServiceFactory

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

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

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

### NOTE

> You can use only the fully qualified class name as the service key

The next step is to add the `#[Inject]` attribute to the service constructor with the service FQCNs to inject:

```php
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
) {
}
```
#[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.

To inject an array value from the service manager, you can use dot notation as below

```php
use Dot\AnnotatedServices\Attribute\Inject;

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

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

### NOTE

### NOTE
> Even if using dot notation, `AttributedServiceFactory` will check first if a service name exists with that name.

### Using the AttributedRepositoryFactory

### Using the AttributedRepositoryFactory
You can register doctrine repositories and inject them using the `AttributedRepositoryFactory` as below:
```php
return [
'factories' => [
ExampleRepository::class => AttributedRepositoryFactory::class,
],
];
```

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`.
```php
use Api\App\Entity\Example;
use Doctrine\ORM\EntityRepository;
use Dot\AnnotatedServices\Attribute\Entity;

#[Entity(name: Example::class)]
class ExampleRepository extends EntityRepository
{
}
```
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`:

- 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)
- dependencies injected via the`#[Entity]`/`#[Inject]` attributes are not cached
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"require": {
"php": "~8.2.0 || ~8.3.0",
"doctrine/orm": "^2.0 || ^3.0",
"doctrine/orm": "^2.9 || ^3.0",
"psr/container": "^1.0 || ^2.0"
},
"require-dev": {
Expand Down
3 changes: 2 additions & 1 deletion docs/book/v4/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

`dot-annotated-services` reads class annotations using [doctrine/annotations](https://github.com/doctrine/annotations) and caches them using [doctrine/cache](https://github.com/doctrine/cache).


## Configuration

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.
Expand All @@ -19,7 +18,9 @@ You can add this configuration values to your application's Doctrine config file
],
];
```

where `AnnotationsCacheFactory` is a custom factory that needs to return a [Doctrine Cache Driver](https://github.com/doctrine/cache/tree/1.13.x/lib/Doctrine/Common/Cache):

```php
<?php

Expand Down
1 change: 0 additions & 1 deletion docs/book/v4/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
After installation, register `dot-annotated-services` in your project by adding the below line to your configuration aggregator (usually: `config/config.php`):

Dot\AnnotatedServices\ConfigProvider::class,

7 changes: 3 additions & 4 deletions docs/book/v4/factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

Injects entity repositories into a class.


### Exceptions thrown

- `Dot\AnnotatedServices\Exception\RuntimeException` if repository does not exist
- `Dot\AnnotatedServices\Exception\RuntimeException` if repository does not extend `Doctrine\ORM\EntityRepository`
- `Dot\AnnotatedServices\Exception\RuntimeException` if repository does not have `@Entity` annotation
- `Psr\Container\NotFoundExceptionInterface` if `Doctrine\ORM\EntityManagerInterface` does not exist in the service container
- `Psr\Container\ContainerExceptionInterface` if service manager is unable to provide an instance of `Doctrine\ORM\EntityManagerInterface`


## AttributedServiceFactory

Injects class dependencies into classes.
Expand All @@ -24,15 +23,14 @@ If it does not find one, it will try to load the dependency as a config tree, ch

You can use the inject annotation on setters too, they will be called at creation time and injected with the configured dependencies.


### Exceptions thrown

- `Dot\AnnotatedServices\Exception\RuntimeException` if service does not exist
- `Dot\AnnotatedServices\Exception\RuntimeException` if service does not have `@Inject` annotation on it's constructor
- `ReflectionException` on failure of creating a ReflectionClass of the dependency
- `Psr\Container\NotFoundExceptionInterface` if a dependency does not exist in the service container
- `Psr\Container\ContainerExceptionInterface` if service manager is unable to provide an instance of a dependency


## AnnotatedServiceAbstractFactory

Using this approach, no service manager configuration is required. It uses the registered abstract factory to create annotated services.
Expand Down Expand Up @@ -66,4 +64,5 @@ class Example
}
}
```

And that's it, you don't need to configure the service manager with this class, creation will happen automatically.
1 change: 1 addition & 0 deletions docs/book/v4/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
Version `4.x` of `dot-annotated-services` is the last version that uses [doctrine/annotations](https://github.com/doctrine/annotations) to parse and inject dependencies.

You can use it to:

- [Inject class dependencies](factories/service.md)
- [Inject entity repositories](factories/repository.md)
1 change: 0 additions & 1 deletion docs/book/v5/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
After installation, register `dot-annotated-services` in your project by adding the below line to your configuration aggregator (usually: `config/config.php`):

Dot\AnnotatedServices\ConfigProvider::class,

Loading