Skip to content

Commit 44b7851

Browse files
committed
completed improvement of make:form command
1 parent db30634 commit 44b7851

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

src/Maker/MakeForm.php

+30-18
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
final class MakeForm extends AbstractMaker
3333
{
3434
private $entityHelper;
35+
private $boundEntityOrClass = true;
3536

3637
public function __construct(DoctrineEntityHelper $entityHelper)
3738
{
@@ -57,8 +58,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
5758
if (null != $input->getArgument('name') && $this->entityHelper->isDoctrineConnected()) {
5859
$question = new Question('Enter the class or entity name that the new form will be bound to (empty for none)');
5960
$question->setAutocompleterValues($this->entityHelper->getEntitiesForAutocomplete());
60-
//$entity = $io->choice("Enter the class or entity name that the new form will be bound to", $this->entityHelper->getEntitiesForAutocomplete(), 'none');
61-
$entity = $io->askQuestion($question);
61+
$this->boundEntityOrClass = $io->askQuestion($question);
6262
}
6363
}
6464

@@ -70,25 +70,37 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
7070
'Type'
7171
);
7272

73-
$entityClassNameDetails = $generator->createClassNameDetails(
74-
$formClassNameDetails->getRelativeNameWithoutSuffix(),
75-
'Entity\\'
76-
);
73+
if (null === $this->boundEntityOrClass) {
7774

78-
$entityClassExists = class_exists($entityClassNameDetails->getFullName());
75+
$generator->generateClass(
76+
$formClassNameDetails->getFullName(),
77+
'form/SimpleType.tpl.php',
78+
[ ]
79+
);
7980

80-
$formFields = $entityClassExists ? $this->entityHelper->getFormFieldsFromEntity($entityClassNameDetails->getFullName()) : ['field_name'];
81+
} else {
8182

82-
$generator->generateClass(
83-
$formClassNameDetails->getFullName(),
84-
'form/Type.tpl.php',
85-
[
86-
'entity_class_exists' => $entityClassExists,
87-
'entity_full_class_name' => $entityClassNameDetails->getFullName(),
88-
'entity_class_name' => $entityClassNameDetails->getShortName(),
89-
'form_fields' => $formFields,
90-
]
91-
);
83+
$entityClassNameDetails = $generator->createClassNameDetails(
84+
true == $this->boundEntityOrClass ? $formClassNameDetails->getRelativeNameWithoutSuffix() : $this->boundEntityOrClass,
85+
'Entity\\'
86+
);
87+
88+
$entityClassExists = class_exists($entityClassNameDetails->getFullName());
89+
90+
$formFields = $entityClassExists ? $this->entityHelper->getFormFieldsFromEntity($entityClassNameDetails->getFullName()) : ['field_name'];
91+
92+
$generator->generateClass(
93+
$formClassNameDetails->getFullName(),
94+
'form/Type.tpl.php',
95+
[
96+
'entity_class_exists' => $entityClassExists,
97+
'entity_full_class_name' => $entityClassNameDetails->getFullName(),
98+
'entity_class_name' => $entityClassNameDetails->getShortName(),
99+
'form_fields' => $formFields,
100+
]
101+
);
102+
103+
}
92104

93105
$generator->writeChanges();
94106

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?= "<?php\n" ?>
2+
3+
namespace <?= $namespace ?>;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
8+
9+
class <?= $class_name ?> extends AbstractType
10+
{
11+
public function buildForm(FormBuilderInterface $builder, array $options)
12+
{
13+
$builder
14+
->add('form_field')
15+
;
16+
}
17+
18+
public function configureOptions(OptionsResolver $resolver)
19+
{
20+
$resolver->setDefaults([
21+
//configure form options here
22+
]);
23+
}
24+
}

src/Resources/skeleton/form/Type.tpl.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?= "<?php\n" ?>
22

3-
namespace <?= $namespace; ?>;
3+
namespace <?= $namespace ?>;
44

55
<?php if ($entity_class_exists): ?>
66
use <?= $entity_full_class_name ?>;

0 commit comments

Comments
 (0)