PHP code example of thenativeweb / eventsourcingdb
1. Go to this page and download the library: Download thenativeweb/eventsourcingdb 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/ */
thenativeweb / eventsourcingdb example snippets
use Thenativeweb\Eventsourcingdb\Client;
$client = new Client('http://localhost:3000', 'secret');
$client->ping();
$client->verifyApiToken();
$client = new Client('http://localhost:3000', 'secret', stallTimeout: 10);
use Thenativeweb\Eventsourcingdb\EventCandidate;
$writtenEvents = $client->writeEvents([
new EventCandidate(
'https://library.eventsourcingdb.io',
'/books/42',
'io.eventsourcingdb.library.book-acquired',
[
'title' => '2001 – A Space Odyssey',
'author' => 'Arthur C. Clarke',
'isbn' => '978-0756906788',
],
),
]);
use Thenativeweb\Eventsourcingdb\IsSubjectPristine;
$writtenEvents = $client->writeEvents([
// events
], [
new IsSubjectPristine('/books/42'),
]);
use Thenativeweb\Eventsourcingdb\DbApiException;
use Thenativeweb\Eventsourcingdb\ErrorCode;
try {
$writtenEvents = $client->writeEvents([
// events
], [
new IsSubjectPristine('/books/42'),
]);
} catch (DbApiException $exception) {
if ($exception->errorCode === ErrorCode::PRECONDITION_VIOLATED) {
// ...
}
}
use Thenativeweb\Eventsourcingdb\IsSubjectPopulated;
$writtenEvents = $client->writeEvents([
// events
], [
new IsSubjectPopulated('/books/42'),
]);
use Thenativeweb\Eventsourcingdb\IsSubjectOnEventId;
$writtenEvents = $client->writeEvents([
// events
], [
new IsSubjectOnEventId('/books/42', '0'),
]);
use Thenativeweb\Eventsourcingdb\IsSubjectPristine;
use Thenativeweb\Eventsourcingdb\PreconditionOptions;
$writtenEvents = $client->writeEvents([
// events
], [
new IsSubjectPristine('/books/42', new PreconditionOptions(recursive: true)),
]);
use Thenativeweb\Eventsourcingdb\IsEventQlQueryTrue;
$writtenEvents = $client->writeEvents([
// events
], [
new IsEventQlQueryTrue("FROM e IN events WHERE e.type == 'io.eventsourcingdb.library.book-borrowed' PROJECT INTO COUNT() < 10")
]);
use Thenativeweb\Eventsourcingdb\ReadEventsOptions;
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: false,
),
);
foreach ($events as $event) {
// ...
}
use Thenativeweb\Eventsourcingdb\ReadEventsOptions;
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: true,
),
);
foreach ($events as $event) {
// ...
}
use Thenativeweb\Eventsourcingdb\Order;
use Thenativeweb\Eventsourcingdb\ReadEventsOptions;
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: false,
order: Order::ANTICHRONOLOGICAL,
),
);
foreach ($events as $event) {
// ...
}
use Thenativeweb\Eventsourcingdb\Bound;
use Thenativeweb\Eventsourcingdb\BoundType;
use Thenativeweb\Eventsourcingdb\ReadEventsOptions;
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: false,
lowerBound: new Bound('100', BoundType::INCLUSIVE),
upperBound: new Bound('200', BoundType::EXCLUSIVE),
),
);
foreach ($events as $event) {
// ...
}
use Thenativeweb\Eventsourcingdb\ReadEventsOptions;
use Thenativeweb\Eventsourcingdb\ReadFromLatestEvent;
use Thenativeweb\Eventsourcingdb\ReadIfEventIsMissing;
$events = $client->readEvents(
'/books/42',
new ReadEventsOptions(
recursive: false,
fromLatestEvent: new ReadFromLatestEvent(
subject: '/books/42',
type: 'io.eventsourcingdb.library.book-borrowed',
ifEventIsMissing: ReadIfEventIsMissing::READ_EVERYTHING,
),
),
);
foreach ($events as $event) {
// ...
}
$rows = $client->runEventQlQuery(
'FROM e IN events PROJECT INTO e',
);
foreach ($rows as $row) {
// ...
}
use Thenativeweb\Eventsourcingdb\ObserveEventsOptions;
$events = $client->observeEvents(
'/books/42',
new ObserveEventsOptions(
recursive: false,
),
);
foreach ($events as $event) {
// ...
}
use Thenativeweb\Eventsourcingdb\ObserveEventsOptions;
$events = $client->observeEvents(
'/books/42',
new ObserveEventsOptions(
recursive: true,
),
);
foreach ($events as $event) {
// ...
}
use Thenativeweb\Eventsourcingdb\Bound;
use Thenativeweb\Eventsourcingdb\BoundType;
use Thenativeweb\Eventsourcingdb\ObserveEventsOptions;
$events = $client->observeEvents(
'/books/42',
new ObserveEventsOptions(
recursive: false,
lowerBound: new Bound('100', BoundType::INCLUSIVE),
),
);
foreach ($events as $event) {
// ...
}
use Thenativeweb\Eventsourcingdb\ObserveEventsOptions;
use Thenativeweb\Eventsourcingdb\ObserveFromLatestEvent;
use Thenativeweb\Eventsourcingdb\ObserveIfEventIsMissing;
$events = $client->observeEvents(
'/books/42',
new ObserveEventsOptions(
recursive: false,
fromLatestEvent: new ObserveFromLatestEvent(
subject: '/books/42',
type: 'io.eventsourcingdb.library.book-borrowed',
ifEventIsMissing: ObserveIfEventIsMissing::READ_EVERYTHING,
),
),
);
foreach ($events as $event) {
// ...
}
use Thenativeweb\Eventsourcingdb\ObserveEventsOptions;
$events = $client->observeEvents(
'/books/42',
new ObserveEventsOptions(
recursive: false,
),
);
$client->abortIn(0.1);
foreach ($events as $event) {
// ...
$client->abortIn(0.1);
}
use Thenativeweb\Eventsourcingdb\DbApiException;
use Thenativeweb\Eventsourcingdb\ErrorCode;
try {
$eventType = $client->readEventType('io.eventsourcingdb.library.book-acquired');
} catch (DbApiException $exception) {
if ($exception->errorCode === ErrorCode::EVENT_TYPE_UNKNOWN) {
// ...
}
}
use Thenativeweb\Eventsourcingdb\OutcomeUnknownException;
try {
$writtenEvents = $client->writeEvents([
// events
]);
} catch (OutcomeUnknownException $exception) {
// Find out whether the events were written, e.g. by reading them,
// before writing them again.
}
// Before
$exception->getMessage(); // 'failed to read events'
$exception->getMessage(); // "Failed to write events, state conflict: precondition failed\n"
$exception->getMessage(); // 'Failed to write events, '
// After
$exception->getMessage(); // 'Failed to read events, failed to read events'
$exception->getMessage(); // 'Failed to write events, state conflict: precondition failed'
$exception->getMessage(); // 'Failed to write events, the server answered with status 500.'
$exception->description; // 'state conflict: precondition failed'
// Before
try { /* ... */ } catch (HeartbeatTimeoutException $exception) {
$exception->getMessage(); // 'No event and no heartbeat arrived for 30 seconds.'
}
// After
try { /* ... */ } catch (ConnectionStalledException $exception) {
$exception->getMessage(); // 'No data arrived from the server for 30 seconds.'
}
// Before
try { $client->writeEvents($events); } catch (ConnectionStalledException $exception) { /* ... */ }
// After
try { $client->writeEvents($events); } catch (OutcomeUnknownException $exception) {
$cause = $exception->getPrevious(); // e.g. a ConnectionStalledException
}
// Before
$client->abortIn(0.1);
$writtenEvents = $client->writeEvents($events); // [], if nothing of the answer had arrived
// After
$client->abortIn(0.1);
try { $client->writeEvents($events); } catch (OutcomeUnknownException $exception) { /* ... */ }
// Before
foreach ($client->observeEvents('/books', $options) as $event) { /* ... */ }
// The loop ended quietly once the response ended, e.g. because a proxy ended it.
// After
try {
foreach ($client->observeEvents('/books', $options) as $event) { /* ... */ }
} catch (RuntimeException $exception) {
// The observation broke off, e.g. because a proxy ended its response.
}
// Before
$stream = new Stream($curlMultiHandler, hasHeartbeatTimeout: true);
$stream = $stream->withHeartbeatTimeout();
$chunks = $curlMultiHandler->contentIterator(withHeartbeatTimeout: true);
// After
$stream = new Stream($curlMultiHandler);
$chunks = $curlMultiHandler->contentIterator();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.