Skip to content

Commit 338cfc5

Browse files
authored
Merge pull request #50 from bigcommerce/interceptor-improvements
interceptor improvements, drop php 7.3
2 parents 01d8905 + daefe96 commit 338cfc5

11 files changed

Lines changed: 247 additions & 21 deletions

File tree

.circleci/config.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
parameters:
1010
php-version:
1111
type: string
12-
default: 7.3-fpm
12+
default: 7.4-fpm
1313
executor:
1414
name: php/php
1515
php-version: << parameters.php-version >>
@@ -27,7 +27,7 @@ jobs:
2727
parameters:
2828
php-version:
2929
type: string
30-
default: 7.3-fpm
30+
default: 7.4-fpm
3131
executor:
3232
name: php/php
3333
php-version: << parameters.php-version >>
@@ -44,7 +44,7 @@ jobs:
4444
parameters:
4545
php-version:
4646
type: string
47-
default: 7.3-fpm
47+
default: 7.4-fpm
4848
executor:
4949
name: php/php
5050
php-version: << parameters.php-version >>
@@ -65,12 +65,12 @@ workflows:
6565
- tests-unit:
6666
matrix:
6767
parameters:
68-
php-version: [ "7.3-fpm", "7.4-fpm", "8.0-fpm" ]
68+
php-version: [ "7.4-fpm", "8.0-fpm" ]
6969
- codesniffer:
7070
matrix:
7171
parameters:
72-
php-version: [ "7.3-fpm", "7.4-fpm", "8.0-fpm" ]
72+
php-version: [ "7.4-fpm", "8.0-fpm" ]
7373
- cs-fixer:
7474
matrix:
7575
parameters:
76-
php-version: [ "7.3-fpm", "7.4-fpm", "8.0-fpm" ]
76+
php-version: [ "7.4-fpm", "8.0-fpm" ]

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ Changelog for grphp.
22

33
### Pending Release
44

5+
### 3.3.0
6+
7+
* Add `getFullyQualifiedMethodName` and `getExpectedResponseMessageClass` to base Client Interceptor class
8+
* Drop PHP 7.3 support
9+
510
### 3.2.2
11+
612
* Add header `TE: trailers` for envoy requests.
713

814
### 3.2.1

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ preserving gRPC BadStatus codes
1515
* Client execution timings in responses
1616
* H2Proxy via nghttpx support that allows gRPC-based communication without the gRPC C libraries
1717

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

2020
## Installation
2121

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "gRPC PHP Framework",
55
"license": "MIT",
66
"require": {
7-
"php": "^7.3 || ^8.0",
7+
"php": "^7.4 || ^8.0",
88
"google/protobuf": "^3.7",
99
"grpc/grpc": "^1.13"
1010
},

src/Grphp/Client.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ class Client
4242
/** @var BaseStub $client */
4343
protected $client;
4444
/** @var Config $config */
45-
protected $config;
45+
protected Config $config;
4646
/** @var array<BaseInterceptor> $interceptors */
47-
protected $interceptors = [];
47+
protected array $interceptors = [];
4848
/** @var string */
49-
private $clientClassName;
49+
private string $clientClassName;
5050

5151
/**
5252
* @param string $clientClassName

src/Grphp/Client/Interceptors/Base.php

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1616
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717
*/
18-
declare(strict_types = 1);
18+
declare(strict_types=1);
1919

2020
namespace Grphp\Client\Interceptors;
2121

@@ -31,13 +31,13 @@
3131
abstract class Base
3232
{
3333
/** @var array */
34-
protected $options = [];
34+
protected array $options = [];
3535
/** @var Message */
3636
protected $request;
3737
/** @var string */
38-
protected $method;
38+
protected string $method;
3939
/** @var array */
40-
protected $metadata = [];
40+
protected array $metadata = [];
4141
/** @var BaseStub */
4242
protected $stub;
4343

@@ -72,6 +72,47 @@ public function setRequest(&$request)
7272
$this->request = $request;
7373
}
7474

75+
/**
76+
* Gets the fully qualified method name, e.g. grphp.catalog.products/GetProduct
77+
*
78+
* @return string
79+
* @throws StubNotFoundException
80+
*/
81+
public function getFullyQualifiedMethodName(): string
82+
{
83+
$methodName = $this->getMethod();
84+
$stub = $this->getStub();
85+
if (empty($stub)) {
86+
throw new StubNotFoundException("Stub not found for $methodName");
87+
}
88+
89+
return $stub->getServiceName() . '/' . ucfirst($methodName);
90+
}
91+
92+
/**
93+
* Get the expected response protobuf message class
94+
*
95+
* @return string
96+
* @throws StubNotFoundException
97+
* @throws ResponseMessageLookupFailedException
98+
*/
99+
public function getExpectedResponseMessageClass(): string
100+
{
101+
$methodName = $this->getMethod();
102+
$stub = $this->getStub();
103+
if (empty($stub)) {
104+
throw new StubNotFoundException("Stub not found for $methodName");
105+
}
106+
107+
$responseMessages = $stub->getExpectedResponseMessages();
108+
$methodName = lcfirst($methodName);
109+
110+
if (!array_key_exists($methodName, $responseMessages)) {
111+
throw new ResponseMessageLookupFailedException();
112+
}
113+
return $responseMessages[$methodName];
114+
}
115+
75116
/**
76117
* @return string
77118
*/
@@ -84,7 +125,7 @@ public function getMethod(): string
84125
* @param string $method
85126
* @return void
86127
*/
87-
public function setMethod(string &$method)
128+
public function setMethod(string &$method): void
88129
{
89130
$this->method = $method;
90131
}
@@ -101,7 +142,7 @@ public function getMetadata(): array
101142
* @param array $metadata
102143
* @return void
103144
*/
104-
public function setMetadata(array &$metadata = [])
145+
public function setMetadata(array &$metadata = []): void
105146
{
106147
$this->metadata = $metadata;
107148
}
@@ -118,7 +159,7 @@ public function getOptions(): array
118159
* @param array $options
119160
* @return void
120161
*/
121-
public function setOptions(array &$options = [])
162+
public function setOptions(array &$options = []): void
122163
{
123164
$this->options = $options;
124165
}
@@ -136,13 +177,13 @@ public function getOption(string $k, $default = null)
136177
/**
137178
* @return BaseStub
138179
*/
139-
public function getStub(): BaseStub
180+
public function getStub(): ?BaseStub
140181
{
141182
return $this->stub;
142183
}
143184

144185
/**
145-
* @param $stub
186+
* @param BaseStub $stub
146187
*/
147188
public function setStub(BaseStub &$stub)
148189
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017-present, BigCommerce Pty. Ltd. All rights reserved
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11+
* Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
declare(strict_types=1);
19+
20+
namespace Grphp\Client\Interceptors;
21+
22+
class ResponseMessageLookupFailedException extends \Exception
23+
{
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017-present, BigCommerce Pty. Ltd. All rights reserved
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11+
* Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
declare(strict_types=1);
19+
20+
namespace Grphp\Client\Interceptors;
21+
22+
class StubNotFoundException extends \Exception
23+
{
24+
}

tests/Support/Compiled/ThingsClient.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@
1919

2020
class ThingsClient extends \Grpc\BaseStub
2121
{
22-
public function getExpectedResponseMessages()
22+
public function getExpectedResponseMessages(): array
2323
{
2424
return [
2525
'getThing' => '\Grphp\Test\GetThingResp',
2626
];
2727
}
2828

29+
public function getServiceName(): string
30+
{
31+
return 'grphp.test.Things';
32+
}
33+
2934
protected $channel;
3035

3136
/**
3237
* @param string $hostname hostname
3338
* @param array $opts channel options
3439
* @param \Grpc\Channel $channel (optional) re-use channel object
40+
* @throws \Exception
3541
*/
3642
public function __construct($hostname, $opts, $channel = null)
3743
{

tests/Support/TestInterceptors.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,14 @@ public function call(callable $callback)
2626
return $callback();
2727
}
2828
}
29+
30+
class TestMetadataSetInterceptor extends Base
31+
{
32+
public function call(callable $callback)
33+
{
34+
$md = $this->getMetadata();
35+
$md['test'] = 'one';
36+
$this->setMetadata($md);
37+
return $callback();
38+
}
39+
}

0 commit comments

Comments
 (0)