Skip to content

Conversation

@antonkomarev
Copy link
Contributor

Overview

Replacement for the:

Simplify registration of server, because PATH_PREFIX constant declares:

Should be used with caution, it only matches routes with the default "/twirp" prefix and default CamelCase service and method names. Use EmployeeServiceServer::getPathPrefix instead.

What this PR does / why we need it

Instead of this:

$this->router->registerHandler(
    EmployeeServiceServer::PATH_PREFIX,
    new EmployeeServiceServer(svc: $this->employeeService),
);

Or this:

$employeeServiceServer = new EmployeeServiceServer(
    svc: $this->employeeService,
);

$this->router->registerHandler(
    $employeeServiceServer->getPathPrefix(),
    $employeeServiceServer,
);

We can do:

$this->router->registerServer(
    new EmployeeServiceServer(
        svc: $this->employeeService,
    )
);

Special notes for your reviewer

I tend to add an *Interface suffix to the interface files, but didn't see that prefix in current local interfaces, so omitted it.

Does this PR introduce a user-facing change?

New ability to register server without manually define server path.

/**
* @param ServerWithPathPrefix&RequestHandlerInterface $server
*/
public function registerServer(ServerWithPathPrefix $server): void
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should have intersection types here, but they are supported since php8.1, and this will require to drop php7.4 & php8.0 support.

Suggested change
public function registerServer(ServerWithPathPrefix $server): void
public function registerServer(ServerWithPathPrefix&RequestHandlerInterface $server): void

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another option that ServerWithPathPrefix should extend RequestHandlerInterface.

Copy link
Member

Choose a reason for hiding this comment

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

Given 8.0 has been EOL for almost two years now, I don't think dropping support would be a huge issue.

I'm not against extending the request handler interface either, just make sure it's implemented by the generated servers separately as well (to follow the concept we agreed to in the other PR)

Copy link
Contributor Author

@antonkomarev antonkomarev Nov 16, 2025

Choose a reason for hiding this comment

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

@sagikazarmark I suggest to move forward and drop old PHP versions support. It will help us to use all features of the language.

In a 4 days PHP8.5 will be released.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sagikazarmark and I propose to remove deprecated \Twirp\Server class, since it's deprecated for a 4 years already.

@antonkomarev
Copy link
Contributor Author

@sagikazarmark @SpencerMalone what do you think about proposed interface name?

And do we add *Interface suffix at the end of interface name, or not? PSR packages has this suffix for example.

@SpencerMalone
Copy link

Idiomatic PHP I think is to have the suffix, even though I don't personally love it (but that's the golang dev in me talking). If it were me, I would probablyyyy leave the suffix off, but honestly don't feel strongly enough to push one way or another.

Copy link

@SpencerMalone SpencerMalone left a comment

Choose a reason for hiding this comment

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

Not a maintainer but this LGTM

@antonkomarev
Copy link
Contributor Author

@sagikazarmark what's your opinion? I'd better go with Interface suffix

@sagikazarmark
Copy link
Member

I always thought that Interface suffix is an ugly code smell people invented because they fundamentally misunderstand interfaces.

Normally I wouldn't fight it, because I know it's a standard in the PHP community, but the rest of the interfaces in the library don't use suffixes (looks like I wrote this lib during one of my rebel periods :) ), so for that reason alone (consistency), I'd say drop the suffix.

Otherwise, LGTM

@antonkomarev
Copy link
Contributor Author

@sagikazarmark renamed interface

@sagikazarmark sagikazarmark merged commit 97bd49e into twirphp:master Nov 26, 2025
15 checks passed
@sagikazarmark
Copy link
Member

Thanks @antonkomarev

Sorry it took some time for me to come around

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants