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
}
91 changes: 51 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This package can clean up your code, by getting rid of all the factories you wri
dependency or two.

![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-dependency-injection)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-dependency-injection/1.0.0)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-dependency-injection/1.1.0)

[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-dependency-injection)](https://github.com/dotkernel/dot-dependency-injection/issues)
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-dependency-injection)](https://github.com/dotkernel/dot-dependency-injection/network)
Expand All @@ -17,30 +17,34 @@ dependency or two.
[![codecov](https://codecov.io/gh/dotkernel/dot-dependency-injection/graph/badge.svg?token=DayAoD2Oj6)](https://codecov.io/gh/dotkernel/dot-dependency-injection)
[![docs-build](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/docs-build.yml/badge.svg)](https://github.com/dotkernel/dot-dependency-injection/actions/workflows/docs-build.yml)

[![SymfonyInsight](https://insight.symfony.com/projects/d434b0aa-1f35-4f98-8356-a902f312ba5c/big.svg)](https://insight.symfony.com/projects/d434b0aa-1f35-4f98-8356-a902f312ba5c)

## Installation

Install `dot-dependency-injection` by running the following command in your project directory:

composer require dotkernel/dot-dependency-injection
```shell
composer require dotkernel/dot-dependency-injection
```

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

Dot\DependencyInjection\ConfigProvider::class,
```php
Dot\DependencyInjection\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 @@ -50,17 +54,19 @@ The next step is to add the `#[Inject]` attribute to the service constructor wit

use Dot\DependencyInjection\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 @@ -69,9 +75,11 @@ To inject an array value from the service manager, you can use dot notation as b

use Dot\DependencyInjection\Attribute\Inject;

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

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

Expand All @@ -83,28 +91,31 @@ 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\DependencyInjection\Attribute\Entity;

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

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

> Dependencies injected via the`#[Entity]`/`#[Inject]` attributes are not cached

- dependencies injected via the`#[Entity]`/`#[Inject]` attributes are not cached
- injecting dependencies into property setters are not supported
> Injecting dependencies into property setters are not supported
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,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
Loading