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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
"ext-filter": "*",
"nette/php-generator": "4.2.1",
"thesis/package-version": "0.1.2",
"thesis/protobuf": "^0.1.6",
"thesis/protobuf": "^0.1.7",
"thesis/protobuf-compiler-types": "0.1.0",
"thesis/protobuf-descriptor-types": "0.1.0"
"thesis/protobuf-descriptor-types": "0.1.0",
"thesis/protoregistry": "^0.1.1"
},
"require-dev": {
"phpunit/phpunit": "^12.4",
Expand Down
1 change: 0 additions & 1 deletion src/Plugin/ClassLikeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function __construct(
$this->grpc = new Generator\GrpcGenerator(
$namespacer,
$graph,
$index,
$package,
);
$this->proto = new Generator\ProtoGenerator(
Expand Down
21 changes: 21 additions & 0 deletions src/Plugin/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use Google\Protobuf\Compiler\CodeGeneratorResponse;
use Thesis\Package;
use Thesis\Protobuf\Encoder;
use Thesis\Protobuf\Registry\File;
use Thesis\Protoc\Exception\CodeCannotBeGenerated;
use Thesis\Protoc\Plugin\Generator\AutoloadFunctionGenerator;
use Thesis\Protoc\Plugin\Generator\FileFactory;
use Thesis\Protoc\Plugin\Parser\FileDescriptor;
use Thesis\Protoc\Plugin\Parser\MessageDescriptor;
use Thesis\Protoc\Plugin\Parser\ServiceMethodDescriptor;
use Thesis\Protoc\ProtocException;

/**
Expand Down Expand Up @@ -84,6 +87,24 @@ private function doGenerate(
if ($options->requireGrpcServer) {
yield from $generator->generateGrpcServer($service);
}

\assert(($serviceName = ($proto->package !== null ? "{$proto->package}." : '') . $service->path) !== '');

$index->addService(new File\ServiceDescriptor(
name: $serviceName,
methods: array_map(
static function (ServiceMethodDescriptor $method): File\MethodDescriptor {
\assert($method->name !== '');

return new File\MethodDescriptor(
$method->name,
$method->clientStreaming,
$method->serverStreaming,
);
},
$service->methods,
),
));
}

yield from array_map($generator->generateEnum(...), $proto->enums);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

declare(strict_types=1);

namespace Thesis\Protoc\Plugin;
namespace Thesis\Protoc\Plugin\Generator;

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

/**
* @api
Expand All @@ -24,13 +23,13 @@ public function generateAutoloadFile(array $descriptors): CodeGeneratorResponse\
return $this->files->create(
code: \sprintf(
<<<'PHP'
\Thesis\Protobuf\Pool\Registry::get()->register(
\Thesis\Protobuf\Registry\Pool::get()->register(
%s,
);

PHP,
implode(",\n", array_map(
static fn(string $descriptor) => " new \\Thesis\\Protobuf\\Pool\\OnceRegistrar(new \\{$descriptor}())",
static fn(string $descriptor) => " new \\Thesis\\Protobuf\\Registry\\OnceRegistrar(new \\{$descriptor}())",
$descriptors,
)),
),
Expand Down
67 changes: 38 additions & 29 deletions src/Plugin/Generator/DescriptorMetadataRegistryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Nette\PhpGenerator\Method;
use Nette\PhpGenerator\Parameter;
use Nette\PhpGenerator\PhpNamespace;
use Thesis\Protobuf\Registry\File;
use Thesis\Protoc\Plugin\NameIndex;

/**
Expand Down Expand Up @@ -38,7 +39,7 @@ public function generate(
->setReadOnly()
->addComment('@api')
->setImplements([
'Pool\Registrar',
'Registry\Registrar',
])
->addMember(
new Constant('DESCRIPTOR_BUFFER')
Expand All @@ -49,53 +50,62 @@ public function generate(

$namespace->add($classType);

$namespace->addUse('Thesis\Protobuf\Pool');
$namespace->addUse('Thesis\Protobuf\Pool\File');
$namespace->addUse('Thesis\Protobuf\Registry');
$namespace->addUse('Thesis\Protobuf\Registry\File');
$namespace->addUse(\Override::class);

$method = $classType
->addMethod('register')
->setPublic()
->setParameters([
new Parameter('pool')->setType('Pool\Registry'),
new Parameter('pool')->setType('Registry\Pool'),
])
->setReturnType('void')
->addAttribute(\Override::class);

$method->addBody('$pool->add(Pool\Descriptor::base64(self::DESCRIPTOR_BUFFER), new File(');
$method->addBody('$pool->add(Registry\Descriptor::base64(self::DESCRIPTOR_BUFFER), new File(');
$method->addBody(' name: ?,', [$filename]);

self::pushParameter($method, 'dependencies', $dependencies);
self::pushParameter(
$method,
'messages',
$index->messageTypes,
static fn(string $type, string $fqcn) => new Literal(
\sprintf("new File\\MessageDescriptor('%s', \\%s::class)", $type, $fqcn),
static fn(File\MessageDescriptor $descriptor) => new Literal(
\sprintf("new File\\MessageDescriptor('%s', \\%s::class)", $descriptor->name, $descriptor->fqcn),
),
);
self::pushParameter($method, 'enums', $index->enumTypes, static fn(string $type, string $fqcn) => new Literal(
\sprintf("new File\\EnumDescriptor('%s', \\%s::class)", $type, $fqcn),
self::pushParameter($method, 'enums', $index->enumTypes, static fn(File\EnumDescriptor $descriptor) => new Literal(
\sprintf("new File\\EnumDescriptor('%s', \\%s::class)", $descriptor->name, $descriptor->fqcn),
));
self::pushParameter(
$method,
'services',
$index->grpc,
static fn(string $type, object $service) => new Literal(
vsprintf(
<<<'PHP'
new File\ServiceDescriptor(
name: '%s',
clientFqcn: %s,
serverFqcn: %s,
$index->services,
static fn(File\ServiceDescriptor $descriptor) => new Literal(
<<<'PHP'
new File\ServiceDescriptor(
name: ?,
methods: [
?
],
)
PHP,
[
$type,
isset($service->client) ? "\\{$service->client}::class" : 'null',
isset($service->server) ? "\\{$service->server}::class" : 'null',
],
),
[
$descriptor->name,
new Literal(implode(
"\n ",
array_map(
static fn(File\MethodDescriptor $method) => \sprintf(
"new File\\MethodDescriptor('%s', %s, %s),",
$method->name,
$method->clientStream ? 'true' : 'false',
$method->serverStream ? 'true' : 'false',
),
$descriptor->methods,
),
)),
],
),
);

Expand All @@ -105,26 +115,25 @@ public function generate(
}

/**
* @template K of array-key
* @template V
* @template R
* @param non-empty-string $name
* @param array<K, V> $items
* @param ?\Closure(K, V): R $format
* @param list<V> $items
* @param ?\Closure(V): R $format
*/
private static function pushParameter(
Method $method,
string $name,
array $items,
?\Closure $format = null,
): void {
$format ??= static fn(mixed $key, mixed $value) => $value;
$format ??= static fn(mixed $value) => $value;

if ($items !== []) {
$method->addBody(" {$name}: [");

foreach ($items as $key => $value) {
$method->addBody(' ?,', [$format($key, $value)]);
foreach ($items as $value) {
$method->addBody(' ?,', [$format($value)]);
}

$method->addBody(' ],');
Expand Down
8 changes: 1 addition & 7 deletions src/Plugin/Generator/GrpcGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\PromotedParameter;
use Thesis\Protoc\Plugin\Dependency;
use Thesis\Protoc\Plugin\NameIndex;
use Thesis\Protoc\Plugin\Naming;
use Thesis\Protoc\Plugin\Parser;

Expand All @@ -23,7 +22,6 @@
public function __construct(
private PhpNamespacer $namespacer,
private Dependency\Graph $graph,
private NameIndex $index,
private ?string $package = null,
) {}

Expand All @@ -33,8 +31,6 @@ public function generateClient(Parser\ServiceDescriptor $service): PhpNamespace

$name = \sprintf('%sClient', Naming::pascalCase($service->name));

$this->index->addClient(($this->package !== null ? "{$this->package}." : '') . $service->path, "{$namespace->getName()}\\{$name}");

$classType = new ClassType($name)
->setFinal()
->setReadOnly()
Expand Down Expand Up @@ -201,12 +197,10 @@ public function generateServer(Parser\ServiceDescriptor $service): PhpNamespace
{
$namespace = $this->namespacer->create($service->path);

$interfaceType = new InterfaceType($name = \sprintf('%sServer', Naming::pascalCase($service->name)))
$interfaceType = new InterfaceType(\sprintf('%sServer', Naming::pascalCase($service->name)))
->addComment('@api')
->addComment($service->comment !== null ? "\n{$service->comment}" : '');

$this->index->addServer(($this->package !== null ? "{$this->package}." : '') . $service->path, "{$namespace->getName()}\\{$name}");

$namespace->add($interfaceType);

if ($service->methods === []) {
Expand Down
56 changes: 39 additions & 17 deletions src/Plugin/Generator/ProtoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Nette\PhpGenerator\InterfaceType;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\PhpNamespace;
use Thesis\Protobuf\Registry\File;
use Thesis\Protoc\Exception\CodeCannotBeGenerated;
use Thesis\Protoc\Plugin\Dependency;
use Thesis\Protoc\Plugin\NameIndex;
Expand Down Expand Up @@ -42,7 +43,15 @@ public function generateEnum(Parser\EnumDescriptor $enum): PhpNamespace

$enumName = Naming::pascalCase($enum->name);

$this->index->addEnumType($enum->fqcn, "{$namespace->getName()}\\{$enumName}");
\assert($enum->fqcn !== '');

/** @var class-string $enumFqcn */
$enumFqcn = "{$namespace->getName()}\\{$enumName}";

$this->index->addEnumType(new File\EnumDescriptor(
$enum->fqcn,
$enumFqcn,
));

$enumType = new EnumType($enumName)
->addComment('@api')
Expand All @@ -66,7 +75,21 @@ public function generateEnum(Parser\EnumDescriptor $enum): PhpNamespace
*/
public function generateMessages(Parser\MessageDescriptor $message): iterable
{
yield $message->path => $this->generateMessage($message);
$namespace = $this->namespacer->create($message->path);

$className = Naming::pascalCase($message->name);

\assert($message->fqcn !== '');

/** @var class-string $messageFqcn */
$messageFqcn = "{$namespace->getName()}\\{$className}";

$this->index->addMessageType(new File\MessageDescriptor(
$message->fqcn,
$messageFqcn,
));

yield $message->path => $this->generateMessage($namespace, $className, $message);

$oneofByIndex = [];

Expand Down Expand Up @@ -99,14 +122,8 @@ public function generateMessages(Parser\MessageDescriptor $message): iterable
* @param list<string> $implements
* @throws CodeCannotBeGenerated
*/
private function generateMessage(Parser\MessageDescriptor $message, array $implements = []): PhpNamespace
private function generateMessage(PhpNamespace $namespace, string $className, Parser\MessageDescriptor $message, array $implements = []): PhpNamespace
{
$namespace = $this->namespacer->create($message->path);

$className = Naming::pascalCase($message->name);

$this->index->addMessageType($message->fqcn, "{$namespace->getName()}\\{$className}");

$classType = new ClassType($className)
->setFinal()
->setReadOnly()
Expand Down Expand Up @@ -325,14 +342,19 @@ private function generateOneofVariant(
],
);

yield $path => $this->generateMessage($descriptor, [
Naming::joinNamespace([
'',
$this->namespacer->namespace,
$message->path,
$interfaceName,
]),
]);
yield $path => $this->generateMessage(
$this->namespacer->create($path),
Naming::pascalCase($className),
$descriptor,
[
Naming::joinNamespace([
'',
$this->namespacer->namespace,
$message->path,
$interfaceName,
]),
],
);
}

private function parseDefaultValue(
Expand Down
Loading