Skip to content

Commit b4eddf0

Browse files
fix: invalid module registration
1 parent 2b0063e commit b4eddf0

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"type": "string",
3535
"editPresentation": "multilineText",
3636
"default": "",
37-
"markdownDescription": "`%module%` will be replaced with the module name. \n\n **Do not add comment symbols like `/*` or `*/`, they will be added automatically.** \n\n Separate lines with the newline character `\\n`"
37+
"markdownDescription": "`%module%` will be replaced with the module name. \n\n **Do not add comment symbols like `/*` or `*/`, they will be added automatically.**"
3838
},
3939
"magento-toolbox.xmlFileHeaderComment": {
4040
"type": "string",

src/common/php/FileHeader.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,22 @@ export default class FileHeader {
1212

1313
return header.replace('%module%', module);
1414
}
15+
16+
public static getHeaderAsComment(module: string): string {
17+
const header = this.getHeader(module);
18+
19+
if (!header) {
20+
return '';
21+
}
22+
23+
let comment = `/**\n`;
24+
25+
header.split(/[\r\n]+/).forEach(line => {
26+
comment += ` * ${line}\n`;
27+
});
28+
29+
comment += ` */\n`;
30+
31+
return comment;
32+
}
1533
}

src/generator/TemplateGenerator.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ export default class TemplateGenerator extends FileGenerator {
77
public constructor(
88
protected fileName: string,
99
protected templateName: string,
10-
protected data: any
10+
protected data: Record<string, any>
1111
) {
1212
super();
1313
}
1414

15+
public getTemplateData(): Record<string, any> {
16+
return this.data;
17+
}
18+
1519
public async generate(workspaceUri: Uri): Promise<GeneratedFile> {
16-
const content = await GenerateFromTemplate.generate(this.templateName, this.data);
20+
const content = await GenerateFromTemplate.generate(this.templateName, this.getTemplateData());
1721

1822
const path = Uri.joinPath(workspaceUri, this.fileName);
1923
return new GeneratedFile(path, content);
Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
import { ModuleWizardComposerData, ModuleWizardData } from 'wizard/ModuleWizard';
2-
import { PhpFile, PsrPrinter } from 'node-php-generator';
32
import { Uri } from 'vscode';
43
import GeneratedFile from 'generator/GeneratedFile';
54
import Magento from 'util/Magento';
6-
import FileGenerator from 'generator/FileGenerator';
5+
import TemplateGenerator from 'generator/TemplateGenerator';
6+
import FileHeader from 'common/php/FileHeader';
77

8-
export default class ModuleRegistrationGenerator extends FileGenerator {
8+
export default class ModuleRegistrationGenerator extends TemplateGenerator {
99
public constructor(protected data: ModuleWizardData | ModuleWizardComposerData) {
10-
super();
10+
super('registration.php', 'php/registration', data);
11+
}
12+
13+
public getTemplateData(): any {
14+
const data = super.getTemplateData();
15+
16+
data.fileHeader = FileHeader.getHeaderAsComment(`${this.data.vendor}_${this.data.module}`);
17+
18+
return data;
1119
}
1220

1321
public async generate(workspaceUri: Uri): Promise<GeneratedFile> {
14-
const registrationContent = this.getRegistrationContent();
15-
const registrationFileUri = Magento.getModuleDirectory(
22+
const moduleDirectory = Magento.getModuleDirectory(
1623
this.data.vendor,
1724
this.data.module,
18-
workspaceUri,
19-
'registration.php'
25+
workspaceUri
2026
);
21-
return new GeneratedFile(registrationFileUri, registrationContent);
22-
}
23-
24-
protected getRegistrationContent(): string {
25-
const printer = new PsrPrinter();
26-
const file = new PhpFile();
27-
file.setStrictTypes(true);
28-
file.addUse('Magento\\Framework\\Component\\ComponentRegistrar');
29-
30-
const moduleName = Magento.getModuleName(this.data.vendor, this.data.module);
31-
32-
let content = printer.printFile(file);
33-
34-
content += `ComponentRegistrar::register(ComponentRegistrar::MODULE, '${moduleName}', __DIR__);\n`;
3527

36-
return content;
28+
return super.generate(moduleDirectory);
3729
}
3830
}

templates/php/registration.ejs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
<% if (fileHeader) { -%>
3+
<%- fileHeader -%>
4+
<% } -%>
5+
declare(strict_types=1);
6+
7+
use Magento\Framework\Component\ComponentRegistrar;
8+
9+
ComponentRegistrar::register(ComponentRegistrar::MODULE, '<%= vendor %>_<%= module %>', __DIR__);

0 commit comments

Comments
 (0)