Skip to content

Commit

Permalink
move code to another SpannerClient.php
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouyihaiDing committed Jul 16, 2018
1 parent 09e5143 commit 9b0f8c1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 122 deletions.
20 changes: 20 additions & 0 deletions Spanner/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
use Google\Protobuf\Value;
use Grpc\UnaryCall;

use Grpc\Gcp\Config;

/**
* Connection to Cloud Spanner over gRPC
*/
Expand Down Expand Up @@ -120,6 +122,15 @@ class Grpc implements ConnectionInterface
*/
private $longRunningGrpcClients;

private function enableChonnectionManagement($conf_path)
{
// TODO: move this function to GrpcTrait.
$conf = new \Grpc\Gcp\ApiConfig();
$conf->mergeFromJsonString($string = file_get_contents($conf_path));
$config = new Config($conf);
return $config;
}

/**
* @param array $config [optional]
*/
Expand Down Expand Up @@ -152,6 +163,15 @@ public function __construct(array $config = [])
? $config['authHttpHandler']
: null
);

if (isset($config['enable-gcp'])) {
$api = 'spanner';
$conf_path = __DIR__. "/../$api.grpc.config";
$grpc_gcp_config = $this->enableChonnectionManagement($conf_path);
$grpcConfig['grpc_call_invoker'] = $grpc_gcp_config->callInvoker();
unset($config['enable-gcp']);
}

$this->spannerClient = isset($config['gapicSpannerClient'])
? $config['gapicSpannerClient']
: new SpannerClient($grpcConfig);
Expand Down
13 changes: 0 additions & 13 deletions Spanner/src/V1/Gapic/SpannerGapicClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,23 +308,10 @@ public static function parseName($formattedName, $template = null)
*/
public function __construct(array $options = [])
{
if (isset($options['enable-gcp'])) {
$api = 'spanner';
$conf_path = "./../../$api.grpc.config";
$this->enableChonnectionManagement($conf_path, &$options);
}
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
}

private function enableChonnectionManagement($conf_path, $options) {
// TODO: move this function to gax.
$conf = new \Grpc\Gcp\ApiConfig();
$conf->mergeFromJsonString($string = file_get_contents($conf_path));
$config = \Google\Cloud\Grpc($conf);
$options['grpc_call_invoker'] = $config->callInvoker();
}

/**
* Creates a new session. A session can be used to perform
* transactions that read and/or modify data in a Cloud Spanner database.
Expand Down
8 changes: 6 additions & 2 deletions Spanner/tests/Unit/SpannerClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* limitations under the License.
*/

namespace Google\Cloud\Spanner\Tests\Unit;
require_once (__DIR__ . "/../../../vendor/autoload.php");

//namespace Google\Cloud\Spanner\Tests\Unit;

use Google\Cloud\Core\Int64;
use Google\Cloud\Core\Iterator\ItemIterator;
Expand Down Expand Up @@ -59,7 +61,9 @@ public function setUp()

$this->connection = $this->prophesize(ConnectionInterface::class);
$this->client = \Google\Cloud\Core\Testing\TestHelpers::stub(SpannerClient::class, [
['projectId' => self::PROJECT]
['projectId' => self::PROJECT,
'enable-gcp' => true,
]
]);
}

Expand Down
107 changes: 0 additions & 107 deletions Spanner/tests/Unit/V1/SpannerClientEnableGCPTest.php

This file was deleted.

5 changes: 5 additions & 0 deletions spannerEnableGcpSample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
require('./vendor/autoload.php');
$spanner = new SpannerClient(['projectId' => 'ddyihai-firestore',
'enable_gcp' => true,
]);

0 comments on commit 9b0f8c1

Please sign in to comment.