|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +declare(strict_types=1); |
| 5 | + |
| 6 | +use Symfony\Component\Console\Application; |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Input\InputOption; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use Symfony\Component\Process\ExecutableFinder; |
| 12 | +use Symfony\Component\Process\Process; |
| 13 | + |
| 14 | +use function Flow\Types\DSL\type_string; |
| 15 | + |
| 16 | +require __DIR__ . '/../vendor/autoload.php'; |
| 17 | + |
| 18 | +$application = new Application('Flow-PHP - Thrift'); |
| 19 | + |
| 20 | +$application->addCommand(new class extends Command { |
| 21 | + public function configure(): void |
| 22 | + { |
| 23 | + $this |
| 24 | + ->setName('parquet') |
| 25 | + ->setDescription('Regenerates the Parquet ThriftModel classes from parquet.thrift.') |
| 26 | + ->addOption( |
| 27 | + 'thrift', |
| 28 | + null, |
| 29 | + InputOption::VALUE_REQUIRED, |
| 30 | + 'Thrift compiler binary, found in PATH when omitted', |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function execute(InputInterface $input, OutputInterface $output): int |
| 35 | + { |
| 36 | + $source = __DIR__ . '/../src/lib/parquet/src'; |
| 37 | + $idl = $source . '/Flow/Parquet/Resources/Thrift/parquet.thrift'; |
| 38 | + $models = $source . '/Flow/Parquet/ThriftModel'; |
| 39 | + |
| 40 | + $thrift = $input->getOption('thrift') === null |
| 41 | + ? (new ExecutableFinder())->find('thrift') |
| 42 | + : type_string()->assert($input->getOption('thrift')); |
| 43 | + |
| 44 | + if (!is_string($thrift) || !is_executable($thrift)) { |
| 45 | + $output->writeln('<error>Thrift compiler not found, pass an executable with --thrift</error>'); |
| 46 | + |
| 47 | + return Command::FAILURE; |
| 48 | + } |
| 49 | + |
| 50 | + if (!str_contains((string) file_get_contents($idl), 'namespace php Flow.Parquet.ThriftModel')) { |
| 51 | + $output->writeln('<error>Flow php namespace not found in thrift definition!</error>'); |
| 52 | + |
| 53 | + return Command::FAILURE; |
| 54 | + } |
| 55 | + |
| 56 | + foreach (glob($models . '/*.php') ?: [] as $file) { |
| 57 | + unlink($file); |
| 58 | + } |
| 59 | + |
| 60 | + (new Process([$thrift, '--gen', 'php', '--out', $source, $idl]))->mustRun( |
| 61 | + static fn(string $type, string $buffer) => $output->write($buffer), |
| 62 | + ); |
| 63 | + |
| 64 | + // the Thrift compiler types every field non-null; a field the IDL does not require is null when a file leaves it out |
| 65 | + foreach (glob($models . '/*.php') ?: [] as $file) { |
| 66 | + $class = 'Flow\\Parquet\\ThriftModel\\' . basename($file, '.php'); |
| 67 | + |
| 68 | + if (!class_exists($class) || !property_exists($class, '_TSPEC')) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + /** @var array<int, array{var: string, isRequired: bool}> $spec */ |
| 73 | + $spec = (new ReflectionProperty($class, '_TSPEC'))->getValue(); |
| 74 | + $model = (string) file_get_contents($file); |
| 75 | + $rewritten = $model; |
| 76 | + |
| 77 | + foreach ($spec as $field) { |
| 78 | + if ($field['isRequired']) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $rewritten = (string) preg_replace( |
| 83 | + '/(\* @var )(?!null\|)([^\n]+)(\n\s*\*\/\n\s*public \$' |
| 84 | + . preg_quote($field['var'], '/') |
| 85 | + . ' = null;)/', |
| 86 | + '$1null|$2$3', |
| 87 | + $rewritten, |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + if ($rewritten !== $model) { |
| 92 | + file_put_contents($file, $rewritten); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return Command::SUCCESS; |
| 97 | + } |
| 98 | +}); |
| 99 | + |
| 100 | +$application->run(); |
0 commit comments