Download the PHP package krak/effects without Composer
On this page you can find all versions of the php package krak/effects. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package effects
Short Description Safely manage side effects within your domain layer.
License MIT
Informations about the package effects
Effects
The effects library is a small set of utilities to help enable side effects in code that you expect to remain pure using PHP generators to transfer ownership.
This is helpful in terms of Domain Driven Design and maintaining a pure domain model.
Usage
How it Works
This works by leveraging the fact that PHP generators allow sending values back to a yielded result. The handleEffects
function simply just iterates over the domain method pulling all of the commands, passing them to the command handler map, and then taking the responses and sending them back to the domain method.
The expect function is just a safety helper to provide type auto completion and assert the expected class in case there was a mapping error to make debugging a bit nicer. It's technically not needed, so if you don't care about auto-completion help with psalm and PHPStorm, then feel free to just use the yield keyword without the expect
function.
Nested Effects with yield from
If you end up needing to raise a few effects with one method, it may make sense to have specific methods used to manage and raise those effects.
You can use the yield from
statement to raise effects from child methods. here's an example:
Prewk\Result Integration
If you are working with more complex domain methods/services, it can be helpful to structure individual parts of the code in separate functions that return results and can short circuit operations like one would use with a normal Result class.
Let's see how we can acheive that with the MapEffectResults class.
Installation
Install with composer at krak/effects
Inspiration
This design is inspired from the Elm language design around maintaining pure application code while leaving side effects to be managed by the runtime.
Here are some other helpful resources around domain model purity and side effects: