Skip to content

Commit 97bd49e

Browse files
Merge pull request #228 from cybercog/add-server-with-path-prefix-interface
Simplify Server registration v2
2 parents beb3c48 + 52e8def commit 97bd49e

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

.github/workflows/ci-depot.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
php: ["7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]
21+
php: ["8.1", "8.2", "8.3", "8.4"]
2222

2323
steps:
2424
- name: Checkout repository

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
php: ["7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]
21+
php: ["8.1", "8.2", "8.3", "8.4"]
2222

2323
steps:
2424
- name: Checkout repository

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^7.3 || ^8.0",
14+
"php": "^8.1",
1515
"php-http/discovery": "^1.19",
1616
"psr/http-client": "^1.0",
1717
"psr/http-factory": "^1.0",

lib/src/Router.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public function registerHandler(string $path, RequestHandlerInterface $handler):
5252
$this->handlers[$path] = $handler;
5353
}
5454

55+
public function registerServer(ServerWithPathPrefix&RequestHandlerInterface $server): void
56+
{
57+
$this->handlers[$server->getPathPrefix()] = $server;
58+
}
59+
5560
public function handle(ServerRequestInterface $request): ResponseInterface
5661
{
5762
foreach ($this->handlers as $path => $handler) {

lib/src/ServerWithPathPrefix.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Twirp;
6+
7+
interface ServerWithPathPrefix
8+
{
9+
/**
10+
* Returns the base service path, in the form: "/<prefix>/<package>.<Service>/"
11+
* that is everything in a Twirp route except for the <Method>. This can be used for routing,
12+
* for example, to identify the requests that are targeted to this service in a mux.
13+
*/
14+
public function getPathPrefix(): string;
15+
}

protoc-gen-twirp_php/templates/service/Server.php.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ use Twirp\BaseServerHooks;
1717
use Twirp\Context;
1818
use Twirp\ErrorCode;
1919
use Twirp\ServerHooks;
20+
use Twirp\ServerWithPathPrefix;
2021

2122
/**
2223
* @see {{ .Service | phpServiceName .File }}
2324
*
2425
* Generated from protobuf service <code>{{ .Service.Desc.FullName }}</code>
2526
*/
26-
final class {{ .Service | phpServiceName .File }}Server implements RequestHandlerInterface
27+
final class {{ .Service | phpServiceName .File }}Server implements
28+
RequestHandlerInterface,
29+
ServerWithPathPrefix
2730
{
2831
/**
2932
* A convenience constant that may identify URL paths.

0 commit comments

Comments
 (0)