Skip to content

Commit

Permalink
Simplify Server registration
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Jan 9, 2025
1 parent 80010e0 commit f4ce1f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function registerHandler(string $path, RequestHandlerInterface $handler):
$this->handlers[$path] = $handler;
}

public function registerServer(ServerInterface $server): void
{
$this->handlers[$server->getPathPrefix()] = $server;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
foreach ($this->handlers as $path => $handler) {
Expand Down
15 changes: 15 additions & 0 deletions lib/src/ServerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Twirp;

use Psr\Http\Server\RequestHandlerInterface;

interface ServerInterface extends RequestHandlerInterface
{
/**
* Returns the base service path, in the form: "/<prefix>/<package>.<Service>/"
* that is everything in a Twirp route except for the <Method>. This can be used for routing,
* for example to identify the requests that are targeted to this service in a mux.
*/
public function getPathPrefix(): string;
}
4 changes: 2 additions & 2 deletions protoc-gen-twirp_php/templates/service/Server.php.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Twirp\BaseServerHooks;
use Twirp\Context;
use Twirp\ErrorCode;
use Twirp\ServerHooks;
use Twirp\ServerInterface;

/**
* @see {{ .Service | phpServiceName .File }}
*
* Generated from protobuf service <code>{{ .Service.Desc.FullName }}</code>
*/
final class {{ .Service | phpServiceName .File }}Server implements RequestHandlerInterface
final class {{ .Service | phpServiceName .File }}Server implements ServerInterface
{
/**
* A convenience constant that may identify URL paths.
Expand Down

0 comments on commit f4ce1f3

Please sign in to comment.