Skip to content

Commit

Permalink
Merge pull request #50 from bigcommerce/interceptor-improvements
Browse files Browse the repository at this point in the history
interceptor improvements, drop php 7.3
  • Loading branch information
splittingred authored Jul 29, 2022
2 parents 01d8905 + daefe96 commit 338cfc5
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 21 deletions.
12 changes: 6 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
parameters:
php-version:
type: string
default: 7.3-fpm
default: 7.4-fpm
executor:
name: php/php
php-version: << parameters.php-version >>
Expand All @@ -27,7 +27,7 @@ jobs:
parameters:
php-version:
type: string
default: 7.3-fpm
default: 7.4-fpm
executor:
name: php/php
php-version: << parameters.php-version >>
Expand All @@ -44,7 +44,7 @@ jobs:
parameters:
php-version:
type: string
default: 7.3-fpm
default: 7.4-fpm
executor:
name: php/php
php-version: << parameters.php-version >>
Expand All @@ -65,12 +65,12 @@ workflows:
- tests-unit:
matrix:
parameters:
php-version: [ "7.3-fpm", "7.4-fpm", "8.0-fpm" ]
php-version: [ "7.4-fpm", "8.0-fpm" ]
- codesniffer:
matrix:
parameters:
php-version: [ "7.3-fpm", "7.4-fpm", "8.0-fpm" ]
php-version: [ "7.4-fpm", "8.0-fpm" ]
- cs-fixer:
matrix:
parameters:
php-version: [ "7.3-fpm", "7.4-fpm", "8.0-fpm" ]
php-version: [ "7.4-fpm", "8.0-fpm" ]
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ Changelog for grphp.

### Pending Release

### 3.3.0

* Add `getFullyQualifiedMethodName` and `getExpectedResponseMessageClass` to base Client Interceptor class
* Drop PHP 7.3 support

### 3.2.2

* Add header `TE: trailers` for envoy requests.

### 3.2.1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ preserving gRPC BadStatus codes
* Client execution timings in responses
* H2Proxy via nghttpx support that allows gRPC-based communication without the gRPC C libraries

grphp currently has active support for gRPC 1.9.0, and requires PHP 7.0+ to run.
grphp currently has active support for gRPC 1.9.0, and requires PHP 7.4+ to run.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "gRPC PHP Framework",
"license": "MIT",
"require": {
"php": "^7.3 || ^8.0",
"php": "^7.4 || ^8.0",
"google/protobuf": "^3.7",
"grpc/grpc": "^1.13"
},
Expand Down
6 changes: 3 additions & 3 deletions src/Grphp/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class Client
/** @var BaseStub $client */
protected $client;
/** @var Config $config */
protected $config;
protected Config $config;
/** @var array<BaseInterceptor> $interceptors */
protected $interceptors = [];
protected array $interceptors = [];
/** @var string */
private $clientClassName;
private string $clientClassName;

/**
* @param string $clientClassName
Expand Down
59 changes: 50 additions & 9 deletions src/Grphp/Client/Interceptors/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
declare(strict_types = 1);
declare(strict_types=1);

namespace Grphp\Client\Interceptors;

Expand All @@ -31,13 +31,13 @@
abstract class Base
{
/** @var array */
protected $options = [];
protected array $options = [];
/** @var Message */
protected $request;
/** @var string */
protected $method;
protected string $method;
/** @var array */
protected $metadata = [];
protected array $metadata = [];
/** @var BaseStub */
protected $stub;

Expand Down Expand Up @@ -72,6 +72,47 @@ public function setRequest(&$request)
$this->request = $request;
}

/**
* Gets the fully qualified method name, e.g. grphp.catalog.products/GetProduct
*
* @return string
* @throws StubNotFoundException
*/
public function getFullyQualifiedMethodName(): string
{
$methodName = $this->getMethod();
$stub = $this->getStub();
if (empty($stub)) {
throw new StubNotFoundException("Stub not found for $methodName");
}

return $stub->getServiceName() . '/' . ucfirst($methodName);
}

/**
* Get the expected response protobuf message class
*
* @return string
* @throws StubNotFoundException
* @throws ResponseMessageLookupFailedException
*/
public function getExpectedResponseMessageClass(): string
{
$methodName = $this->getMethod();
$stub = $this->getStub();
if (empty($stub)) {
throw new StubNotFoundException("Stub not found for $methodName");
}

$responseMessages = $stub->getExpectedResponseMessages();
$methodName = lcfirst($methodName);

if (!array_key_exists($methodName, $responseMessages)) {
throw new ResponseMessageLookupFailedException();
}
return $responseMessages[$methodName];
}

/**
* @return string
*/
Expand All @@ -84,7 +125,7 @@ public function getMethod(): string
* @param string $method
* @return void
*/
public function setMethod(string &$method)
public function setMethod(string &$method): void
{
$this->method = $method;
}
Expand All @@ -101,7 +142,7 @@ public function getMetadata(): array
* @param array $metadata
* @return void
*/
public function setMetadata(array &$metadata = [])
public function setMetadata(array &$metadata = []): void
{
$this->metadata = $metadata;
}
Expand All @@ -118,7 +159,7 @@ public function getOptions(): array
* @param array $options
* @return void
*/
public function setOptions(array &$options = [])
public function setOptions(array &$options = []): void
{
$this->options = $options;
}
Expand All @@ -136,13 +177,13 @@ public function getOption(string $k, $default = null)
/**
* @return BaseStub
*/
public function getStub(): BaseStub
public function getStub(): ?BaseStub
{
return $this->stub;
}

/**
* @param $stub
* @param BaseStub $stub
*/
public function setStub(BaseStub &$stub)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright (c) 2017-present, BigCommerce Pty. Ltd. All rights reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
declare(strict_types=1);

namespace Grphp\Client\Interceptors;

class ResponseMessageLookupFailedException extends \Exception
{
}
24 changes: 24 additions & 0 deletions src/Grphp/Client/Interceptors/StubNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright (c) 2017-present, BigCommerce Pty. Ltd. All rights reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
declare(strict_types=1);

namespace Grphp\Client\Interceptors;

class StubNotFoundException extends \Exception
{
}
8 changes: 7 additions & 1 deletion tests/Support/Compiled/ThingsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@

class ThingsClient extends \Grpc\BaseStub
{
public function getExpectedResponseMessages()
public function getExpectedResponseMessages(): array
{
return [
'getThing' => '\Grphp\Test\GetThingResp',
];
}

public function getServiceName(): string
{
return 'grphp.test.Things';
}

protected $channel;

/**
* @param string $hostname hostname
* @param array $opts channel options
* @param \Grpc\Channel $channel (optional) re-use channel object
* @throws \Exception
*/
public function __construct($hostname, $opts, $channel = null)
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Support/TestInterceptors.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ public function call(callable $callback)
return $callback();
}
}

class TestMetadataSetInterceptor extends Base
{
public function call(callable $callback)
{
$md = $this->getMetadata();
$md['test'] = 'one';
$this->setMetadata($md);
return $callback();
}
}
Loading

0 comments on commit 338cfc5

Please sign in to comment.