-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakeRouteProvider.php
More file actions
196 lines (164 loc) · 5.96 KB
/
MakeRouteProvider.php
File metadata and controls
196 lines (164 loc) · 5.96 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace Sofascore\PurgatoryBundle\Maker;
use Doctrine\Persistence\ManagerRegistry;
use Sofascore\PurgatoryBundle\Listener\Enum\Action;
use Sofascore\PurgatoryBundle\Maker\Util\ActionCollection;
use Sofascore\PurgatoryBundle\RouteProvider\PurgeRoute;
use Sofascore\PurgatoryBundle\RouteProvider\RouteProviderInterface;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\Question;
final class MakeRouteProvider extends AbstractMaker
{
public function __construct(
private readonly ManagerRegistry $managerRegistry,
) {
}
/**
* {@inheritDoc}
*/
public static function getCommandName(): string
{
return 'make:purgatory-provider';
}
public static function getCommandDescription(): string
{
return 'Create a purge route provider';
}
/**
* {@inheritDoc}
*/
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command->addArgument('name', InputArgument::OPTIONAL, 'The name of the Purgatory route provider class (e.g. <fg=yellow>BlogPostRouteProvider</>)');
}
/**
* {@inheritDoc}
*/
public function configureDependencies(DependencyBuilder $dependencies): void
{
}
/**
* {@inheritDoc}
*/
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
/** @var string $name */
$name = $input->getArgument('name');
if (false === $entity = $this->inputEntity($io)) {
return;
}
$variables = [
'entity' => Str::getShortClassName($entity),
];
if (null !== $actions = $this->inputActions($io)) {
$variables['actions'] = new ActionCollection($actions);
}
$classData = ClassData::create(
class: \sprintf('Purgatory\RouteProvider\%s', $name),
suffix: 'RouteProvider',
useStatements: [
RouteProviderInterface::class,
PurgeRoute::class,
Action::class,
$entity,
],
);
$generator->generateClassFromClassData(
classData: $classData,
templateName: __DIR__.'/../../templates/maker/RouteProvider.tpl.php',
variables: $variables,
);
$generator->writeChanges();
}
/**
* @return class-string|false
*/
private function inputEntity(ConsoleStyle $io): string|false
{
$q = new Question('What entity is the purge route provider for?');
$q->setTrimmable(true);
/** @var string $purgeEntity */
$purgeEntity = $io->askQuestion($q);
$entities = $this->getEntityCollection();
$entities = array_filter(
$entities,
static fn (string $name): bool => str_contains(strtolower($name), strtolower($purgeEntity)),
\ARRAY_FILTER_USE_KEY,
);
if ([] === $entities) {
$io->error('No entities found');
return false;
}
if (1 === \count($entities) && 1 === \count($entities[array_key_first($entities)])) {
$entity = $entities[array_key_first($entities)][0];
} else {
$entityChoices = array_keys($entities);
sort($entityChoices, \SORT_STRING | \SORT_FLAG_CASE);
/** @var string $target */
$target = $io->choice('Select one of the available entities', $entityChoices);
/** @var class-string $entity */
$entity = \count($entities[$target]) > 1
? $io->choice('Select one of the available entities', $entities[$target])
: $entities[$target][0];
}
return $entity;
}
/**
* @return ?non-empty-list<'Create'|'Update'|'Delete'>
*/
private function inputActions(ConsoleStyle $io): ?array
{
if ($io->confirm('Should route provider handle only some actions (update/create/delete)?', default: false)) {
/** @var non-empty-list<'Create'|'Update'|'Delete'> $actions */
$actions = $io->choice(
question: 'Select actions',
choices: [
Action::Create->name,
Action::Update->name,
Action::Delete->name,
],
multiSelect: true,
);
$actions = array_values(array_unique($actions));
} else {
$actions = null;
}
return $actions;
}
/**
* @return array<string, non-empty-list<class-string>>
*/
private function getEntityCollection(): array
{
/** @var array<string, non-empty-list<class-string>> $entities */
$entities = [];
foreach ($this->managerRegistry->getManagers() as $manager) {
foreach ($manager->getMetadataFactory()->getAllMetadata() as $metadata) {
$entityFqcn = $metadata->getName();
$name = strrchr($entityFqcn, '\\');
$name = substr(false === $name ? $entityFqcn : $name, 1);
if (isset($entities[$name])) {
if (!\in_array($entityFqcn, $entities[$name], true)) {
$entities[$name][] = $entityFqcn;
}
} else {
$entities[$name] = [$entityFqcn];
}
}
}
foreach ($entities as &$entityFqcns) {
sort($entityFqcns, \SORT_STRING | \SORT_FLAG_CASE);
}
return $entities;
}
}