-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathMakeForm.php
More file actions
145 lines (120 loc) · 4.93 KB
/
MakeForm.php
File metadata and controls
145 lines (120 loc) · 4.93 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
<?php
/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MakerBundle\Maker;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\NamespaceType;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Renderer\FormTypeRenderer;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassDetails;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Validator\Validation;
/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Ryan Weaver <weaverryan@gmail.com>
*/
final class MakeForm extends AbstractMaker
{
public function __construct(private DoctrineHelper $entityHelper, private FormTypeRenderer $formTypeRenderer)
{
}
public static function getCommandName(): string
{
return 'make:form';
}
public static function getCommandDescription(): string
{
return 'Create a new form class';
}
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->addArgument('name', InputArgument::OPTIONAL, \sprintf('The name of the form class (e.g. <fg=yellow>%sType</>)', Str::asClassName(Str::getRandomTerm())))
->addArgument('bound-class', InputArgument::OPTIONAL, 'The name of Entity or fully qualified model class name that the new form will be bound to (empty for none)')
->setHelp($this->getHelpFileContents('MakeForm.txt'))
;
$inputConfig->setArgumentAsNonInteractive('bound-class');
}
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
if (null === $input->getArgument('bound-class')) {
$argument = $command->getDefinition()->getArgument('bound-class');
$entities = $this->entityHelper->getEntitiesForAutocomplete();
$question = new Question($argument->getDescription());
$question->setValidator(static fn ($answer) => Validator::existsOrNull($answer, $entities));
$question->setAutocompleterValues($entities);
$question->setMaxAttempts(3);
$input->setArgument('bound-class', $io->askQuestion($question));
}
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$formClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
$generator->getNamespace(NamespaceType::Form).'\\',
'Type'
);
$formFields = ['field_name' => null];
$boundClass = $input->getArgument('bound-class');
$boundClassDetails = null;
if (null !== $boundClass) {
$boundClassDetails = $generator->createClassNameDetails(
$boundClass,
$generator->getNamespace(NamespaceType::Entity).'\\'
);
$doctrineEntityDetails = $this->entityHelper->createDoctrineDetails($boundClassDetails->getFullName());
if (null !== $doctrineEntityDetails) {
$formFields = $doctrineEntityDetails->getFormFields();
} else {
$classDetails = new ClassDetails($boundClassDetails->getFullName());
$formFields = $classDetails->getFormFields();
}
}
$this->formTypeRenderer->render(
$formClassNameDetails,
$formFields,
$boundClassDetails
);
$generator->writeChanges();
$this->writeSuccessMessage($io);
$io->text([
'Next: Add fields to your form and start using it.',
'Find the documentation at <fg=yellow>https://symfony.com/doc/current/forms.html</>',
]);
}
public function configureDependencies(DependencyBuilder $dependencies): void
{
$dependencies->addClassDependency(
AbstractType::class,
// technically only form is needed, but the user will *probably* also want validation
'form'
);
$dependencies->addClassDependency(
Validation::class,
'validator',
// add as an optional dependency: the user *probably* wants validation
false
);
$dependencies->addClassDependency(
DoctrineBundle::class,
'orm',
false
);
}
}