Skip to content
This repository was archived by the owner on Jun 19, 2022. It is now read-only.

Commit 4cc59fd

Browse files
committed
enhance make:hsl:crud, add monterhealthfilter
1 parent 06d0a07 commit 4cc59fd

File tree

3 files changed

+42
-63
lines changed

3 files changed

+42
-63
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"samj/fractal-bundle": "^4.0",
1414
"babdev/pagerfanta-bundle": "^2.2",
1515
"mark-gerarts/automapper-plus-bundle": "^1.2",
16+
"monterhealth/api-filter-bundle": "^1.2",
1617
"intervention/image": "^2.5",
1718
"symfony-bundles/json-request-bundle": "^3.0",
1819
"symfony/apache-pack": "^1.0",
@@ -30,7 +31,6 @@
3031
"twig/extra-bundle": "For email css",
3132
"twig/inky-extra": "For email css",
3233
"nesbot/carbon": "Easy format your datetime",
33-
"nelmio/cors-bundle": "CORS management",
34-
"monterhealth/api-filter-bundle": "Api filter in query builder"
34+
"nelmio/cors-bundle": "CORS management"
3535
}
3636
}

src/Maker/MakeHslCrud.php

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
final class MakeHslCrud extends AbstractMaker
2727
{
28+
use MakerTrait;
29+
2830
private $doctrineHelper;
2931

3032
private $inflector;
@@ -50,79 +52,66 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
5052
{
5153
$command
5254
->setDescription('Creates API CRUD for Doctrine entity class')
53-
->addArgument('entity-class', InputArgument::OPTIONAL, sprintf('The class name of the entity to create CRUD (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
54-
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeHslCrud.txt'))
55-
;
55+
->addArgument('name', InputArgument::OPTIONAL, sprintf('The class name for new CRUD controller (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
56+
->addArgument('entity-name', InputArgument::OPTIONAL, 'The existing entity class for this CRUD')
57+
->setHelp(file_get_contents(__DIR__ . '/../Resources/help/MakeHslCrud.txt'));
5658

57-
$inputConfig->setArgumentAsNonInteractive('entity-class');
59+
$inputConfig->setArgumentAsNonInteractive('entity-name');
5860
}
5961

6062
public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
6163
{
62-
if (null === $input->getArgument('entity-class')) {
63-
$argument = $command->getDefinition()->getArgument('entity-class');
64-
65-
$entities = $this->doctrineHelper->getEntitiesForAutocomplete();
66-
67-
$question = new Question($argument->getDescription());
68-
$question->setAutocompleterValues($entities);
69-
70-
$value = $io->askQuestion($question);
71-
72-
$input->setArgument('entity-class', $value);
73-
}
64+
$argument = $command->getDefinition()->getArgument('entity-name');
65+
$entityClassname = $io->askQuestion($this->createEntityClassQuestion($argument->getDescription()));
66+
$input->setArgument('entity-name', $entityClassname);
7467
}
7568

7669
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
7770
{
7871
$entityClassDetails = $generator->createClassNameDetails(
79-
Validator::entityExists($input->getArgument('entity-class'), $this->doctrineHelper->getEntitiesForAutocomplete()),
72+
Validator::entityExists($input->getArgument('entity-name'), $this->doctrineHelper->getEntitiesForAutocomplete()),
8073
'Entity\\'
8174
);
8275

8376
$entityDoctrineDetails = $this->doctrineHelper->createDoctrineDetails($entityClassDetails->getFullName());
8477

8578
$repositoryVars = [];
79+
$repositoryClassDetails = $generator->createClassNameDetails(
80+
'\\' . $entityDoctrineDetails->getRepositoryClass(),
81+
'Repository\\',
82+
'Repository'
83+
);
8684

87-
// check if got repository and get it
88-
if (null !== $entityDoctrineDetails->getRepositoryClass()) {
89-
$repositoryClassDetails = $generator->createClassNameDetails(
90-
'\\'.$entityDoctrineDetails->getRepositoryClass(),
91-
'Repository\\',
92-
'Repository'
93-
);
94-
95-
$repositoryVars = [
96-
'repository_full_class_name' => $repositoryClassDetails->getFullName(),
97-
'repository_class_name' => $repositoryClassDetails->getShortName(),
98-
'repository_var' => lcfirst($this->singularize($repositoryClassDetails->getShortName())),
99-
];
100-
}
101-
85+
$repositoryVars = [
86+
'repository_full_class_name' => $repositoryClassDetails->getFullName(),
87+
'repository_class_name' => $repositoryClassDetails->getShortName(),
88+
'repository_var' => lcfirst($this->singularize($repositoryClassDetails->getShortName())),
89+
];
90+
10291
// check if got DTO and get it
92+
$dtoClassname = Str::asClassName($io->ask('Enter dto class name for this CRUD', null, [Validator::class, 'notBlank']));
10393
$dtoClassDetails = $generator->createClassNameDetails(
104-
$entityClassDetails->getShortName() . 'Input',
94+
$dtoClassname,
10595
'Dto\\Input\\'
10696
);
10797

108-
if(!class_exists($dtoClassDetails->getFullName()))
109-
{
110-
throw new RuntimeCommandException(sprintf("You must create a DTO (make:hsl:dto) for entity '%s' first.", $entityClassDetails->getFullName()));
98+
if (!class_exists($dtoClassDetails->getFullName())) {
99+
throw new RuntimeCommandException(sprintf("Class \"%s\" does not exist.", $dtoClassDetails->getFullName()));
111100
}
112-
101+
113102
// check if got Transformer and get it
103+
$transformerClassname = Str::asClassName($io->ask('Enter transformer class name for this CRUD', null, [Validator::class, 'notBlank']));
114104
$transformerClassDetails = $generator->createClassNameDetails(
115-
$entityClassDetails->getShortName() . 'Transformer',
105+
$transformerClassname,
116106
'Transformer\\'
117107
);
118108

119-
if(!class_exists($transformerClassDetails->getFullName()))
120-
{
121-
throw new RuntimeCommandException(sprintf("You must create a Transformer (make:hsl:transformer) for entity '%s' first.", $entityClassDetails->getFullName()));
109+
if (!class_exists($transformerClassDetails->getFullName())) {
110+
throw new RuntimeCommandException(sprintf("Class \"%s\" does not exist.", $transformerClassDetails->getFullName()));
122111
}
123112

124113
$controllerClassDetails = $generator->createClassNameDetails(
125-
$entityClassDetails->getRelativeNameWithoutSuffix().'Controller',
114+
Str::asClassName($input->getArgument('name')) . 'Controller',
126115
'Controller\\',
127116
'Controller'
128117
);
@@ -136,8 +125,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
136125

137126
$generator->generateController(
138127
$controllerClassDetails->getFullName(),
139-
__DIR__.'/../Resources/skeleton/crud/Controller.tpl.php',
140-
array_merge([
128+
__DIR__ . '/../Resources/skeleton/crud/Controller.tpl.php',
129+
array_merge(
130+
[
141131
'entity_full_class_name' => $entityClassDetails->getFullName(),
142132
'entity_class_name' => $entityClassDetails->getShortName(),
143133
'dto_full_class_name' => $dtoClassDetails->getFullName(),

src/Resources/skeleton/crud/Controller.tpl.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
use <?= $dto_full_class_name ?>;
77
use <?= $entity_full_class_name ?>;
88
use <?= $transformer_full_class_name ?>;
9+
use <?= $repository_full_class_name ?>;
910
use Fd\HslBundle\Fractal\FractalTrait;
1011
use Fd\HslBundle\Pagination\PaginatorInterface;
1112
use League\Fractal\Manager;
1213
use League\Fractal\Resource\Collection;
1314
use League\Fractal\Resource\Item;
14-
<?php if (isset($repository_full_class_name)): ?>
15-
use <?= $repository_full_class_name ?>;
16-
<?php endif ?>
15+
use MonterHealth\ApiFilterBundle\MonterHealthApiFilter;
1716
use Symfony\Bundle\FrameworkBundle\Controller\<?= $parent_class_name ?>;
1817
use Symfony\Component\HttpFoundation\Request;
1918
use Symfony\Component\HttpFoundation\Response;
@@ -37,25 +36,15 @@ public function __construct(Manager $manager, AutoMapperInterface $mapper)
3736
/**
3837
* @Route("", name="<?= $route_name ?>_collection", methods={"GET"})
3938
*/
40-
<?php if (isset($repository_full_class_name)): ?>
41-
public function collection(Request $request, <?= $repository_class_name ?> $<?= $repository_var ?>, PaginatorInterface $pagination): Response
42-
{
43-
$data = $pagination->paginate($<?= $repository_var ?>->createQueryBuilder('qb'), <?= $transformer_class_name ?>::class);
44-
45-
return $this->json($this->fractal($request)->createData($data)->toArray());
46-
}
47-
<?php else: ?>
48-
public function collection(Request $request, PaginationManager $pagination): Response
39+
public function collection(Request $request, <?= $repository_class_name ?> $<?= $repository_var ?>, PaginatorInterface $pagination, MonterHealthApiFilter $monterHealthApiFilter): Response
4940
{
50-
$<?= $entity_var_plural ?> = $this->getDoctrine()
51-
->getRepository(<?= $entity_class_name ?>::class)
52-
->findByQB();
41+
$qb = $<?= $repository_var ?>->createQueryBuilder('qb');
42+
$monterHealthApiFilter->addFilterConstraints($qb, $<?= $repository_var ?>->getClassName(), $request->query);
5343

54-
$data = $pagination->getQueryBuilderPagination()->paginate($<?= $entity_var_plural ?>, <?= $transformer_class_name ?>::class);
44+
$data = $pagination->paginate($qb, <?= $transformer_class_name ?>::class);
5545

5646
return $this->json($this->fractal($request)->createData($data)->toArray());
5747
}
58-
<?php endif ?>
5948

6049
/**
6150
* @Route("/{<?= $entity_identifier ?>}", name="<?= $route_name ?>_item", methods={"GET"}, requirements={"<?= $entity_identifier ?>"="\d+"})

0 commit comments

Comments
 (0)