Skip to content

Commit a285c88

Browse files
committed
add a namespace
1 parent 79b8b72 commit a285c88

File tree

4 files changed

+103
-27
lines changed

4 files changed

+103
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
88

99
### Added
1010
- Initial release
11-
- Command for create a basic skeleton of PHP class
11+
- Command for create a basic skeleton of PHP class
12+
- Add namespace PSR4 in skeleton

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ With this extension, you can quickly generate a skeleton for a PHP class just by
88

99
## Planned Features
1010

11-
- Generate PSR-4 namespace
1211
- Generate skeleton of other types of PHP files [interface, trait, enum, value object]
1312

1413
## Requirements

src/utils/files.ts

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,86 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
3+
import * as fs from 'fs';
34

4-
export function getPathFile(className: string): vscode.Uri
5-
{
5+
export function getCurrentPathFile(className: string): vscode.Uri {
66

77
let fsPath = null;
88

9-
// Create in current file directory
109
if (vscode.window.activeTextEditor) {
1110
const currentlyOpenTabfileUri = vscode.window.activeTextEditor.document.uri;
1211
const currentlyOpenTabfileName = path.basename(currentlyOpenTabfileUri.toString());
1312
fsPath = currentlyOpenTabfileUri.fsPath.replace(currentlyOpenTabfileName, "");
1413
}
1514

16-
// Create in root workspace
1715
if (!fsPath && vscode.workspace.workspaceFolders !== undefined) {
1816
fsPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
1917
}
2018

2119
if (!fsPath) {
2220
throw new Error('Working folder not found, open a folder an try again');
2321
}
24-
22+
2523
return vscode.Uri.parse(`file:` + path.join(`${fsPath}`, `${className}.php`));
2624
}
2725

28-
export async function writeFile(pathFile: vscode.Uri, skeleton: string): Promise<void>
29-
{
26+
export async function writeFile(pathFile: vscode.Uri, skeleton: string): Promise<void> {
3027
await vscode.workspace.fs.writeFile(pathFile, Buffer.from(skeleton));
3128
}
3229

33-
export function showFile(pathFile: vscode.Uri): void
34-
{
30+
export function showFile(pathFile: vscode.Uri): void {
3531
vscode.window.showTextDocument(pathFile);
36-
}
32+
}
33+
34+
function getRootPath(): string|null
35+
{
36+
if (vscode.workspace.workspaceFolders === undefined) {
37+
return null;
38+
}
39+
return vscode.workspace.workspaceFolders[0].uri.fsPath;
40+
41+
}
42+
43+
function getPsr4(): { [key: string]: string } {
44+
const rootPath = getRootPath();
45+
if(!rootPath){
46+
return {};
47+
}
48+
const composerJsonPath = path.join(rootPath, `composer.json`);
49+
if (!fs.existsSync(composerJsonPath)) {
50+
return {};
51+
}
52+
const composerJsonContent = fs.readFileSync(composerJsonPath, 'utf8');
53+
54+
const composerJson = JSON.parse(composerJsonContent);
55+
56+
const autoload = composerJson.autoload;
57+
58+
if (!autoload || !autoload['psr-4']) {
59+
return {};
60+
}
61+
62+
return autoload['psr-4'];
63+
}
64+
65+
export function generateNamespace(folder: vscode.Uri, fileName: string): string {
66+
let folderPath = folder.fsPath;
67+
const psr4 = getPsr4();
68+
69+
const rootPath = getRootPath();
70+
folderPath = folderPath.replace(rootPath??'', '');
71+
folderPath = folderPath.replace(/\/|\\/g, '\\').replace(/\\/, '');
72+
73+
for (const prefix of Object.keys(psr4)) {
74+
const basePath = psr4[prefix].replace(/\/|\\/g, '\\\\');
75+
const prefixRegex = new RegExp(`^${basePath}`);
76+
77+
if (prefixRegex.test(folderPath)) {
78+
const relativePath = folderPath.replace(prefixRegex, '');
79+
const namespace = prefix + relativePath;
80+
vscode.window.showInformationMessage("in1");
81+
return namespace;
82+
}
83+
}
84+
vscode.window.showInformationMessage("out");
85+
return folderPath;
86+
}

src/wizardPhpSkeletons.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
11
import * as vscode from 'vscode';
2-
import { getPathFile, showFile, writeFile } from './utils/files';
2+
import * as path from 'path';
3+
import { showFile, writeFile, generateNamespace } from './utils/files';
34

45
export async function wizardGeneratePhpSkeleton() {
5-
6+
const folder = await wizardSelectFolder();
67
const type = await wizardFileType();
78
const fileName = await wizardFileName(type);
8-
const classSkeleton = generatePhpSkeleton(type,fileName);
99

10-
const pathFile = getPathFile(fileName);
10+
const namespace = generateNamespace(folder,fileName);
11+
12+
const classSkeleton = generatePhpSkeleton(type,fileName,namespace);
13+
14+
const pathFile = vscode.Uri.parse(`file:` + path.join(`${folder.fsPath}`, `${fileName}.php`));
1115

1216
await writeFile(pathFile, classSkeleton);
1317

1418
showFile(pathFile);
1519
}
1620

21+
async function wizardSelectFolder(): Promise<vscode.Uri>
22+
{
23+
if (!vscode.workspace) {
24+
throw new Error("Working folder not found, open a folder an try again");
25+
}
26+
const folder = await vscode.window.showOpenDialog(
27+
{
28+
canSelectFiles: false,
29+
canSelectFolders: true,
30+
canSelectMany: false,
31+
openLabel: "Select folder where new class should be placed"
32+
}
33+
);
34+
35+
if (!folder) {
36+
throw new Error("Select the folder where you want create a PHP file");
37+
}
38+
39+
return folder[0];
40+
}
41+
1742
async function wizardFileType(): Promise<string>
1843
{
44+
const acceptedTypes = [
45+
"class",
46+
// "interface",
47+
// "trait",
48+
// "enum",
49+
// "value object (immutable class)"
50+
];
1951
const type = await vscode.window.showQuickPick(
20-
[
21-
"class",
22-
// "interface",
23-
// "trait",
24-
// "enum",
25-
// "value object (immutable class)"
26-
],
52+
acceptedTypes,
2753
{
2854
placeHolder: "select the type of file you want to create"
2955
}
@@ -47,18 +73,18 @@ async function wizardFileName(type: string): Promise<string>
4773
return className;
4874
}
4975

50-
function generatePhpSkeleton(type: string,className: string): string
76+
function generatePhpSkeleton(type: string,className: string, namespace: string): string
5177
{
5278
if(type === "class"){
53-
return generatePhpClassSkeleton(className);
79+
return generatePhpClassSkeleton(className, namespace);
5480
}
5581
return "## TODO";
5682
}
5783

58-
function generatePhpClassSkeleton(className: string): string {
84+
function generatePhpClassSkeleton(className: string, namespace: string): string {
5985
return `<?php
6086
61-
## TODO: generate namespace
87+
namespace ${namespace};
6288
6389
class ${className}
6490
{

0 commit comments

Comments
 (0)