PHP code example of smartgecko / governor-framework
1. Go to this page and download the library: Download smartgecko/governor-framework 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/ */
smartgecko / governor-framework example snippets
class User extends AbstractAnnotatedAggregateRoot
{
/**
* @AggregateIdentifier
* @var string
*/
private $identifier;
private $email;
/**
* @CommandHandler
* @param CreateUserCommand $command
*/
public function __construct(CreateUserCommand $command)
{
$this->apply(new UserCreatedEvent($command->getIdentifier(), $command->getEmail()));
}
/**
* @CommandHandler
* @param ChangeUserEmailCommand $command
*/
public function changeEmail(ChangeUserEmailCommand $command)
{
$this->apply(new UserEmailChangedEvent($this->identifier, $command->getEmail()));
}
/**
* @EventHandler
* @param UserEmailChangedEvent $event
*/
public function onEmailChanged(UserEmailChangedEvent $event)
{
$this->email = $event->getEmail();
}
/**
* @EventHandler
* @param UserCreatedEvent $event
*/
public function onUserCreated(UserCreatedEvent $event)
{
$this->identifier = $event->getIdentifier();
$this->email = $event->getEmail();
}
public function getEmail()
{
return $this->email;
}
}
abstract class AbstractUserEvent
{
/**
* @Type("string")
* @var string
*/
private $identifier;
/**
* @Type("string")
* @var string
*/
private $email;
function __construct($identifier, $email)
{
$this->identifier = $identifier;
$this->email = $email;
}
public function getIdentifier()
{
return $this->identifier;
}
public function getEmail()
{
return $this->email;
}
}
class UserCreatedEvent extends AbstractUserEvent
{
}
class UserEmailChangedEvent extends AbstractUserEvent
{
}
abstract class AbstractUserCommand
{
/**
* @TargetAggregateIdentifier
* @var string
*/
private $identifier;
private $email;
function __construct($identifier, $email)
{
$this->identifier = $identifier;
$this->email = $email;
}
public function getIdentifier()
{
return $this->identifier;
}
public function getEmail()
{
return $this->email;
}
}
class CreateUserCommand extends AbstractUserCommand
{
}
class ChangeUserEmailCommand extends AbstractUserCommand
{
}
class UserEventListener implements EventListenerInterface
{
public function handle(EventMessageInterface $event)
{
print_r($event->getPayload());
}
}
// set up logging
$logger = new Logger('governor');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
// 1. create a command bus and command gateway
$commandBus = new SimpleCommandBus();
$commandBus->setLogger($logger);
$commandGateway = new DefaultCommandGateway($commandBus);
$rootDirectory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'CommandHandlerExample';
@mkdir($rootDirectory);
echo sprintf("Initializing FileSystemEventStore in %s\n", $rootDirectory);
// 2. initialize the event store
$eventStore = new FilesystemEventStore(new SimpleEventFileResolver($rootDirectory),
new JMSSerializer());
// 3. create the event bus
$eventBus = new SimpleEventBus();
$eventBus->setLogger($logger);
// 4. create an event sourcing repository
$repository = new EventSourcingRepository(User::class,
$eventBus, new NullLockManager(), $eventStore,
new GenericAggregateFactory(User::class));
//5. create and register our commands
AnnotatedAggregateCommandHandler::subscribe(User::class, $repository, $commandBus);
//6. create and register the eventlistener
$eventListener = new UserEventListener();
$eventBus->subscribe($eventListener);
//7. send commands
$aggregateIdentifier = Uuid::uuid1()->toString();
$commandGateway->send(new CreateUserCommand($aggregateIdentifier,
'[email protected]'));
$commandGateway->send(new ChangeUserEmailCommand($aggregateIdentifier,
'[email protected]'));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.