From 80ea81bac77c51e236583d4aad29bea5fde01745 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Tue, 4 Oct 2016 12:08:28 -0400 Subject: [PATCH] Add support for the Datastore emulator (#181) --- src/Datastore/Connection/Rest.php | 6 +++++- src/Datastore/DatastoreClient.php | 18 ++++++++++++++++++ src/PubSub/PubSubClient.php | 17 +++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Datastore/Connection/Rest.php b/src/Datastore/Connection/Rest.php index ae0261f28b66..6ae775532610 100644 --- a/src/Datastore/Connection/Rest.php +++ b/src/Datastore/Connection/Rest.php @@ -38,10 +38,14 @@ class Rest implements ConnectionInterface */ public function __construct(array $config = []) { + $emulatorHost = getenv('DATASTORE_EMULATOR_HOST'); + + $baseUri = $this->getEmulatorBaseUri(self::BASE_URI, $emulatorHost); + $this->setRequestWrapper(new RequestWrapper($config)); $this->setRequestBuilder(new RequestBuilder( __DIR__ . '/ServiceDefinition/datastore-v1.json', - self::BASE_URI + $baseUri )); } diff --git a/src/Datastore/DatastoreClient.php b/src/Datastore/DatastoreClient.php index eeae33e17e58..55d98628bd8e 100644 --- a/src/Datastore/DatastoreClient.php +++ b/src/Datastore/DatastoreClient.php @@ -35,6 +35,12 @@ * through use of data partitions. A partition ID can be supplied when creating an instance of Cloud Datastore, and will * be used in all operations executed in that instance. * + * To enable the + * [Google Cloud Datastore Emulator](https://cloud.google.com/datastore/docs/tools/datastore-emulator), + * set the + * [`PUBSUB_EMULATOR_HOST`](https://cloud.google.com/datastore/docs/tools/datastore-emulator#setting_environment_variables) + * environment variable. + * * Example: * ``` * use Google\Cloud\ServiceBuilder; @@ -62,6 +68,18 @@ * 'namespaceId' => 'my-application-namespace' * ]); * ``` + * + * ``` + * // Using the Datastore Emulator + * use Google\Cloud\ServiceBuilder; + * + * // Be sure to use the port specified when starting the emulator. + * // `8900` is used as an example only. + * putenv('DATASTORE_EMULATOR_HOST=http://localhost:8900'); + * + * $cloud = new ServiceBuilder(); + * $datastore = $cloud->datastore(); + * ``` */ class DatastoreClient { diff --git a/src/PubSub/PubSubClient.php b/src/PubSub/PubSubClient.php index bd22bf1514c6..69df97e58bf1 100644 --- a/src/PubSub/PubSubClient.php +++ b/src/PubSub/PubSubClient.php @@ -26,8 +26,9 @@ * messages between independent applications. Find more information at * [Google Cloud Pub/Sub docs](https://cloud.google.com/pubsub/docs/). * - * The [PUBSUB_EMULATOR_HOST](https://cloud.google.com/pubsub/emulator#env) environment variable - * from the gcloud SDK is honored, otherwise the actual API endpoint will be used. + * To enable the [Google Cloud Pub/Sub Emulator](https://cloud.google.com/pubsub/emulator), + * set the [`PUBSUB_EMULATOR_HOST`](https://cloud.google.com/pubsub/emulator#env) + * environment variable. * * Example: * ``` @@ -44,6 +45,18 @@ * * $pubsub = new PubSubClient(); * ``` + * + * ``` + * // Using the Pub/Sub Emulator + * use Google\Cloud\ServiceBuilder; + * + * // Be sure to use the port specified when starting the emulator. + * // `8900` is used as an example only. + * putenv('PUBSUB_EMULATOR_HOST=http://localhost:8900'); + * + * $cloud = new ServiceBuilder(); + * $pubsub = $cloud->pubsub(); + * ``` */ class PubSubClient {