1. Go to this page and download the library: Download star/domain-event library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
star / domain-event example snippets
// Product.php
class Product extends AggregateRoot
{
}
class ProductWasCreated implements DomainEvent
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function name(): string
{
return $this->name;
}
}
// Product.php
/**
* Static construct, since the __construct() is protected
*
* @param string $name
* @return Product
*/
public static function draftProduct(string $name): Product
{
return self::fromStream([new ProductWasCreated($name)]);
}
/**
* Mutation method that handles the business logic of your aggregate
*/
public function confirm(): void
{
$this->mutate(new ProductWasConfirmed($this->getId()));
}
class DoSomethingProductCreated implements EventListener
{
// methods on listener can be anything, it is configured by listensTo
public function doSomething(ProductWasCreated $event): void
{
// do something with the event
}
public function doSomethingAtFirst(PostWasPublished $event): void
{
}
public function doSomethingInBetween(PostWasPublished $event): void
{
}
public function doSomethingAtLast(PostWasPublished $event): void
{
}
public function listensTo(): array
{
return [
ProductWasCreated::class => 'doSomething', // priority will be assigned at runtime
PostWasPublished::class => [ // multiple methods may be assigned priority
100 => 'doSomethingAtFirst',
0 => 'doSomethingInBetween',
-20 => 'doSomethingAtLast',
],
];
}
}
$publisher = new class() implements EventPublisher {}; // your implementation choice
$publisher->subscribe(new DoSomethingProductCreated()); // This is a subscriber that listens to the ProductWasCreated event
$product = Product::draftProduct('lightsaber');
$publisher->publishChanges($product->uncommitedEvents()); // will notify the listener and call the DoSomethingProductCreated::doSomething() method
protected function onStuffWasDone(StuffWasDone $event): void;
use Star\Component\DomainEvent\Ports\Symfony\DependencyInjection\CommandBusPass;
use Star\Component\DomainEvent\Ports\Symfony\DependencyInjection\EventPublisherPass;
use Star\Component\DomainEvent\Ports\Symfony\DependencyInjection\QueryBusPass;
// Kernel.php
public function build(ContainerBuilder $container): void {
$container->addCompilerPass(new CommandBusPass());
$container->addCompilerPass(new QueryBusPass());
$container->addCompilerPass(new EventPublisherPass());
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.