1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SLLH\IsoCodesValidator\Dev\Console\Command; |
4
|
|
|
|
5
|
|
|
use Gnugat\NomoSpaco\File\FileRepository; |
6
|
|
|
use Gnugat\NomoSpaco\FqcnRepository; |
7
|
|
|
use Gnugat\NomoSpaco\Token\ParserFactory; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Sullivan Senechal <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class GenerateCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* IsoCodes classes to not generate as validator. |
19
|
|
|
* |
20
|
|
|
* Generally abstract classes, interfaces or deprecations. |
21
|
|
|
* |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
private $excludedClasses = [ |
25
|
|
|
'Luhn', |
26
|
|
|
'Gtin', |
27
|
|
|
'Dun14', |
28
|
|
|
'Itf14', |
29
|
|
|
'Upca', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
protected function configure() |
36
|
|
|
{ |
37
|
|
|
$this |
38
|
|
|
->setName('generate') |
39
|
|
|
; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
$isoCodePath = __DIR__.'/../../../vendor/ronanguilloux/isocodes'; |
48
|
|
|
if (!file_exists($isoCodePath.'/.git')) { |
49
|
|
|
$output->writeln( |
50
|
|
|
'<error>No .git directory found on ronanguilloux/isocodes package. Please run rm -r vendor/ronanguilloux/isocodes && composer update ronanguilloux/isocodes --prefer-source command.</error>' |
|
|
|
|
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
return 1; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$fqcnRepository = new FqcnRepository(new FileRepository(), new ParserFactory()); |
57
|
|
|
|
58
|
|
|
$isoCodesClasses = array_diff( |
59
|
|
|
array_map(function ($fqcn) { |
60
|
|
|
return str_replace('IsoCodes\\', '', $fqcn); |
61
|
|
|
}, $fqcnRepository->findIn(__DIR__.'/../../../vendor/ronanguilloux/isocodes/src/IsoCodes')), |
62
|
|
|
$this->excludedClasses |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$constraintClasses = array_filter(array_map(function ($fqcn) { |
66
|
|
|
return str_replace('SLLH\\IsoCodesValidator\\Constraints\\', '', $fqcn); |
67
|
|
|
}, $fqcnRepository->findIn(__DIR__.'/../../../src/Constraints')), function ($className) { |
68
|
|
|
return !empty(trim($className)); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
$twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__.'/../..')); |
72
|
|
|
|
73
|
|
|
foreach (array_udiff($isoCodesClasses, $constraintClasses, 'strcasecmp') as $className) { |
74
|
|
|
$classVersion = str_replace( |
75
|
|
|
'v', |
76
|
|
|
'', |
77
|
|
|
exec("git -C ${isoCodePath} tag --list --contains $(git -C ${isoCodePath} log --pretty=format:\"%h\" --diff-filter=A src/IsoCodes/${className}.php) | head -n 1") |
|
|
|
|
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$output->writeln("Generate <comment>${className}</comment> constraint (<info>v${classVersion}</info>)."); |
81
|
|
|
|
82
|
|
|
file_put_contents( |
83
|
|
|
__DIR__."/../../../src/Constraints/${className}.php", |
84
|
|
|
$twig->render('templates/Constraint.php.twig', [ |
85
|
|
|
'class_name' => $className, |
86
|
|
|
'class_version' => $classVersion, |
87
|
|
|
]) |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
file_put_contents( |
91
|
|
|
__DIR__."/../../../tests/Constraints/${className}ValidatorTest.php", |
92
|
|
|
$twig->render('templates/ConstraintValidatorTest.php.twig', [ |
93
|
|
|
'class_name' => $className, |
94
|
|
|
]) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return 0; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.