-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSchemaGenerateCommand.php
81 lines (65 loc) · 2.69 KB
/
SchemaGenerateCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
declare(strict_types=1);
namespace PhpKafka\PhpAvroSchemaGenerator\Command;
use PhpKafka\PhpAvroSchemaGenerator\Generator\SchemaGenerator;
use PhpKafka\PhpAvroSchemaGenerator\Generator\SchemaGeneratorInterface;
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry;
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SchemaGenerateCommand extends Command
{
private SchemaGeneratorInterface $generator;
private ClassRegistryInterface $classRegistry;
public function __construct(
ClassRegistryInterface $classRegistry,
SchemaGeneratorInterface $generator,
string $name = null
) {
$this->classRegistry = $classRegistry;
$this->generator = $generator;
parent::__construct($name);
}
protected function configure(): void
{
$this
->setName('avro:schema:generate')
->setDescription('Generate schema from classes')
->setHelp('Experimental: Generate schema files from class files')
->addArgument('classDirectory', InputArgument::REQUIRED, 'Class directory')
->addArgument('outputDirectory', InputArgument::REQUIRED, 'Output directory')
;
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Generating schema files');
/** @var string $classDirectoryArg */
$classDirectoryArg = $input->getArgument('classDirectory');
/** @var string $outputDirectoryArg */
$outputDirectoryArg = $input->getArgument('outputDirectory');
$classDirectory = $this->getPath($classDirectoryArg);
$outputDirectory = $this->getPath($outputDirectoryArg);
$registry = $this->classRegistry->addClassDirectory($classDirectory)->load();
$this->generator->setOutputDirectory($outputDirectory);
$this->generator->setClassRegistry($registry);
$schemas = $this->generator->generate();
$result = $this->generator->exportSchemas($schemas);
// retrieve the argument value using getArgument()
$output->writeln(sprintf('Generated %d schema files', $result));
return 0;
}
/**
* @param string $path
* @return string
*/
private function getPath(string $path): string
{
$result = realpath($path);
if (false === $result || false === is_dir($result)) {
throw new \RuntimeException(sprintf('Directory not found %s', $path));
}
return $result;
}
}