Skip to content

Commit e88be49

Browse files
committed
php class skeleton with properties
1 parent a285c88 commit e88be49

File tree

1 file changed

+85
-20
lines changed

1 file changed

+85
-20
lines changed

src/wizardPhpSkeletons.ts

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ export async function wizardGeneratePhpSkeleton() {
77
const type = await wizardFileType();
88
const fileName = await wizardFileName(type);
99

10-
const namespace = generateNamespace(folder,fileName);
10+
const namespace = generateNamespace(folder, fileName);
1111

12-
const classSkeleton = generatePhpSkeleton(type,fileName,namespace);
12+
const classSkeleton = await generatePhpSkeleton(type, fileName, namespace);
1313

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`));
1515

1616
await writeFile(pathFile, classSkeleton);
17-
17+
1818
showFile(pathFile);
1919
}
2020

21-
async function wizardSelectFolder(): Promise<vscode.Uri>
22-
{
21+
async function wizardSelectFolder(): Promise<vscode.Uri> {
2322
if (!vscode.workspace) {
2423
throw new Error("Working folder not found, open a folder an try again");
2524
}
@@ -39,8 +38,7 @@ async function wizardSelectFolder(): Promise<vscode.Uri>
3938
return folder[0];
4039
}
4140

42-
async function wizardFileType(): Promise<string>
43-
{
41+
async function wizardFileType(): Promise<string> {
4442
const acceptedTypes = [
4543
"class",
4644
// "interface",
@@ -61,8 +59,7 @@ async function wizardFileType(): Promise<string>
6159
return type;
6260
}
6361

64-
async function wizardFileName(type: string): Promise<string>
65-
{
62+
async function wizardFileName(type: string): Promise<string> {
6663
let className = await vscode.window.showInputBox({
6764
placeHolder: `Name of ${type}`
6865
});
@@ -73,28 +70,96 @@ async function wizardFileName(type: string): Promise<string>
7370
return className;
7471
}
7572

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);
8076
}
8177
return "## TODO";
8278
}
8379

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
86104
87105
namespace ${namespace};
88-
106+
89107
class ${className}
90108
{
91-
public function __construct()
92-
{
93-
}
109+
public function __construct(${declarePropertiesAsString})
110+
{
111+
}
112+
113+
${gettersAndSetters}
94114
}`;
95115

116+
return skeleton;
96117
}
97118

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+
}
98163

99164

100165
function capitalizeAndTrim(str: string): string {

0 commit comments

Comments
 (0)