Skip to content
Merged
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
9 changes: 4 additions & 5 deletions bin/compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@

require_once __DIR__ . '/../vendor/autoload.php';

use Thesis\Protobuf;
use Thesis\Protobuf\Reflection;
use Thesis\Protoc;
use Thesis\Protoc\Io;
use Thesis\Protoc\Plugin;

$protobuf = Protoc\ProtobufEncoder::default();

$entrypoint = new Protoc\Entrypoint(
new Plugin\Compiler(),
new Protobuf\Serializer(),
Reflection\Reflector::build(),
new Plugin\Compiler($protobuf),
$protobuf,
);

$entrypoint->run(
Expand Down
16 changes: 4 additions & 12 deletions src/Entrypoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Google\Protobuf\Compiler\CodeGeneratorRequest;
use Google\Protobuf\Compiler\CodeGeneratorResponse;
use Thesis\Protobuf;
use Thesis\Protobuf\Reflection;

/**
* @api
Expand All @@ -16,20 +14,16 @@
{
public function __construct(
private Plugin\Compiler $compiler,
private Protobuf\Serializer $serializer,
private Reflection\Reflector $reflector,
private ProtobufEncoder $protobuf,
) {}

public function run(
ReadInput $input,
WriteOutput $output,
): void {
try {
$request = $this->reflector->map(
$this->serializer->deserialize(
$this->reflector->type(CodeGeneratorRequest::class),
$input->read(),
),
$request = $this->protobuf->decode(
$input->read(),
CodeGeneratorRequest::class,
);

Expand All @@ -44,9 +38,7 @@ public function run(
);
}

$buffer = $this->serializer->serialize(
$this->reflector->message($response),
);
$buffer = $this->protobuf->encode($response);

if ($buffer !== '') {
$output->write($buffer);
Expand Down
40 changes: 40 additions & 0 deletions src/Plugin/AutoloadFunctionGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Thesis\Protoc\Plugin;

use Google\Protobuf\Compiler\CodeGeneratorResponse;
use Thesis\Protoc\Plugin\Generator\FileFactory;

/**
* @api
*/
final readonly class AutoloadFunctionGenerator
{
public function __construct(
private FileFactory $files,
) {}

/**
* @param list<string> $descriptors
*/
public function generateAutoloadFile(array $descriptors): CodeGeneratorResponse\File
{
return $this->files->create(
code: \sprintf(
<<<'PHP'
\Thesis\Protobuf\Pool\Registry::get()->register(
%s,
);

PHP,
implode(",\n", array_map(
static fn(string $descriptor) => " new \\Thesis\\Protobuf\\Pool\\OnceRegistrar(new \\{$descriptor}())",
$descriptors,
)),
),
path: 'autoload.metadata',
);
}
}
85 changes: 85 additions & 0 deletions src/Plugin/ClassLikeGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Thesis\Protoc\Plugin;

use Google\Protobuf\Compiler\CodeGeneratorResponse;
use Thesis\Protoc\Plugin\Generator\FileFactory;

/**
* @api
*/
final readonly class ClassLikeGenerator
{
private Generator\GrpcGenerator $grpc;

private Generator\ProtoGenerator $proto;

private Generator\DescriptorMetadataRegistryGenerator $metadata;

public function __construct(
string $namespace,
private FileFactory $files,
Dependency\Graph $graph,
NameIndex $index,
?string $package = null,
?string $syntax = null,
) {
$namespacer = new Generator\PhpNamespacer($namespace);
$this->grpc = new Generator\GrpcGenerator(
$namespacer,
$graph,
$package,
);
$this->proto = new Generator\ProtoGenerator(
$graph,
$index,
$namespacer,
$syntax,
);
$this->metadata = new Generator\DescriptorMetadataRegistryGenerator(
$namespacer,
);
}

/**
* @return iterable<CodeGeneratorResponse\File>
*/
public function generateMessages(Parser\MessageDescriptor $message): iterable
{
foreach ($this->proto->generateMessages($message) as $path => $namespace) {
yield $this->files->create($namespace, $path);
}
}

public function generateEnum(Parser\EnumDescriptor $enum): CodeGeneratorResponse\File
{
return $this->files->create($this->proto->generateEnum($enum), $enum->path);
}

public function generateGrpcClient(Parser\ServiceDescriptor $service): CodeGeneratorResponse\File
{
return $this->files->create($this->grpc->generateClient($service), "{$service->path}Client");
}

/**
* @return iterable<CodeGeneratorResponse\File>
*/
public function generateGrpcServer(Parser\ServiceDescriptor $service): iterable
{
yield $this->files->create($this->grpc->generateServer($service), "{$service->path}Server");

if ($service->methods !== []) {
yield $this->files->create($this->grpc->generateServerRegistry($service), "{$service->path}ServerRegistry");
}
}

public function generateDescriptorMetadataRegistry(
NameIndex $index,
string $descriptorName,
string $buffer,
): CodeGeneratorResponse\File {
return $this->files->create($this->metadata->generate($index, $descriptorName, $buffer), $descriptorName);
}
}
67 changes: 60 additions & 7 deletions src/Plugin/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use Google\Protobuf\Compiler\CodeGeneratorResponse;
use Thesis\Package;
use Thesis\Protoc\Exception\CodeCannotBeGenerated;
use Thesis\Protoc\Plugin\Generator\FileFactory;
use Thesis\Protoc\Plugin\Parser\FileDescriptor;
use Thesis\Protoc\Plugin\Parser\MessageDescriptor;
use Thesis\Protoc\ProtobufEncoder;
use Thesis\Protoc\ProtocException;

/**
Expand All @@ -24,8 +26,9 @@

private Parser $parser;

public function __construct()
{
public function __construct(
private ProtobufEncoder $protobuf,
) {
$this->parser = new Parser();
}

Expand Down Expand Up @@ -58,14 +61,21 @@ private function doGenerate(

$registry = new Dependency\Registry($request);

$descriptorPaths = new PathTable();

foreach ($request as $source => $proto) {
$phpNamespace = self::determinePhpNamespace($proto, $options);

$generator = new Generator(
$index = new NameIndex();

$generator = new ClassLikeGenerator(
namespace: $phpNamespace,
path: $options->srcPath ?? str_replace('\\', '/', $phpNamespace),
generatedDoc: self::createGeneratedDoc($request, $source),
files: new FileFactory(
self::createClassLikeGeneratedDoc($request, $source),
$path = $options->srcPath ?? str_replace('\\', '/', $phpNamespace),
),
graph: $registry->graph($source),
index: $index,
package: $proto->package,
syntax: $proto->syntax,
);
Expand All @@ -85,13 +95,36 @@ private function doGenerate(
foreach ($proto->messages as $descriptor) {
yield from $this->doGenerateMessages($generator, $descriptor);
}

if (!$index->empty()) {
$descriptorName = Naming::descriptorName($source);

yield $generator->generateDescriptorMetadataRegistry(
$index,
$descriptorName,
$this->protobuf->encode($proto->file),
);

$descriptorPaths->addRelation("{$phpNamespace}\\{$descriptorName}", $path);
}
}

foreach ($descriptorPaths->groupByNamespace() as $ns => $descriptors) {
$generator = new AutoloadFunctionGenerator(
new FileFactory(
self::createAutoloadFileGeneratedDoc($request),
$options->srcPath ?? str_replace('\\', '/', $ns),
),
);

yield $generator->generateAutoloadFile($descriptors);
}
}

/**
* @return iterable<CodeGeneratorResponse\File>
*/
private function doGenerateMessages(Generator $generator, MessageDescriptor $descriptor): iterable
private function doGenerateMessages(ClassLikeGenerator $generator, MessageDescriptor $descriptor): iterable
{
if (!($descriptor->options?->mapEntry === true)) {
yield from $generator->generateMessages($descriptor);
Expand Down Expand Up @@ -135,7 +168,7 @@ private static function determinePhpNamespace(
/**
* @return non-empty-string
*/
private static function createGeneratedDoc(
private static function createClassLikeGeneratedDoc(
Parser\Request $request,
string $source,
): string {
Expand All @@ -153,6 +186,26 @@ private static function createGeneratedDoc(
);
}

/**
* @return non-empty-string
*/
private static function createAutoloadFileGeneratedDoc(
Parser\Request $request,
): string {
return \sprintf(
<<<'DOC'
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v%s
* protoc — v%s
*/
DOC,
Package\version(self::PLUGIN_NAME),
self::createCompilerVersion($request),
);
}

private static function createCompilerVersion(Parser\Request $request): string
{
$compilerVersion = $request->request->compilerVersion;
Expand Down
Loading