9
9
use Symfony \Component \Console \Style \SymfonyStyle ;
10
10
11
11
use Alexecus \Spawner \Command \Command ;
12
+ use Alexecus \Spawner \Operations \Template ;
12
13
use Alexecus \Spawner \Operations \Copy ;
13
14
14
15
use Alexecus \Spawner \Input \Validators \EmptyValidator ;
20
21
class FormGenerator extends Command
21
22
{
22
23
/**
23
- * @var Copy
24
+ * @var Template
24
25
*/
25
- private $ copy ;
26
+ private $ template ;
26
27
27
- public function __construct (Copy $ copy )
28
+ public function __construct (Template $ template , Copy $ copy )
28
29
{
29
30
parent ::__construct ();
30
31
32
+ $ this ->template = $ template ;
31
33
$ this ->copy = $ copy ;
32
34
}
33
35
@@ -38,8 +40,7 @@ public function configure()
38
40
{
39
41
$ this
40
42
->setName ('generate:form ' )
41
- ->setDescription ('Generates a new form plugin ' )
42
- ;
43
+ ->setDescription ('Generates a new form plugin ' );
43
44
}
44
45
45
46
/**
@@ -51,19 +52,42 @@ public function execute(InputInterface $input, OutputInterface $output)
51
52
new EmptyValidator ('Form should not be empty ' ),
52
53
]);
53
54
54
- $ name = $ this -> ask ( ' What is the name of the form ? ' , ' ContactForm ' );
55
+ // inputs
55
56
57
+ $ name = $ this ->ask ('What is the name of the form ? ' , 'ContactForm ' );
58
+ $ isTemplate = $ this ->confirm ('Do you want to generate a template file ? ' );
56
59
$ confirm = $ this ->confirm ('Do you confirm form generation ? ' );
57
60
61
+ // actions
62
+
58
63
if ($ confirm ) {
64
+ if ($ isTemplate ) {
65
+ $ this ->createTemplate ($ path , $ name );
66
+ }
67
+
59
68
$ this ->createForm ($ path , $ name );
60
69
$ this ->success ("The form $ name was generated " );
61
70
}
62
71
}
63
72
73
+ /**
74
+ * Creates the template file
75
+ *
76
+ * @param string $path The namespace path
77
+ * @param string $name The name of the form
78
+ */
79
+ private function createTemplate ($ path , $ name )
80
+ {
81
+ $ source = __DIR__ . '/template/template.html.twig ' ;
82
+ $ target = "$ path/ $ name.html.twig " ;
83
+
84
+ $ this ->copy ->perform ($ source , $ target );
85
+ }
86
+
64
87
/**
65
88
* Creates the form class
66
89
*
90
+ * @param string $path The namespace path
67
91
* @param string $name The name of the form
68
92
*/
69
93
private function createForm ($ path , $ name )
@@ -75,6 +99,6 @@ private function createForm($path, $name)
75
99
'form_name ' => $ name ,
76
100
];
77
101
78
- $ this ->copy ->perform ($ source , $ target , $ replacements );
102
+ $ this ->template ->perform ($ source , $ target , $ replacements );
79
103
}
80
104
}
0 commit comments