|
| 1 | +import FileHeader from 'common/php/FileHeader'; |
| 2 | +import PhpNamespace from 'common/PhpNamespace'; |
| 3 | +import GeneratedFile from 'generator/GeneratedFile'; |
| 4 | +import FileGenerator from 'generator/FileGenerator'; |
| 5 | +import { PhpFile, PsrPrinter } from 'node-php-generator'; |
| 6 | +import { Uri } from 'vscode'; |
| 7 | +import { DataPatchWizardData } from 'wizard/DataPatchWizard'; |
| 8 | +import Magento from 'util/Magento'; |
| 9 | +import * as fs from 'fs'; |
| 10 | +import * as path from 'path'; |
| 11 | + |
| 12 | +export default class DataPatchGenerator extends FileGenerator { |
| 13 | + private static readonly DATA_PATCH_INTERFACE = |
| 14 | + 'Magento\\Framework\\Setup\\Patch\\DataPatchInterface'; |
| 15 | + private static readonly PATCH_REVERTABLE_INTERFACE = |
| 16 | + 'Magento\\Framework\\Setup\\Patch\\PatchRevertableInterface'; |
| 17 | + |
| 18 | + public constructor(protected data: DataPatchWizardData) { |
| 19 | + super(); |
| 20 | + } |
| 21 | + |
| 22 | + public async generate(workspaceUri: Uri): Promise<GeneratedFile> { |
| 23 | + const [vendor, module] = this.data.module.split('_'); |
| 24 | + const setupDir = 'Setup/Patch/Data'; |
| 25 | + const namespaceParts = [vendor, module, 'Setup', 'Patch', 'Data']; |
| 26 | + const moduleDirectory = Magento.getModuleDirectory(vendor, module, workspaceUri); |
| 27 | + |
| 28 | + // Create setup directory if it doesn't exist |
| 29 | + const setupDirPath = path.join(moduleDirectory.fsPath, setupDir); |
| 30 | + if (!fs.existsSync(setupDirPath)) { |
| 31 | + fs.mkdirSync(setupDirPath, { recursive: true }); |
| 32 | + } |
| 33 | + |
| 34 | + const phpFile = new PhpFile(); |
| 35 | + phpFile.setStrictTypes(true); |
| 36 | + |
| 37 | + const header = FileHeader.getHeader(this.data.module); |
| 38 | + |
| 39 | + if (header) { |
| 40 | + phpFile.addComment(header); |
| 41 | + } |
| 42 | + |
| 43 | + const namespace = phpFile.addNamespace(PhpNamespace.fromParts(namespaceParts).toString()); |
| 44 | + namespace.addUse(DataPatchGenerator.DATA_PATCH_INTERFACE); |
| 45 | + |
| 46 | + if (this.data.revertable) { |
| 47 | + namespace.addUse(DataPatchGenerator.PATCH_REVERTABLE_INTERFACE); |
| 48 | + } |
| 49 | + |
| 50 | + const patchClass = namespace.addClass(this.data.className); |
| 51 | + patchClass.addImplement(DataPatchGenerator.DATA_PATCH_INTERFACE); |
| 52 | + |
| 53 | + if (this.data.revertable) { |
| 54 | + patchClass.addImplement(DataPatchGenerator.PATCH_REVERTABLE_INTERFACE); |
| 55 | + } |
| 56 | + |
| 57 | + // Add apply method |
| 58 | + const applyMethod = patchClass.addMethod('apply'); |
| 59 | + applyMethod.addComment('@inheritdoc'); |
| 60 | + applyMethod.setReturnType('self'); |
| 61 | + applyMethod.setBody('// TODO: Implement apply() method.\n\nreturn $this;'); |
| 62 | + |
| 63 | + // Add revert method if revertable |
| 64 | + if (this.data.revertable) { |
| 65 | + const revertMethod = patchClass.addMethod('revert'); |
| 66 | + revertMethod.addComment('@inheritdoc'); |
| 67 | + revertMethod.setReturnType('void'); |
| 68 | + revertMethod.setBody('// TODO: Implement revert() method.'); |
| 69 | + } |
| 70 | + |
| 71 | + // Add getAliases method |
| 72 | + const getAliasesMethod = patchClass.addMethod('getAliases'); |
| 73 | + getAliasesMethod.addComment('@inheritdoc'); |
| 74 | + getAliasesMethod.setReturnType('array'); |
| 75 | + getAliasesMethod.setBody('return [];'); |
| 76 | + |
| 77 | + // Add getDependencies method |
| 78 | + const getDependenciesMethod = patchClass.addMethod('getDependencies'); |
| 79 | + getDependenciesMethod.setStatic(true); |
| 80 | + getDependenciesMethod.addComment('@inheritdoc'); |
| 81 | + getDependenciesMethod.setReturnType('array'); |
| 82 | + getDependenciesMethod.setBody('return [];'); |
| 83 | + |
| 84 | + const printer = new PsrPrinter(); |
| 85 | + |
| 86 | + return new GeneratedFile( |
| 87 | + Uri.joinPath(moduleDirectory, setupDir, `${this.data.className}.php`), |
| 88 | + printer.printFile(phpFile) |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments