Пример #1
0
{
    echo "Hello, I am the Symfony2 Event Dispatcher Listener!" . PHP_EOL;
    var_dump($event->event());
}
// Now create our integration class and pass in the dispatcher
$symfony2EventDispatcherSubscriber = new symfony2EventDispatcherSubscriber($symfony2EventDispatcher);
// We need an EventStore. We'll use the DBAL with in-memory sqlite
$eventStore = DBALEventStore::createWithOptions('events', ['driver' => 'pdo_sqlite', 'memory' => true]);
// Register :D
$eventStore->registerEventSubscriber($symfony2EventDispatcherSubscriber);
// Create the table we need
$eventStore->createTable();
// Initialise a repository with this event store
$userRepository = new UserRepository($eventStore);
// Create a user
$user = User::createWithUsername("David");
// We can output the users username and there's no sign of an event
//  event anywhere! Nifty
echo "Hello, {$user->username()}!" . PHP_EOL;
// We can even save this user, still no mention of an event
$userRepository->save($user);
// Lets backup the identifier so that we can discard and reload
$userIdentifier = $user->identifier();
unset($user);
// Load the user from the EventStore, using our repository
$user = $userRepository->load($userIdentifier);
// Viola!
echo "Hello, {$user->username()}!" . PHP_EOL;
var_dump($user);
// What about identifiers that don't exist?
try {
Пример #2
0
{
    echo 'Hello, I am the Symfony2 Event Dispatcher Listener!' . PHP_EOL;
    var_dump($event->event());
}
// Now create our integration class and pass in the dispatcher
$symfony2EventDispatcherSubscriber = new symfony2EventDispatcherSubscriber($symfony2EventDispatcher);
// We need an EventStore. We'll use the DBAL with in-memory sqlite
$eventStore = DBALEventStore::createWithOptions('events', ['driver' => 'pdo_sqlite', 'memory' => true]);
// Register :D
$eventStore->registerSubscriber($symfony2EventDispatcherSubscriber);
// Create the table we need
$eventStore->createTable();
// Initialise a repository with this event store
$userRepository = Repository::createForWrites('Example\\User', $eventStore);
// Create a user
$user = User::createWithUsername('David');
// We can output the users username and there's no sign of an event
//  event anywhere! Nifty
echo "Hello, {$user->username()}!" . PHP_EOL;
// We can even save this user, still no mention of an event
$userRepository->save($user);
// Lets backup the identifier so that we can discard and reload
$userIdentifier = $user->identifier();
unset($user);
echo 'The user object has now been unset. Lets load through our repository!' . PHP_EOL;
// Load the user from the EventStore, using our repository
$user = $userRepository->load($userIdentifier);
// Viola!
echo "Hello, {$user->username()}!" . PHP_EOL;
var_dump($user);
// What about identifiers that don't exist?
Пример #3
0
 public function save(User $user)
 {
     $this->eventStore->store($user->identifier(), $user->stagedEvents());
 }