Skip to content

Commit

Permalink
📝 Improve doc for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- committed Jul 29, 2024
1 parent 728e1de commit 6f22364
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

- New way of injecting the dispatcher to entities. This is great and also supports proxies! (which was previously a limitation)

## [2.3.3] - 2023-05-16

### Added
Expand Down
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ As your model needs a dispatcher you need to call the `setDispatcher()` method a

> It doesn't use the constructor to add the dispatcher because in PHP you can create objects without the constructor. For instance, that's what Doctrine does.
```php
use Biig\Component\Domain\Model\Instantiator\Instantiator;
use Doctrine\ORM\EntityManager;

class SomeController
{
public function index(Instantiator $instantiator, EntityManager $entityManager)
{
$model = $instantiator->instantiate(YourModel::class);
$entityManager->persist($model);
$entityManager->flush();
}
}
```

Integration to Symfony
----------------------

Expand All @@ -98,10 +113,12 @@ Learn more about [Symfony Integration](/docs/domain_event_dispatcher.md#symfony-
Versions
--------

| Version | Status | Documentation | Symfony Version | PHP Version |
|---------|------------|---------------| --------------- | ------------|
| 1.x | Maintained | [v1][v1-doc] | '>= 3.3 && <5' | '>= 7.1' |
| 2.x | Latest | [v2][v2-doc] | '>= 4.3' | '>= 7.1' |
| Version | Status | Documentation | Symfony Version | PHP Version |
|---------|------------|----------------|-----------------|-------------|
| 1.x | Deprecated | [v1][v1-doc] | >= 3.3 && <5 | >= 7.1 |
| 2.x | Maintained | [v2][v2-doc] | >= 4.3 | >= 7.4 |
| 3.x | Latest | [v3][v3-doc] | >= 5.4 | >= 8.1 |

[v1-doc]: https://github.com/biig-io/DomainComponent/tree/v1
[v2-doc]: https://github.com/biig-io/DomainComponent
[v1-doc]: https://github.com/swagindustries/doctrine-domain-events/tree/v1.5.2/docs
[v2-doc]: https://github.com/swagindustries/doctrine-domain-events/tree/v2.3.3/docs
[v3-docs]: https://github.com/swagindustries/doctrine-domain-events/tree/master/docs
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"require": {
"php": ">=8.1",
"symfony/event-dispatcher": "^5.0|^6.0|^7.0",
"symfony/event-dispatcher": "^5.4|^6.0|^7.0",
"doctrine/doctrine-bundle": "^1.8|^2.0",
"doctrine/orm": "^2.18.3"
},
Expand Down
34 changes: 17 additions & 17 deletions docs/domain_event_dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ domain events.
Make a rule
-----------

To make a new rule (its a listener) you should implement the `DomainRuleInterface`.
To make a new rule (it's a listener) you should implement the `DomainRuleInterface`.

### Standalone usage

Expand All @@ -20,32 +20,37 @@ use Biig\Component\Domain\Rule\DomainRuleInterface;
use Biig\Component\Domain\Event\DomainEvent;

$dispatcher->addRule(new class implements DomainRuleInterface {
public function execute(DomainEvent $event) {
public function execute(DomainEvent $event)
{
// add some specific behavior
}

public function on() {
public function on()
{
return 'on.event';
}
});
```

#### Add a post persist delayed rule

A post persist rule will occure only if the specified event is emit, but only after the data is persisted in storage. Basically flushed in the case of Doctrine.
A post persist rule will occur only if the specified event is emitted, but only after the data is persisted in storage.
Basically flushed in the case of Doctrine.

```php
<?php
use Biig\Component\Domain\Event\DomainEvent;
use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface;

$dispatcher->addRule(new class implements PostPersistDomainRuleInterface {
public function execute(DomainEvent $event) {
public function execute(DomainEvent $event)
{
// add some specific behavior
}

public function after() {
return 'on.event'; // You have to specify the model
public function after()
{
return 'on.event';
}
});
```
Expand All @@ -65,17 +70,17 @@ biig_domain:
> Using PostPersistRule means that the flush of doctrine will (re)dispatch some events. If this is a great way to add features during your workflow...
> This also means that using a Doctrine **flush** in a domain rule (even a different one) is something tricky.
>
> However this package will not fail or end in infinite loop, it's 100% supported, but events order may be surprising.
> However, this package will not fail or end in infinite loop, it's 100% supported, but events order may be surprising.
>
> ⚠️⚠️⚠️⚠️⚠️⚠️⚠️
### Symfony Integration
If you use the Symfony Bundle with auto-configuration of your services.
If you use the Symfony Bundle with autoconfiguration of your services.
**You don't have anything to do.**
If you don't auto-discover your services and don't enable auto-configuration, then you will need to add the tag:
If you don't auto-discover your services and don't enable autoconfiguration, then you will need to add the tag:
```yaml
My\Domain\Rule:
tags:
Expand All @@ -92,19 +97,14 @@ My\Domain\Rule:
- { name: biig_domain.rule, event: 'your.event.name', method: 'execute', priority: 0 }
```
_Notice: the priority field is optional._
_Notice: the priority field is optional as well as method._
#### Configuration reference
```yaml
biig_domain:
# It modifies the DoctrineBundle configuration to register a new
# ClassMetadataInfo class so the instantiator now set the domain event
# dispatcher to your models automatically
override_doctrine_instantiator: true

# By default it will override the doctrine instantiator only for
# By default, it will override the doctrine instantiator only for
# the "default" entity manager of your application. You can specify
# many entity managers if you want.
entity_managers: []
Expand Down
16 changes: 4 additions & 12 deletions docs/injection_in_doctrine_entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,31 @@ Injection of DomainEventDispatcher in Doctrine entities

_This feature allows you to merge your doctrine entities with DDD model._

_To achieve this goal it provides you a set of classes to extends doctrine behavior on entities instantiation._
_To achieve this goal it provides you a set of classes to extends doctrine behavior on entity instantiation._

How it works
------------

Doctrine uses an `Instantiator` class to instantiate entities. (this is some kind of factory)

As this `Instantiator` is hardly instantiated by Doctrine, we need to extends the ORM core. Which mean
this feature **may** be in **conflict** with some other packages that may extends doctrine behavior (I don't know any).
As this `Instantiator` is hardly instantiated by Doctrine, we need to extend the ORM core. Which mean
this feature **may** be in **conflict** with some other packages that may extend doctrine behavior (I don't know any).


### Usage without integration

```php
<?php
use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory;
use Biig\Component\Domain\Event\DomainEventDispatcher;

$dispatcher = new DomainEventDispatcher();
$configuration = new \Doctrine\ORM\Configuration();
$configuration->setClassMetadataFactoryName(new ClassMetadataFactory($dispatcher));
$entityManager = new \Doctrine\ORM\EntityManager($connection, $configuration);
$entityManager->getEventManager()->addEventSubscriber(new PostLoadDispatcherInjectionListener($dispatcher));
```

### Symfony integration

And then you can enable or disable this feature. Here is the default configuration:

```yaml
biig_domain:
override_doctrine_instantiator: true
```
⚠ You need to know it
----------------------

Expand Down

0 comments on commit 6f22364

Please sign in to comment.