1. Go to this page and download the library: Download butschster/proto-parser library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
butschster / proto-parser example snippets
use Butschster\ProtoParser\ProtoParserFactory;
$parser = ProtoParserFactory::create();
$ast = $parser->parse(<<<'PROTO'
syntax = "proto3";
package example;
message Person {
string name = 1;
int32 id = 2;
string email = 3;
}
PROTO);
// Now you can work with the AST
echo $ast->package->name; // Outputs: example
echo $ast->topLevelDefs[0]->name; // Outputs: Person
$ast = $parser->parse(<<<'PROTO'
syntax = "proto3";
message Person {
string name = 1;
int32 id = 2;
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
}
PROTO);
$personMessage = $ast->topLevelDefs[0];
echo $personMessage->name; // Outputs: Person
foreach ($personMessage->fields as $field) {
echo "{$field->name}: {$field->type->type}\n";
}
$phoneTypeEnum = $personMessage->enums[0];
echo $phoneTypeEnum->name; // Outputs: PhoneType
$phoneNumberMessage = $personMessage->messages[0];
echo $phoneNumberMessage->name; // Outputs: PhoneNumber
use Butschster\ProtoParser\ProtoParser;
use Butschster\ProtoParser\ProtoParserFactory;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpFile;
$parser = ProtoParserFactory::create();
$ast = $parser->parse(<<<'PROTO'
syntax = "proto3";
message Person {
string name = 1;
int32 age = 2;
repeated string hobbies = 3;
}
PROTO,
);
// Generate PHP class
$file = new PhpFile();
$file->setStrictTypes();
$namespace = $file->addNamespace('App\Dto');
$class = $namespace->addClass('Person');
$class->setFinal()
->setReadOnly();
foreach ($ast->topLevelDefs[0]->fields as $field) {
$type = match ($field->type->type) {
'string' => 'string',
'int32' => 'int',
default => 'mixed',
};
$property = $class->addProperty($field->name)
->setType($type)
->setPublic();
if ($field->modifier === \Butschster\ProtoParser\Ast\FieldModifier::Repeated) {
$property->setType('array');
}
}
echo $file;
declare(strict_types=1);
namespace App\Dto;
final readonly class Person
{
public string $name;
public int $age;
public array $hobbies;
}
final readonly class ProtoNode implements NodeInterface
{
public function __construct(
public SyntaxDeclNode $syntax,
public array $imports = [],
public ?PackageDeclNode $package = null,
public array $options = [],
public array $topLevelDefs = [],
) {}
}
final readonly class SyntaxDeclNode implements NodeInterface
{
public function __construct(
public string $syntax,
public array $comments = [],
) {}
}
final readonly class ImportDeclNode implements NodeInterface
{
public function __construct(
public string $path,
public ?ImportModifier $modifier = null,
public array $comments = [],
) {}
}
final readonly class PackageDeclNode implements NodeInterface
{
public function __construct(
public string $name,
public array $comments = [],
) {}
}
final readonly class OptionDeclNode implements NodeInterface
{
public function __construct(
public ?string $name,
public array $comments = [],
public array $options = [],
) {}
}
final readonly class MessageDefNode implements NodeInterface
{
public function __construct(
public string $name,
public array $fields = [],
public array $messages = [],
public array $enums = [],
public array $comments = [],
) {}
}
final readonly class FieldDeclNode implements NodeInterface
{
public function __construct(
public FieldType $type,
public string $name,
public int $number,
public ?FieldModifier $modifier = null,
public array $options = [],
public array $comments = [],
) {}
}
final readonly class EnumDefNode implements NodeInterface
{
public function __construct(
public string $name,
public array $fields = [],
public array $comments = [],
) {}
}
final readonly class EnumFieldNode implements NodeInterface
{
public function __construct(
public string $name,
public int $number,
public array $options = [],
public array $comments = [],
) {}
}
final readonly class ServiceDefNode implements NodeInterface
{
public function __construct(
public string $name,
public array $methods = [],
public array $options = [],
public array $comments = [],
) {}
}
final readonly class RpcDeclNode implements NodeInterface
{
public function __construct(
public string $name,
public RpcMessageType $inputType,
public RpcMessageType $outputType,
public array $options = [],
public array $comments = [],
) {}
}
final readonly class MapFieldDeclNode implements NodeInterface
{
public function __construct(
public FieldType $keyType,
public FieldType $valueType,
public string $name,
public int $number,
public array $options = [],
public array $comments = [],
) {}
}
final readonly class OneofFieldNode implements NodeInterface
{
public function __construct(
public FieldType $type,
public string $name,
public int $number,
public array $options = [],
) {}
}
final readonly class CommentNode
{
public function __construct(
public string $text,
) {}
}
final readonly class ReservedNode implements NodeInterface
{
public function __construct(
public array $ranges,
) {}
}
final readonly class OptionNode
{
public function __construct(
public string $name,
public mixed $value,
public array $comments = [],
) {}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.