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
2 changes: 1 addition & 1 deletion docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default defineConfig({
socialLinks: [
{
icon: 'github',
link: 'https://github.com/cloudcreativity/ddd-modules',
link: 'https://github.com/cloudcreativity/php-ddd-modules',
},
],

Expand Down
8 changes: 4 additions & 4 deletions docs/guide/application/asynchronous-processing.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ allow command messages to be queued for asynchronous dispatch.
There are two scenarios where a bounded context's work could be queued:

- **Commands dispatched by the presentation and delivery layer** - but where the presentation layer does not need to
wait for the result of the command, i.e. prefers to return early. We refer to this as **external queueing**, as the
wait for the result of the command, i.e. prefers to return early. We refer to this as **external queuing**, as the
request to queue the command comes from _outside_ your bounded context.
- **Work executed as an internal implementation of your application layer.** A typical example is where a domain event
listener queues work that needs to happen as a consequence of the domain event, but the execution of the work does not
Expand Down Expand Up @@ -117,7 +117,7 @@ We could push this internal work to a queue via a domain event listener:
```php
namespace App\Modules\EventManagement\Application\Orchestration\Listeners;

use App\Modules\EventManagement\Application\Ports\InternalQueue;
use App\Modules\EventManagement\Application\Ports\Queue\InternalQueue;
use App\Modules\EventManagement\Application\Internal\RecalculateSalesAtEventCommand;
use App\Modules\EventManagement\Domain\Events\AttendeeTicketWasCancelled;

Expand All @@ -129,7 +129,7 @@ final readonly class QueueTicketSalesReportRecalculation

public function handle(AttendeeTicketWasCancelled $event): void
{
$this->queue->queue(new RecalculateSalesAtEventCommand(
$this->queue->push(new RecalculateSalesAtEventCommand(
$event->eventId,
));
}
Expand Down Expand Up @@ -175,7 +175,7 @@ Define the internal command bus as follows:
```php
namespace App\Modules\EventManagement\Application\Internal;

use CloudCreativity\Modules\Application\Ports\Driving\CommandBus\CommandDispatcher;
use CloudCreativity\Modules\Contracts\Messaging\CommandDispatcher;

interface InternalCommandBus extends CommandDispatcher
{
Expand Down
40 changes: 20 additions & 20 deletions docs/guide/application/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ For example:
```php
namespace App\Modules\EventManagement\Application\UseCases;

use App\Modules\EventManagement\Api\Input\CancelAttendeeTicketCommand;
use App\Modules\EventManagement\Application\Ports\Persistence\AttendeeRepository;
use CloudCreativity\Modules\Application\Bus\Middleware\ExecuteInUnitOfWork;
use CloudCreativity\Modules\Toolkit\Pipeline\Through;
use CloudCreativity\Modules\Toolkit\Results\Result;
use CloudCreativity\Modules\Toolkit\Result\Result;

#[Through(ExecuteInUnitOfWork::class)]
final readonly class CancelAttendeeTicketHandler
Expand All @@ -70,7 +71,7 @@ final readonly class CancelAttendeeTicketHandler
) {
}

public function handle(CancelAttendeeTicketCommand $command): Result
public function execute(CancelAttendeeTicketCommand $command): Result
{
$attendee = $this->attendees->findOrFail($command->attendeeId);

Expand Down Expand Up @@ -149,7 +150,7 @@ use CloudCreativity\Modules\Toolkit\Pipeline\Through;

#[Through(LogMessageDispatch::class)]
#[WithCommand(CancelAttendeeTicketCommand::class, CancelAttendeeTicketHandler::class)]
final class CommandBus extends CommandDispatcher implements Port
final class CommandBusAdapter extends CommandDispatcher implements Port
{
}
```
Expand All @@ -165,7 +166,7 @@ container. This container is then used to resolve any middleware and command han
dispatcher via the `Through` and `WithCommand` attributes.

```php
$dispatcher = new CommandBus($psrContainer);
$dispatcher = new CommandBusAdapter($psrContainer);
```

So, for example in a Laravel application, you would bind this to the interface as follows in your service provider:
Expand All @@ -184,7 +185,7 @@ $this->app->bind(
### Dispatching Commands

You can now dispatch command messages to your bounded context from the _outside world_. For example, if we were using
a single action controller to handle a HTTP request in a Laravel application, we might dispatch a command like this:
a single action controller to handle an HTTP request in a Laravel application, we might dispatch a command like this:

```php
namespace App\Http\Controllers\Api\Attendees;
Expand Down Expand Up @@ -273,7 +274,7 @@ And then our implementation is as follows:
```php
namespace App\Modules\EventManagement\Application\Adapters;

use App\Modules\EventManagement\Application\Ports\Queue;
use App\Modules\EventManagement\Application\Ports\Queue\Queue;
use App\Modules\EventManagement\Api\CommandQueuer as Port;
use CloudCreativity\Modules\Application\Bus\CommandQueuer as Queuer;

Expand Down Expand Up @@ -368,7 +369,7 @@ Our `SetupBeforeDispatch` middleware allows setup work to be run before the comm
teardown work when the command has completed.

This allows you to set up any state and guarantee that the state is cleaned up, regardless of the outcome of the
command. The primary use case for this is to boostrap [Domain Services](../domain/services) and to garbage collect any
command. The primary use case for this is to bootstrap [Domain Services](../domain/services) and to garbage collect any
singleton instances of dependencies.

For example, bind an instance of the middleware into your service container:
Expand Down Expand Up @@ -397,15 +398,15 @@ Here our setup middleware takes a setup closure as its only constructor argument
return a closure to do any teardown work. The teardown callback is guaranteed to always be executed, regardless of
whether a command succeeded or failed - and it also runs if an exception is thrown.

If you only need to do teardown work, use the `TeardownAfterDispatch` middleware instead. This takes a single teardown
If you only need to do teardown work, use the `TearDownAfterDispatch` middleware instead. This takes a single teardown
closure as its only constructor argument:

```php
use CloudCreativity\Modules\Bus\Middleware\TeardownAfterDispatch;
use CloudCreativity\Modules\Bus\Middleware\TearDownAfterDispatch;

$container->bind(
'event:management:teardown',
fn () => new TeardownAfterDispatch(function (): Closure {
'event-management:teardown',
fn () => new TearDownAfterDispatch(function (): void {
// clean up work goes here.
}),
);
Expand Down Expand Up @@ -578,27 +579,26 @@ namespace App\Modules\EventManagement\Application\Adapters\Middleware;

use Closure;
use CloudCreativity\Modules\Contracts\Bus\Middleware\BusMiddleware;
use CloudCreativity\Modules\Contracts\Messaging\Command;
use CloudCreativity\Modules\Contracts\Messaging\Query;
use CloudCreativity\Modules\Contracts\Messaging\Message;
use CloudCreativity\Modules\Contracts\Toolkit\Result\Result;

class MyBusMiddleware implements BusMiddleware
{
/**
* Handle the command or query.
* Handle the message.
*
* @param Command|Query $message
* @param Closure(Command|Query): Result<mixed> $next
* @return Result<mixed>
* @param Message $message
* @param Closure(Message): ?Result<mixed> $next
* @return Result<mixed>|null
*/
public function __invoke(
Command|Query $message,
Message $message,
Closure $next,
): Result
): ?Result
{
// code here executes before the handler

$result = $next($command);
$result = $next($message);

// code here executes after the handler

Expand Down
13 changes: 6 additions & 7 deletions docs/guide/application/domain-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ To use this dispatcher, create a concrete implementation of your domain layer's
namespace App\Modules\EventManagement\Application\Orchestration;

use App\Modules\EventManagement\Domain\Events\DomainEventDispatcher as IDomainEventDispatcher;
use CloudCreativity\Modules\Application\DomainEventDispatching\UnitOfWorkAwareDispatcher;
use CloudCreativity\Modules\Application\DomainEventDispatching\ListenTo;
use CloudCreativity\Modules\Application\DomainEventDispatching\Middleware\LogDomainEventDispatch;
use CloudCreativity\Modules\Application\DomainEventDispatching\UnitOfWorkAwareDispatcher;
use CloudCreativity\Modules\Toolkit\Pipeline\Through;

#[Through(LogDomainEventDispatch::class)]
Expand Down Expand Up @@ -148,8 +149,7 @@ listeners and middleware.
For example:

```php

$dispatcher = new DomainEventDispatcher($container);
$dispatcher = new DomainEventDispatcherAdapter($container);
```

The main thing you need to ensure is that your instance of this domain event dispatcher is a singleton. This ensures
Expand Down Expand Up @@ -190,7 +190,7 @@ final readonly class AttendeeTicketCancelled implements
```

:::warning
Doing this risks means side effects of the domain event will occur, even if something about the command handling
Doing this means side effects of the domain event will occur, even if something about the command handling
subsequently fails. For example, if there is an error in your infrastructure layer when persisting state changes. This
can compromise the consistency of your domain state.
:::
Expand Down Expand Up @@ -270,7 +270,6 @@ namespace App\Modules\EventManagement\Application\Orchestration\Middleware;
use Closure;
use CloudCreativity\Modules\Contracts\Application\DomainEventDispatching\DomainEventMiddleware;
use CloudCreativity\Modules\Contracts\Domain\Events\DomainEvent;
use CloudCreativity\Modules\Contracts\Toolkit\Result\Result;

final class MyMiddleware implements DomainEventMiddleware
{
Expand All @@ -284,11 +283,11 @@ final class MyMiddleware implements DomainEventMiddleware
public function __invoke(
DomainEvent $event,
Closure $next,
): Result
): void
{
// code here executes before the event is emitted.

$next($command);
$next($event);

// code here executes after it is emitted.
}
Expand Down
Loading