Download the PHP package thenativeweb/eventsourcingdb without Composer

On this page you can find all versions of the php package thenativeweb/eventsourcingdb. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package eventsourcingdb

eventsourcingdb

The official PHP client SDK for EventSourcingDB – a purpose-built database for event sourcing.

EventSourcingDB enables you to build and operate event-driven applications with native support for writing, reading, and observing events. This client SDK provides convenient access to its capabilities in PHP.

For more information on EventSourcingDB, see its official documentation.

This client SDK includes support for Testcontainers to spin up EventSourcingDB instances in integration tests. For details, see Using Testcontainers.

Getting Started

Install the client SDK:

Import the Client class and create an instance by providing the URL of your EventSourcingDB instance and the API token to use:

Then call the ping function to check whether the instance is reachable. If it is not, the function will throw an exception:

Note that ping does not require authentication, so the call may succeed even if the API token is invalid.

If you want to verify the API token, call verifyApiToken. If the token is invalid, the function will throw an exception:

Talking to Servers With and Without the v2 API

Creating a client does not send a request. The client SDK works with servers that offer the v2 API of EventSourcingDB, which it offers since version 1.3, and with servers that only offer the v1 API. On its first request, the client asks the server for the ping of the v2 API, GET /api/v2/system/ping, and remembers the answer: if the server answers it, the client uses the v2 API, and if the server does not know the route, the client uses the v1 API. If the server answers a path of the v2 API with 404 or 405, e.g. after it was rolled back to a version without it, the client asks again on the next request. A write with a recursive precondition, which only the v2 API offers, asks again as well if the client remembers that the server only offers the v1 API, since the server may have been upgraded in the meantime, see Using Preconditions on Subjects Recursively. If asking fails, e.g. because the server is not reachable, the request that asked fails with that error. If the client does not remember an answer yet, the next request asks again; if it remembers that the server only offers the v1 API, it keeps that answer, and only a write with a recursive precondition asks again.

Note that the v2 API reads with the HTTP method QUERY (RFC 10008), which a proxy in front of the database has to let through. There is no fallback to POST.

Configuring the Stall Timeout

A request fails once nothing at all, neither data nor a heartbeat, has arrived from the server for 30 seconds, counted from sending the request, including connecting and waiting for the response headers. This keeps a stalled connection from blocking forever. Since anything that arrives resets this time, a long request that keeps receiving data, such as reading many events or observing events, is never cut off.

To change the stall timeout, pass the stallTimeout option in seconds when creating the client:

If a request stalls, the client closes the connection and throws a ConnectionStalledException, which extends RuntimeException. This holds for every request, including streams such as observeEvents and runEventQlQuery, which receive a heartbeat every second while there is nothing else to send. If a write stalls once the connection to the server is established, the client throws an OutcomeUnknownException instead, whose previous exception is the ConnectionStalledException, since the server may have stored the write nonetheless. A write that stalls while connecting was never sent, so the client throws the ConnectionStalledException itself, see Knowing Whether a Write Was Stored.

Writing Events

Call the writeEvents function and hand over an array with one or more events. You do not have to provide all event fields – some are automatically added by the server.

Specify source, subject, type, and data according to the CloudEvents format.

The function returns the written events, including the fields added by the server:

Using the isSubjectPristine precondition

If you only want to write events in case a subject (such as /books/42) does not yet have any events, import the IsSubjectPristine class, use it to create a precondition, and pass it in an array as the second argument:

If a precondition does not hold, the function throws a DbApiException whose errorCode is ErrorCode::PRECONDITION_VIOLATED, and writes no event:

The same holds for the other preconditions.

Using the isSubjectPopulated precondition

If you only want to write events in case a subject (such as /books/42) already has at least one event, import the IsSubjectPopulated class, use it to create a precondition, and pass it in an array as the second argument:

Using the isSubjectOnEventId precondition

If you only want to write events in case the last event of a subject (such as /books/42) has a specific ID (e.g., 0), import the IsSubjectOnEventId class, use it to create a precondition, and pass it in an array as the second argument:

Note that according to the CloudEvents standard, event IDs must be of type string.

Using Preconditions on Subjects Recursively

If you want a precondition on a subject to apply not only to the subject itself, but also to all nested subjects, pass a PreconditionOptions object as the last argument of IsSubjectPristine, IsSubjectPopulated, or IsSubjectOnEventId, and set its recursive option to true:

IsSubjectPristine then holds if neither the subject nor any nested subject has an event, IsSubjectPopulated holds if any of them has an event, and IsSubjectOnEventId holds if the last event among all of them has the given ID. By default, recursive is false, and the precondition applies to the given subject alone.

Note that only the v2 API offers recursive preconditions. If the server only offers the v1 API, the function throws an IncompatibleServerException, which extends RuntimeException, before it sends the events, and writes no event. To find out, the client asks the server again which API it offers if it remembers that the server only offers the v1 API, since the server may have been upgraded in the meantime.

Using the isEventQlQueryTrue precondition

If you want to write events depending on an EventQL query, use the IsEventQlQueryTrue function to create a precondition:

Note that the query must return a single row with a single value, which is interpreted as a boolean.

Reading Events

To read all events of a subject, call the readEvents function with the subject and an options object. Set the recursive option to false. This ensures that only events of the given subject are returned, not events of nested subjects.

The function returns an iterator, which you can use in a foreach loop:

Note that if you leave the foreach loop early, e.g. with break, the connection is closed once the iterator is released. If you keep the iterator in a variable, such as $events, the connection therefore stays open until you release the variable, e.g. with unset($events).

Reading From Subjects Recursively

If you want to read not only all the events of a subject, but also the events of all nested subjects, set the recursive option to true:

This also allows you to read all events ever written. To do so, provide / as the subject and set recursive to true, since all subjects are nested under the root subject.

Reading in Anti-Chronological Order

By default, events are read in chronological order. To read in anti-chronological order, provide the order option and set it to Order::ANTICHRONOLOGICAL:

Note that you can also use Order::CHRONOLOGICAL to explicitly enforce the default order.

Specifying Bounds

Sometimes you do not want to read all events, but only a range of events. For that, you can specify the lowerBound and upperBound options – either one of them or even both at the same time.

Specify the ID and whether to include or exclude it, for both the lower and upper bound:

Starting From the Latest Event of a Given Type

To read starting from the latest event of a given type, provide the fromLatestEvent option and specify the subject, the type, and how to proceed if no such event exists.

Possible options are ReadIfEventIsMissing::READ_NOTHING, which skips reading entirely, or ReadIfEventIsMissing::READ_EVERYTHING, which effectively behaves as if fromLatestEvent was not specified:

Note that fromLatestEvent and lowerBound can not be provided at the same time.

Running EventQL Queries

To run an EventQL query, call the runEventQlQuery function and provide the query as a string. The function returns an iterator, which you can use in a foreach loop:

Note that each row returned by the iterator matches the projection specified in your query, e.g. an associative array for an event, or a number for COUNT().

Note that EventSourcingDB sends a heartbeat every second while there is nothing else to send. If neither a row nor a heartbeat arrives for the stall timeout, which is 30 seconds by default, e.g. because the connection stalled, the iterator closes the connection and throws a ConnectionStalledException.

Observing Events

To observe all events of a subject, call the observeEvents function with the subject as the first argument and an options object as the second argument. Set the recursive option to false. This ensures that only events of the given subject are returned, not events of nested subjects.

The function returns an asynchronous iterator, which you can use e.g. inside a foreach loop:

Note that EventSourcingDB sends a heartbeat every second while there are no events to send. If neither an event nor a heartbeat arrives for the stall timeout, which is 30 seconds by default, e.g. because the connection stalled, the iterator closes the connection and throws a ConnectionStalledException.

Note that EventSourcingDB never ends an observation on its own, other than with an error, which the iterator throws as a DbApiException, e.g. with the errorCode ErrorCode::SERVER_SHUTTING_DOWN once a server that offers the v2 API shuts down. If its response ends cleanly nonetheless, e.g. because a proxy ends it, the iterator throws a RuntimeException that says the stream broke off, and if the connection breaks, it throws a RuntimeException that says so. Only abortIn and leaving the loop end an observation without an exception.

Note that if you leave the foreach loop early, e.g. with break, the connection is closed once the iterator is released. If you keep the iterator in a variable, such as $events, the connection therefore stays open until you release the variable, e.g. with unset($events).

Observing From Subjects Recursively

If you want to observe not only all the events of a subject, but also the events of all nested subjects, set the recursive option to true:

This also allows you to observe all events ever written. To do so, provide / as the subject and set recursive to true, since all subjects are nested under the root subject.

Specifying Bounds

Sometimes you do not want to observe all events, but only a range of events. For that, you can specify the lowerBound option.

Specify the ID and whether to include or exclude it:

Starting From the Latest Event of a Given Type

To observe starting from the latest event of a given type, provide the fromLatestEvent option and specify the subject, the type, and how to proceed if no such event exists.

Possible options are ObserveIfEventIsMissing::WAIT_FOR_EVENT, which waits for an event of the given type to happen, or ObserveIfEventIsMissing::READ_EVERYTHING, which effectively behaves as if fromLatestEvent was not specified:

Note that fromLatestEvent and lowerBound can not be provided at the same time.

Aborting Observing

If you need to abort observing use abortIn before or within the foreach loop. The abortIn method expects the abort time in seconds, counted from the call. It applies only to the iteration currently going on, or, if there is none, to the next one, so later requests of the client are not aborted. It covers the whole iteration, including connecting and waiting for the server to answer, and once it is over, the iteration ends without an exception:

Registering an Event Schema

To register an event schema, call the registerEventSchema function and hand over an event type and the desired schema:

If a schema is already registered for the event type, the function throws a DbApiException whose errorCode is ErrorCode::EVENT_SCHEMA_ALREADY_REGISTERED.

Listing Subjects

To list subjects, call the readSubjects function with a subject and an options object, in which you have to set the recursive option. To list all subjects, provide / as the subject and set recursive to true, since all subjects are nested under the root subject. The function returns an asynchronous iterator, which you can use e.g. inside a foreach loop:

If you only want to list subjects within a specific branch, provide the desired subject instead. The listing includes the subject itself:

If you set recursive to false, the listing only includes the given subject, if it exists, and none of the subjects nested under it.

Aborting Listing

If you need to abort listing use abortIn before or within the foreach loop. The abortIn method expects the abort time in seconds, counted from the call. It applies only to the iteration currently going on, or, if there is none, to the next one, so later requests of the client are not aborted. It covers the whole iteration, including connecting and waiting for the server to answer, and once it is over, the iteration ends without an exception:

Listing Event Types

To list all event types, call the readEventTypes function. The function returns an asynchronous iterator, which you can use e.g. inside a foreach loop:

Each event type is an EventType instance, whose type property holds the name of the event type, whose hasEvents property tells whether events of the type have been written, rather than only a schema registered for it, and whose schema property holds the registered schema, or an empty array if there is none.

Aborting Listing

If you need to abort listing use abortIn before or within the foreach loop. The abortIn method expects the abort time in seconds, counted from the call. It applies only to the iteration currently going on, or, if there is none, to the next one, so later requests of the client are not aborted. It covers the whole iteration, including connecting and waiting for the server to answer, and once it is over, the iteration ends without an exception:

Listing a Specific Event Type

To list a specific event type, call the readEventType function with the event type as an argument. The function returns the detailed event type, which includes the schema:

If the event type does not exist, the function throws a DbApiException whose errorCode is ErrorCode::EVENT_TYPE_UNKNOWN.

Handling Errors

If the server reports an error, the client throws a DbApiException, which extends RuntimeException. It has the following properties:

The ErrorCode class has a constant for every code, so that you can tell errors apart without comparing strings:

If a stream breaks off, i.e. its response ends cleanly, but before the server ended the stream, the iterator throws a RuntimeException that says so. This holds for a stream of the v2 API whose response ends without the item that ends the stream, and for an observation, which the server never ends, other than with an error. A connection that breaks fails a stream with a RuntimeException as well.

If the server only offers the v1 API, the client assigns the code of the v2 API to those errors of the v1 API that clearly match one, such as a precondition that does not hold, a schema that is already registered, or an event type that does not exist. Other errors of the v1 API keep an empty code.

Knowing Whether a Write Was Stored

EventSourcingDB never stores a write halfway, but a write whose answer is lost may have been stored nonetheless. So, if writing events or registering an event schema fails in a way that leaves open whether the server stored it, the client throws an OutcomeUnknownException, which extends RuntimeException, and whose previous exception, which getPrevious() returns, is the cause. This is the case, e.g., if the connection stalls or breaks after the request was sent, if abortIn aborts the write once the connection is established, while it waits for the answer or reads it, if the answer breaks off, if the server fails unexpectedly, or if the answer does not come from EventSourcingDB, e.g. from a proxy.

A write certainly was not stored if the client throws any other exception, i.e. if the client refused the write before sending it, which it reports with an IncompatibleServerException, if asking the server which API it offers failed before the write was sent, e.g. because abortIn aborted it, if the connection could not be established at all, e.g. because abortIn aborted the write while connecting, or if the server refused the write, which it reports with a DbApiException:

Aborting a Request

To abort a request, call abortIn before it, with the abort time in seconds, counted from the call. It applies only to the stream currently being read, or, if there is none, to the next request, so later requests of the client are not aborted. It covers the whole request, from asking the server which API it offers, if the client does not know it yet, and connecting, to waiting for the answer and reading it, and if the time is over before the request starts, the client does not send it. An aborted stream ends without an exception; any other aborted request throws a RequestAbortedException, which extends RuntimeException:

Note that if abortIn aborts writeEvents or registerEventSchema once the connection to the server is established, the client throws an OutcomeUnknownException whose previous exception is the RequestAbortedException, since the server may have stored the write nonetheless. If it aborts the write before, e.g. while asking the server which API it offers or while connecting, the client throws the RequestAbortedException itself, since the write was certainly not stored.

Note that a request that you send while a stream is read, e.g. a write within the foreach loop over an observation, cannot get an abort time of its own through the same client, since abortIn then applies to the stream. To abort such a request, send it through a second client.

Note that a stream counts as being read as long as you keep its iterator, also once you have left its loop, e.g. with break. To have abortIn apply to the next request instead, release the iterator first, e.g. with unset($events).

Verifying an Event's Hash

To verify the integrity of an event, call the verifyHash function on the event instance. This recomputes the event's hash locally and compares it to the hash stored in the event. If the hashes differ, the function throws a HashVerificationFailedException:

The function supports both versions of the hash: version 1, which events written before EventSourcingDB 1.3 keep, and version 2, which starts with esdb:hash:v2:. If the hash is of a version the client SDK does not know, the function throws an UnsupportedHashVersionException, whose message names the version.

Note that both exceptions extend RuntimeException, so code that catches a RuntimeException keeps working.

Note that an event returned by the client keeps its data exactly as the server sent it, to verify the hash. It therefore does not compare equal, e.g. with == or assertEquals, to an event you build by hand from the same fields. Compare their fields instead.

Note that this only verifies the hash. If you also want to verify the signature, you can skip this step and call verifySignature directly, which performs a hash verification internally.

Verifying an Event's Signature

To verify the authenticity of an event, call the verifySignature function on the event instance. This requires the public key that matches the private key used for signing on the server.

The function first verifies the event's hash, and then checks the signature. If the hash verification fails, it throws one of the exceptions described above. If the signature is invalid, it throws a SignatureVerificationFailedException, which extends RuntimeException as well:

Using Testcontainers

Import the Container class, call the start function to run a test container, get a client, run your test code, and finally call the stop function to stop the test container:

To check if the test container is running, call the isRunning function:

Configuring the Container Instance

By default, Container uses the latest tag of the official EventSourcingDB Docker image. To change that, call the withImageTag function:

Similarly, you can configure the port to use and the API token. Call the withPort or the withApiToken function respectively:

If you want to sign events, call the withSigningKey function. This generates a new signing and verification key pair inside the container:

You can retrieve the private key (for signing) and the public key (for verifying signatures) once the container has been started:

The signingKey is the private key EventSourcingDB signs events with. The verificationKey can be passed to verifySignature when verifying events read from the database.

The start function pulls the image if it is missing, and then waits until the database in the container answers a ping, for 30 seconds at most, not counting the pull. To change this time, call the withStartupTimeout function with the seconds:

If a container keeps running, but does not become reachable in time, start replaces it with a new one, up to three containers in total, and then throws a RuntimeException. If Docker fails, or if the container exits, start throws a RuntimeException at once, which, if the container exited, contains the last 20 lines of its log. In any case, start removes every container that it failed to start.

Configuring the Client Manually

In case you need to set up the client yourself, use the following functions to get details on the container:

Upgrading From Version 1

Version 2 of the client SDK speaks the v2 API of EventSourcingDB wherever the server offers it, and the v1 API otherwise, see Talking to Servers With and Without the v2 API. It changes the following:


All versions of eventsourcingdb with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
ext-curl Version *
ext-openssl Version *
ext-sodium Version *
testcontainers/testcontainers Version ^1.2.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package thenativeweb/eventsourcingdb contains the following files

Loading the files please wait ...