@@ -7,19 +7,18 @@ export async function wizardGeneratePhpSkeleton() {
7
7
const type = await wizardFileType ( ) ;
8
8
const fileName = await wizardFileName ( type ) ;
9
9
10
- const namespace = generateNamespace ( folder , fileName ) ;
10
+ const namespace = generateNamespace ( folder , fileName ) ;
11
11
12
- const classSkeleton = generatePhpSkeleton ( type , fileName , namespace ) ;
12
+ const classSkeleton = await generatePhpSkeleton ( type , fileName , namespace ) ;
13
13
14
- const pathFile = vscode . Uri . parse ( `file:` + path . join ( `${ folder . fsPath } ` , `${ fileName } .php` ) ) ;
14
+ const pathFile = vscode . Uri . parse ( `file:` + path . join ( `${ folder . fsPath } ` , `${ fileName } .php` ) ) ;
15
15
16
16
await writeFile ( pathFile , classSkeleton ) ;
17
-
17
+
18
18
showFile ( pathFile ) ;
19
19
}
20
20
21
- async function wizardSelectFolder ( ) : Promise < vscode . Uri >
22
- {
21
+ async function wizardSelectFolder ( ) : Promise < vscode . Uri > {
23
22
if ( ! vscode . workspace ) {
24
23
throw new Error ( "Working folder not found, open a folder an try again" ) ;
25
24
}
@@ -39,8 +38,7 @@ async function wizardSelectFolder(): Promise<vscode.Uri>
39
38
return folder [ 0 ] ;
40
39
}
41
40
42
- async function wizardFileType ( ) : Promise < string >
43
- {
41
+ async function wizardFileType ( ) : Promise < string > {
44
42
const acceptedTypes = [
45
43
"class" ,
46
44
// "interface",
@@ -61,8 +59,7 @@ async function wizardFileType(): Promise<string>
61
59
return type ;
62
60
}
63
61
64
- async function wizardFileName ( type : string ) : Promise < string >
65
- {
62
+ async function wizardFileName ( type : string ) : Promise < string > {
66
63
let className = await vscode . window . showInputBox ( {
67
64
placeHolder : `Name of ${ type } `
68
65
} ) ;
@@ -73,28 +70,96 @@ async function wizardFileName(type: string): Promise<string>
73
70
return className ;
74
71
}
75
72
76
- function generatePhpSkeleton ( type : string , className : string , namespace : string ) : string
77
- {
78
- if ( type === "class" ) {
79
- return generatePhpClassSkeleton ( className , namespace ) ;
73
+ async function generatePhpSkeleton ( type : string , className : string , namespace : string ) : Promise < string > {
74
+ if ( type === "class" ) {
75
+ return await generatePhpClassSkeleton ( className , namespace ) ;
80
76
}
81
77
return "## TODO" ;
82
78
}
83
79
84
- function generatePhpClassSkeleton ( className : string , namespace : string ) : string {
85
- return `<?php
80
+ async function generatePhpClassSkeleton ( className : string , namespace : string ) : Promise < string > {
81
+ const properties = await wizardProperties ( ) ;
82
+
83
+ let declareProperties = [ ] ;
84
+ for ( const property of properties ) {
85
+ declareProperties . push ( `${ property . visibility } ${ property . type } $${ property . name } ` ) ;
86
+ }
87
+ const declarePropertiesAsString = declareProperties . join ( ", " ) ;
88
+
89
+ let gettersAndSetters = "" ;
90
+ for ( const property of properties ) {
91
+ const capitalizedName = capitalize ( property . name ) ;
92
+ gettersAndSetters += `
93
+ public function get${ capitalizedName } (): ${ property . type }
94
+ {
95
+ return $this->${ property . name } ;
96
+ }
97
+ public function set${ capitalizedName } ($${ property . name } ): void
98
+ {
99
+ $this->${ property . name } = $${ property . name } ;
100
+ }` ;
101
+ }
102
+
103
+ let skeleton = `<?php
86
104
87
105
namespace ${ namespace } ;
88
-
106
+
89
107
class ${ className }
90
108
{
91
- public function __construct()
92
- {
93
- }
109
+ public function __construct(${ declarePropertiesAsString } )
110
+ {
111
+ }
112
+
113
+ ${ gettersAndSetters }
94
114
}` ;
95
115
116
+ return skeleton ;
96
117
}
97
118
119
+ function capitalize ( str : string ) : string {
120
+ return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ;
121
+ }
122
+
123
+
124
+ async function wizardProperties ( ) : Promise < Array < { name : string , visibility : string , type : string } > > {
125
+ let properties = [ ] ;
126
+
127
+ let input = await vscode . window . showInputBox ( {
128
+ prompt : "Enter a property name (press 'Cancel' or leave empty to finish)"
129
+ } ) ;
130
+
131
+ while ( input ) {
132
+ const acceptedVisibilities = [
133
+ "private" ,
134
+ "protected" ,
135
+ "public"
136
+ ] ;
137
+ let visibility = await vscode . window . showQuickPick (
138
+ acceptedVisibilities ,
139
+ {
140
+ placeHolder : "Select the visibility of the property"
141
+ }
142
+ ) ;
143
+ if ( ! visibility ) {
144
+ visibility = "private" ;
145
+ }
146
+
147
+ let type = await vscode . window . showInputBox ( {
148
+ prompt : "Enter the type of the property (int, string, etc.)"
149
+ } ) ;
150
+ if ( ! type ) {
151
+ type = "string" ;
152
+ }
153
+
154
+ properties . push ( { name : input , visibility : visibility ?? '' , type : type ?? '' } ) ;
155
+
156
+ input = await vscode . window . showInputBox ( {
157
+ prompt : "Enter a property name (press 'Cancel' or leave empty to finish)"
158
+ } ) ;
159
+ }
160
+
161
+ return properties ;
162
+ }
98
163
99
164
100
165
function capitalizeAndTrim ( str : string ) : string {
0 commit comments