99designs/twirfony


README

Twirfony 由两部分组成,代码生成和运行时 Symfony 扩展。

代码生成

代码生成是用 Golang 编写的 protoc 插件,因为 Go 的 proto 工具非常出色。

生成 twirp 接口

go install ./protoc-gen-twirp_php
protoc --twirp_php_out src --php_out src haberdasher.proto

‼️ 在 99designs 中,您应该使用 99dev twirp generate {app},并阅读https://github.com/99designs/twirpgen 上的文档

运行时

运行时组件是一个允许您将实现生成的 twirp 服务接口的类直接挂载到路由器中的 Symfony 扩展。

  1. 添加 twirfony 依赖
composer require 99designs/twirfony
  1. 注册扩展
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        return [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new AppBundle\AppBundle(),
            new Twirfony\TwirfonyBundle\TwirfonyBundle(), // add this line
        ];
    }
  1. 在 routing.yml 中注册路由器
twirp_api:
    resource: 'twirp.service_registry::loadRoutes'
    type: service
    prefix: /twirp
  1. 创建并实现您的 twirp 服务
namespace AppBundle\Service;

use AppBundle\Twirp\HaberdasherInterface;
use AppBundle\Twirp\Hat;
use AppBundle\Twirp\Size;
use Twirfony\TwirpService;

class HaberdasherService implements TwirpService, HaberdasherInterface
{
    public function makeHat(Size $size): Hat
    {
        return (new Hat)
            ->setInches($size->getInches())
            ->setColor("blue")
            ->setName("Fedora");
    }
}
  1. 注册并标记您的服务
    haberdasher_service:
        class: AppBundle\Service\HaberdasherService
        tags: ['twirp.service']