Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Server registration #205

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swapped it to the new interface, because it is extending RequestHandlerInterface

{
/**
* A convenience constant that may identify URL paths.
Expand Down