-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add new code generator from AVRO to PHP #3708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e2b764c
84e7264
5ce7149
8147d94
cb41450
d5794a6
662f7d2
2366b1c
3ca4e2f
54fde5a
53b5da7
456961b
88fdcdc
56ac403
067dca1
cd9b00d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,52 @@ | ||||||||||||||
| #!/usr/bin/env php | ||||||||||||||
| <?php | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Licensed to the Apache Software Foundation (ASF) under one | ||||||||||||||
| * or more contributor license agreements. See the NOTICE file | ||||||||||||||
| * distributed with this work for additional information | ||||||||||||||
| * regarding copyright ownership. The ASF licenses this file | ||||||||||||||
| * to you under the Apache License, Version 2.0 (the | ||||||||||||||
| * "License"); you may not use this file except in compliance | ||||||||||||||
| * with the License. You may obtain a copy of the License at | ||||||||||||||
| * | ||||||||||||||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||
| * | ||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||
| * limitations under the License. | ||||||||||||||
| */ | ||||||||||||||
|
|
||||||||||||||
| declare(strict_types=1); | ||||||||||||||
|
|
||||||||||||||
| const _AVRO_AUTOLOADER_LOCATIONS = [ | ||||||||||||||
| __DIR__ . '/../../autoload.php', | ||||||||||||||
| __DIR__ . '/../vendor/autoload.php', | ||||||||||||||
| __DIR__ . '/../../../vendor/autoload.php', | ||||||||||||||
| __DIR__ . '/vendor/autoload.php' | ||||||||||||||
| ]; | ||||||||||||||
|
|
||||||||||||||
| foreach (_AVRO_AUTOLOADER_LOCATIONS as $autoloader) { | ||||||||||||||
| if (file_exists($autoloader)) { | ||||||||||||||
| require $autoloader; | ||||||||||||||
| break; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (!class_exists(\Apache\Avro\Console\GenerateCommand::class)) { | ||||||||||||||
| fwrite(STDERR, "Error: Composer autoloader not found. Run 'composer install' first.\n"); | ||||||||||||||
| exit(1); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| use Apache\Avro\Console\GenerateCommand; | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should those imports be upper in the file ? |
||||||||||||||
| use Composer\InstalledVersions; | ||||||||||||||
| use Symfony\Component\Console\Application; | ||||||||||||||
|
|
||||||||||||||
| $version = InstalledVersions::getPrettyVersion('apache/avro'); | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| $app = new Application('avro', $version); | ||||||||||||||
| $app->addCommand(new GenerateCommand()); | ||||||||||||||
| $app->setDefaultCommand('generate', true); | ||||||||||||||
| $app->run(); | ||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,149 @@ | ||||||
| <?php | ||||||
|
|
||||||
| /** | ||||||
| * Licensed to the Apache Software Foundation (ASF) under one | ||||||
| * or more contributor license agreements. See the NOTICE file | ||||||
| * distributed with this work for additional information | ||||||
| * regarding copyright ownership. The ASF licenses this file | ||||||
| * to you under the Apache License, Version 2.0 (the | ||||||
| * "License"); you may not use this file except in compliance | ||||||
| * with the License. You may obtain a copy of the License at | ||||||
| * | ||||||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| * See the License for the specific language governing permissions and | ||||||
| * limitations under the License. | ||||||
| */ | ||||||
|
|
||||||
| declare(strict_types=1); | ||||||
|
|
||||||
| namespace Apache\Avro\Console; | ||||||
|
|
||||||
| use Apache\Avro\Generator\AvroCodeGenerator; | ||||||
| use Apache\Avro\Schema\AvroSchema; | ||||||
| use Symfony\Component\Console\Attribute\AsCommand; | ||||||
| use Symfony\Component\Console\Command\Command; | ||||||
| use Symfony\Component\Console\Input\InputInterface; | ||||||
| use Symfony\Component\Console\Input\InputOption; | ||||||
| use Symfony\Component\Console\Output\OutputInterface; | ||||||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||||||
|
|
||||||
| #[AsCommand( | ||||||
| name: 'generate', | ||||||
| description: 'Generate PHP classes from Avro schema files', | ||||||
| )] | ||||||
| class GenerateCommand extends Command | ||||||
| { | ||||||
| protected function configure(): void | ||||||
| { | ||||||
| $this | ||||||
| ->addOption('file', 'f', InputOption::VALUE_OPTIONAL, 'One Avro schema file (.avsc)') | ||||||
| ->addOption('directory', 'd', InputOption::VALUE_OPTIONAL, 'A directory containing multiple Avro schema files (.avsc)') | ||||||
| ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Output directory for generated PHP files') | ||||||
| ->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED, 'PHP namespace for generated classes'); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Usually the short options are single character
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The -n is already taken by the Symfony Console Component We could use a capital N, wdyt?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||||||
| } | ||||||
|
|
||||||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||||||
| { | ||||||
| $io = new SymfonyStyle($input, $output); | ||||||
|
|
||||||
| /** @var string $outputDir */ | ||||||
| $outputDir = $input->getOption('output'); | ||||||
| /** @var string $namespace */ | ||||||
| $namespace = $input->getOption('namespace'); | ||||||
|
|
||||||
| /** @var null|string $file */ | ||||||
| $file = $input->getOption('file'); | ||||||
| /** @var null|string $directory */ | ||||||
| $directory = $input->getOption('directory'); | ||||||
|
|
||||||
| if ( | ||||||
| (null === $file && null === $directory) | ||||||
| || (null !== $file && null !== $directory) | ||||||
| ) { | ||||||
| $io->error('You must provide a file path or a directory'); | ||||||
|
|
||||||
| return Command::FAILURE; | ||||||
| } | ||||||
|
|
||||||
| if (null === $outputDir || '' === $outputDir) { | ||||||
| $io->error('Output directory is required (--output / -o).'); | ||||||
|
|
||||||
| return Command::FAILURE; | ||||||
| } | ||||||
|
|
||||||
| if (null === $namespace || '' === $namespace) { | ||||||
| $io->error('PHP namespace is required (--namespace / -ns).'); | ||||||
|
|
||||||
| return Command::FAILURE; | ||||||
| } | ||||||
|
|
||||||
| if (!is_dir($outputDir) && !mkdir($outputDir, 0755, true) && !is_dir($outputDir)) { | ||||||
| $io->error(sprintf('Could not create output directory "%s".', $outputDir)); | ||||||
|
|
||||||
| return Command::FAILURE; | ||||||
| } | ||||||
|
|
||||||
| $outputDir = rtrim((string) realpath($outputDir), '/'); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| $files = []; | ||||||
| if (null !== $file) { | ||||||
| $files[] = $file; | ||||||
| } elseif (null !== $directory) { | ||||||
| if (!is_dir($directory)) { | ||||||
| $io->error(sprintf('Directory not found: %s', $directory)); | ||||||
|
|
||||||
| return Command::FAILURE; | ||||||
| } | ||||||
| $files = glob(rtrim($directory, '/').'/*.avsc') ?: []; | ||||||
| } | ||||||
|
|
||||||
| $generator = new AvroCodeGenerator(); | ||||||
| $written = []; | ||||||
| $exitCode = Command::SUCCESS; | ||||||
|
|
||||||
| foreach ($files as $file) { | ||||||
| if (!file_exists($file)) { | ||||||
| $io->error(sprintf('File not found: %s', $file)); | ||||||
| $exitCode = Command::FAILURE; | ||||||
|
|
||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| $json = file_get_contents($file); | ||||||
| if (false === $json) { | ||||||
| $io->error(sprintf('Could not read file: %s', $file)); | ||||||
| $exitCode = Command::FAILURE; | ||||||
|
|
||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| try { | ||||||
| $schema = AvroSchema::parse($json); | ||||||
| $generatedFiles = $generator->translate($schema, $outputDir, $namespace); | ||||||
|
|
||||||
| foreach ($generatedFiles as $path => $content) { | ||||||
| if (false === file_put_contents($path, $content)) { | ||||||
| $io->error(sprintf('Could not write file: %s', $path)); | ||||||
| $exitCode = Command::FAILURE; | ||||||
|
|
||||||
| continue; | ||||||
| } | ||||||
| $written[] = $path; | ||||||
| } | ||||||
| } catch (\Throwable $e) { | ||||||
| $io->error(sprintf('Error processing %s: %s', $file, $e->getMessage())); | ||||||
| $exitCode = Command::FAILURE; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if ([] !== $written) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An empty |
||||||
| $io->listing($written); | ||||||
| $io->success(sprintf('%d file(s) generated in %s.', count($written), $outputDir)); | ||||||
| } | ||||||
|
|
||||||
| return $exitCode; | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some documentation about the purpose of this file.