-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-builder
executable file
·69 lines (62 loc) · 2.24 KB
/
generate-builder
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
#!/usr/bin/env php
<?php
function findAutoload(): string
{
foreach (['/../../autoload.php', '/../vendor/autoload.php', '/vendor/autoload.php'] as $usableFilePath) {
$fullUsableFilePath = __DIR__ . $usableFilePath;
if (file_exists($fullUsableFilePath)) {
return $fullUsableFilePath;
}
}
fwrite(STDERR, 'You need to use composer to use this binary, learn more on https://getcomposer.org/');
exit(1);
}
require findAutoload();
use Nati\BuilderGenerator\FileBuilderGenerator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Style\SymfonyStyle;
$strategies = FileBuilderGenerator::strategies()->shortnames();
$generate = function (InputInterface $input, OutputInterface $output) {
try {
FileBuilderGenerator::create(new ConsoleLogger($output))
->generateFrom(
$input->getArgument('filepath'),
$input->getOption('strategy'),
(bool)$input->getOption('faker')
);
} catch (Throwable $e) {
(new SymfonyStyle($input, $output))
->getErrorStyle()
->error('Error while generating builder, ' . $e->getMessage());
exit(1);
}
};
(new SingleCommandApplication())
->setName('generate-builder')
->setVersion('2.x')
->addArgument('filepath', InputArgument::REQUIRED, 'The filepath to the class you want to generate a builder on')
->addOption(
'strategy',
null,
InputOption::VALUE_REQUIRED,
sprintf(
'Strategy used to build class among "%s" - will be automatically detected if none',
implode('", "', $strategies)
),
null,
$strategies
)
->addOption(
'faker',
null,
InputOption::VALUE_NONE,
'Use faker to initialize properties with more relevant random data',
null
)
->setCode($generate)
->run();