Skip to content

Commit

Permalink
clarify gRPC dependencies (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwsupplee authored Nov 17, 2017
1 parent 68eeb82 commit f4bd25c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
31 changes: 27 additions & 4 deletions src/Core/ClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ trait ClientTrait
*/
private function getConnectionType(array $config)
{
$isGrpcExtensionLoaded = $this->getGrpcDependencyStatus();
$isGrpcExtensionLoaded = $this->isGrpcLoaded();
$defaultTransport = $isGrpcExtensionLoaded ? 'grpc' : 'rest';
$transport = isset($config['transport'])
? strtolower($config['transport'])
Expand All @@ -56,15 +56,38 @@ private function getConnectionType(array $config)
if (!$isGrpcExtensionLoaded) {
throw new GoogleException(
'gRPC support has been requested but required dependencies ' .
'have not been found. Please make sure to run the following ' .
'from the command line: pecl install grpc'
'have not been found. ' . $this->getGrpcInstallationMessage()
);
}
}

return $transport;
}

/**
* Throw an exception if the gRPC extension is not loaded.
*
* @throws GoogleException
*/
private function requireGrpc()
{
if (!$this->isGrpcLoaded()) {
throw new GoogleException(
'The requested client requires the gRPC extension. ' .
$this->getGrpcInstallationMessage()
);
}
}

/**
* @return string
*/
private function getGrpcInstallationMessage()
{
return 'Please see https://cloud.google.com/php/grpc for installation ' .
'instructions.';
}

/**
* Fetch and validate the keyfile and set the project ID.
*
Expand Down Expand Up @@ -208,7 +231,7 @@ protected function getMetaData()
* @codeCoverageIgnore
* @return bool
*/
protected function getGrpcDependencyStatus()
protected function isGrpcLoaded()
{
return extension_loaded('grpc');
}
Expand Down
18 changes: 11 additions & 7 deletions src/Firestore/FirestoreClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

namespace Google\Cloud\Firestore;

use Google\Cloud\Core\Blob;
use Google\Cloud\Core\Retry;
use Google\Cloud\Core\GeoPoint;
use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\Blob;
use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\ValidateTrait;
use Google\Cloud\Firestore\Connection\Grpc;
use Google\Cloud\Core\Exception\AbortedException;
use Google\Cloud\Core\Exception\GoogleException;
use Google\Cloud\Core\GeoPoint;
use Google\Cloud\Core\Iterator\ItemIterator;
use Google\Cloud\Core\Iterator\PageIterator;
use Google\Cloud\Core\Exception\AbortedException;
use Google\Cloud\Core\Retry;
use Google\Cloud\Core\ValidateTrait;
use Google\Cloud\Firestore\Connection\Grpc;

/**
* Cloud Firestore is a flexible, scalable, realtime database for mobile, web, and server development.
Expand Down Expand Up @@ -68,7 +69,8 @@ class FirestoreClient
private $valueMapper;

/**
* Create a Firestore client.
* Create a Firestore client. Please note that this client requires
* [the gRPC extension](https://cloud.google.com/php/grpc).
*
* @param array $config [optional] {
* Configuration Options.
Expand Down Expand Up @@ -96,9 +98,11 @@ class FirestoreClient
* platform compatibility. **Defaults to** false.
* }
* @throws \InvalidArgumentException
* @throws GoogleException
*/
public function __construct(array $config = [])
{
$this->requireGrpc();
$config += [
'returnInt64AsObject' => false,
'scopes' => [self::FULL_CONTROL_SCOPE],
Expand Down
5 changes: 3 additions & 2 deletions src/Firestore/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"license": "Apache-2.0",
"minimum-stability": "stable",
"require": {
"ext-grpc": "*",
"google/cloud-core": "^1.0",
"google/gax": "^0.25",
"google/proto-client": "^0.25",
"google/gax": "^0.26",
"google/proto-client": "^0.26",
"ramsey/uuid": "~3"
},
"extra": {
Expand Down
7 changes: 5 additions & 2 deletions src/Spanner/SpannerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\Exception\GoogleException;
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Core\Int64;
use Google\Cloud\Core\Iterator\ItemIterator;
Expand Down Expand Up @@ -80,7 +81,8 @@ class SpannerClient
private $returnInt64AsObject;

/**
* Create a Spanner client.
* Create a Spanner client. Please note that this client requires
* [the gRPC extension](https://cloud.google.com/php/grpc).
*
* @param array $config [optional] {
* Configuration Options.
Expand Down Expand Up @@ -108,10 +110,11 @@ class SpannerClient
* returned as a {@see Google\Cloud\Core\Int64} object for 32 bit
* platform compatibility. **Defaults to** false.
* }
* @throws Google\Cloud\Core\Exception\GoogleException
* @throws GoogleException
*/
public function __construct(array $config = [])
{
$this->requireGrpc();
$config += [
'scopes' => [
self::FULL_CONTROL_SCOPE,
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/Core/ClientTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ public function dependencyStatusProvider()
];
}

public function testRequireGrpcPassesWithGrpc()
{
$this->assertNull(
(new ClientTraitStubGrpcDependencyChecks(true))
->runRequireGrpc()
);
}

/**
* @expectedException Google\Cloud\Core\Exception\GoogleException
*/
public function testRequireGrpcThrowsExceptionWithoutGrpc()
{
(new ClientTraitStubGrpcDependencyChecks(false))
->runRequireGrpc();
}

public function testConfigureAuthentication()
{
$keyFilePath = __DIR__ . '/../fixtures/json-key-fixture.json';
Expand Down Expand Up @@ -252,6 +269,11 @@ public function runDetectProjectId($config)
{
return $this->detectProjectId($config);
}

public function runRequireGrpc()
{
return $this->requireGrpc();
}
}

class ClientTraitStubOnGce extends ClientTraitStub
Expand Down Expand Up @@ -287,7 +309,7 @@ public function __construct($dependencyStatus)
$this->dependencyStatus = $dependencyStatus;
}

protected function getGrpcDependencyStatus()
protected function isGrpcLoaded()
{
return $this->dependencyStatus;
}
Expand Down

0 comments on commit f4bd25c

Please sign in to comment.