Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit b09bb8d

Browse files
authored
PHP8 support, refactoring
2 parents da59a93 + 9fb357a commit b09bb8d

30 files changed

+1189
-421
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ vendor/
66
clover.xml
77
example/vendor/
88
go.sum
9-
builds/
9+
builds/
10+
.phpunit.result.cache

.travis.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ install:
1515
- popd
1616
- export GO111MODULE=on
1717
- go mod download
18-
- composer install --no-interaction --prefer-source --ignore-platform-reqs
18+
- composer install --no-interaction --prefer-source
1919
- go get
2020

2121
script:
2222
- go test -race -v -coverprofile=grpc.txt -covermode=atomic
2323
- go test ./parser -race -v -coverprofile=parser.txt -covermode=atomic
2424
- go test -race -v -coverprofile=protoc-gen-php-grpc.txt ./cmd/protoc-gen-php-grpc -covermode=atomic
25-
- composer require phpunit/phpunit:~7.0 --ignore-platform-reqs
2625
- vendor/bin/spiral-cs check src tests/GRPC
2726
- vendor/bin/phpunit --coverage-clover=coverage.xml
2827
- go build cmd/protoc-gen-php-grpc/main.go
@@ -62,3 +61,12 @@ jobs:
6261
- sudo cp `which php7.4` `which php`
6362
- php -v
6463
- composer self-update
64+
- stage: Test
65+
env: "PHP=8.0"
66+
before_install:
67+
- sudo add-apt-repository -y ppa:ondrej/php
68+
- sudo apt-get update
69+
- sudo apt-get install -y php8.0-cli php8.0-xml php8.0-xdebug
70+
- sudo cp `which php8.0` `which php`
71+
- php -v
72+
- composer self-update

composer.json

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,55 @@
11
{
2-
"name": "spiral/php-grpc",
3-
"type": "server",
4-
"description": "High-Performance GRPC server for PHP applications",
5-
"license": "MIT",
6-
"authors": [
7-
{
8-
"name": "Anton Titov / Wolfy-J",
9-
"email": "[email protected]"
10-
}
11-
],
12-
"require": {
13-
"php": ">=7.2",
14-
"ext-json": "*",
15-
"google/protobuf": "^3.7",
16-
"spiral/roadrunner": "^1.8"
17-
},
18-
"require-dev": {
19-
"phpunit/phpunit": "~7.0",
20-
"spiral/code-style": "^1.0"
21-
},
22-
"autoload": {
23-
"psr-4": {
24-
"Spiral\\GRPC\\": "src/"
25-
}
26-
},
27-
"autoload-dev": {
28-
"psr-4": {
29-
"": "tests/src/",
30-
"Spiral\\GRPC\\Tests\\": "tests/GRPC/"
31-
}
32-
}
2+
"name": "spiral/php-grpc",
3+
"type": "library",
4+
"description": "High-Performance GRPC server for PHP applications",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Anton Titov / Wolfy-J",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.2",
14+
"ext-json": "*",
15+
"symfony/polyfill-php80": "^1.22",
16+
"symfony/polyfill-php73": "^1.22",
17+
"google/protobuf": "^3.7",
18+
"spiral/roadrunner": "^1.8"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "^8.5|^9.0",
22+
"spiral/code-style": "^1.0",
23+
"jetbrains/phpstorm-attributes": "^1.0",
24+
"symfony/var-dumper": ">=4.4",
25+
"vimeo/psalm": "^4.6"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"Spiral\\GRPC\\": "src"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"": "tests/src",
35+
"Spiral\\GRPC\\Tests\\": "tests/GRPC"
36+
}
37+
},
38+
"scripts": {
39+
"test": [
40+
"spiral-cs check src tests/GRPC",
41+
"psalm --no-cache",
42+
"phpunit"
43+
]
44+
},
45+
"extra": {
46+
"branch-alias": {
47+
"dev-master": "2.0.x-dev"
48+
}
49+
},
50+
"config": {
51+
"sort-packages": true
52+
},
53+
"minimum-stability": "dev",
54+
"prefer-stable": true
3355
}

example/server/composer.json

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
{
2-
"require": {
3-
"spiral/php-grpc": "^1.0"
4-
},
5-
"autoload": {
6-
"psr-4": {
7-
"": "src/"
8-
}
9-
}
2+
"name": "app/example-grpc-server",
3+
"description": "Example GRPC Server",
4+
"repositories": [
5+
{
6+
"type": "path",
7+
"url": "../.."
8+
}
9+
],
10+
"require": {
11+
"spiral/php-grpc": "*"
12+
},
13+
"require-dev": {
14+
"grpc/grpc": "^1.36"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"": "src"
19+
}
20+
},
21+
"minimum-stability": "dev",
22+
"prefer-stable": true
1023
}

example/server/worker.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,24 @@
33
* Sample GRPC PHP server.
44
*/
55

6-
use Spiral\Goridge;
7-
use Spiral\RoadRunner;
6+
use Service\EchoInterface;
7+
use Spiral\Goridge\StreamRelay;
8+
use Spiral\GRPC\Server;
9+
use Spiral\RoadRunner\Worker;
810

9-
ini_set('display_errors', 'stderr');
10-
require "vendor/autoload.php";
11+
require __DIR__ . '/vendor/autoload.php';
1112

12-
//To run server in debug mode - new \Spiral\GRPC\Server(null, ['debug' => true]);
13-
$server = new \Spiral\GRPC\Server();
14-
$server->registerService(\Service\EchoInterface::class, new EchoService());
13+
$server = new Server(null, [
14+
'debug' => false, // optional (default: false)
15+
]);
1516

16-
$w = new RoadRunner\Worker(new Goridge\StreamRelay(STDIN, STDOUT));
17-
$server->serve($w);
17+
$server->registerService(EchoInterface::class, new EchoService());
18+
19+
$worker = \method_exists(Worker::class, 'create')
20+
// RoadRunner >= 2.x
21+
? Worker::create()
22+
// RoadRunner 1.x
23+
: new Worker(new StreamRelay(STDIN, STDOUT))
24+
;
25+
26+
$server->serve($worker);

phpunit.xml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010
processIsolation="false"
1111
stopOnFailure="false"
1212
stopOnError="false"
13-
>
14-
13+
stderr="true">
1514
<testsuites>
16-
<testsuite name="High-Performance GRPC server for PHP applications">
17-
<directory>./tests/</directory>
15+
<testsuite name="Test Suite">
16+
<directory>tests</directory>
1817
</testsuite>
1918
</testsuites>
20-
2119
<filter>
2220
<whitelist>
2321
<directory>src</directory>
2422
</whitelist>
2523
</filter>
26-
</phpunit>
24+
<php>
25+
<ini name="error_reporting" value="-1"/>
26+
<ini name="memory_limit" value="-1"/>
27+
</php>
28+
</phpunit>

psalm.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns="https://getpsalm.org/schema/config"
5+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
6+
7+
errorLevel="1"
8+
hoistConstants="true"
9+
resolveFromConfigFile="true"
10+
allowPhpStormGenerics="true"
11+
findUnusedPsalmSuppress="true"
12+
findUnusedVariablesAndParams="true"
13+
ensureArrayStringOffsetsExist="true"
14+
addParamDefaultToDocblockType="true"
15+
>
16+
<issueHandlers>
17+
<RedundantConditionGivenDocblockType errorLevel="suppress" />
18+
<MixedReturnStatement errorLevel="suppress" />
19+
<UnusedPsalmSuppress errorLevel="suppress" />
20+
</issueHandlers>
21+
<projectFiles>
22+
<directory name="src" />
23+
<ignoreFiles>
24+
<directory name="vendor" />
25+
</ignoreFiles>
26+
</projectFiles>
27+
</psalm>

src/Context.php

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
<?php
22

33
/**
4-
* Spiral Framework.
4+
* This file is part of RoadRunner GRPC package.
55
*
6-
* @license MIT
7-
* @author Anton Titov (Wolfy-J)
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
88
*/
99

1010
declare(strict_types=1);
1111

1212
namespace Spiral\GRPC;
1313

14-
final class Context implements ContextInterface
14+
/**
15+
* @template-implements \IteratorAggregate<string, mixed>
16+
* @template-implements \ArrayAccess<string, mixed>
17+
*/
18+
final class Context implements ContextInterface, \IteratorAggregate, \Countable, \ArrayAccess
1519
{
16-
/** @var array */
20+
/**
21+
* @var array<string, mixed>
22+
*/
1723
private $values;
1824

1925
/**
20-
* @param array $values
26+
* @param array<string, mixed> $values
2127
*/
2228
public function __construct(array $values)
2329
{
2430
$this->values = $values;
2531
}
2632

2733
/**
28-
* @inheritdoc
34+
* {@inheritDoc}
2935
*/
3036
public function withValue(string $key, $value): ContextInterface
3137
{
@@ -36,18 +42,82 @@ public function withValue(string $key, $value): ContextInterface
3642
}
3743

3844
/**
39-
* @inheritdoc
45+
* {@inheritDoc}
46+
* @param mixed|null $default
4047
*/
41-
public function getValue(string $key)
48+
public function getValue(string $key, $default = null)
4249
{
43-
return $this->values[$key] ?? null;
50+
return $this->values[$key] ?? $default;
4451
}
4552

4653
/**
47-
* @inheritdoc
54+
* {@inheritDoc}
4855
*/
4956
public function getValues(): array
5057
{
5158
return $this->values;
5259
}
60+
61+
/**
62+
* {@inheritDoc}
63+
*/
64+
public function offsetExists($offset): bool
65+
{
66+
assert(\is_string($offset), 'Offset argument must be a type of string');
67+
68+
/**
69+
* Note: PHP Opcode optimisation
70+
* @see https://www.php.net/manual/pt_BR/internals2.opcodes.isset-isempty-var.php
71+
*
72+
* Priority use `ZEND_ISSET_ISEMPTY_VAR !0` opcode instead of `DO_FCALL 'array_key_exists'`.
73+
*/
74+
return isset($this->values[$offset]) || \array_key_exists($offset, $this->values);
75+
}
76+
77+
/**
78+
* {@inheritDoc}
79+
*/
80+
public function offsetGet($offset)
81+
{
82+
assert(\is_string($offset), 'Offset argument must be a type of string');
83+
84+
return $this->values[$offset] ?? null;
85+
}
86+
87+
/**
88+
* {@inheritDoc}
89+
*/
90+
public function offsetSet($offset, $value): void
91+
{
92+
assert(\is_string($offset), 'Offset argument must be a type of string');
93+
94+
$this->values[$offset] = $value;
95+
}
96+
97+
/**
98+
* {@inheritDoc}
99+
*/
100+
public function offsetUnset($offset): void
101+
{
102+
assert(\is_string($offset), 'Offset argument must be a type of string');
103+
104+
unset($this->values[$offset]);
105+
}
106+
107+
108+
/**
109+
* {@inheritDoc}
110+
*/
111+
public function getIterator(): \Traversable
112+
{
113+
return new \ArrayIterator($this->values);
114+
}
115+
116+
/**
117+
* {@inheritDoc}
118+
*/
119+
public function count(): int
120+
{
121+
return \count($this->values);
122+
}
53123
}

0 commit comments

Comments
 (0)