@omnifyjp/ts 6.3.8 → 6.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,5 +4,6 @@
|
|
|
4
4
|
import { SchemaReader } from './schema-reader.js';
|
|
5
5
|
import type { GeneratedFile, PhpConfig } from './types.js';
|
|
6
6
|
/** Generate base model and user model for all project-owned object schemas,
|
|
7
|
-
* plus
|
|
7
|
+
* plus, for each package schema the project extends, a user model that
|
|
8
|
+
* extends the package model and carries the extend's columns. */
|
|
8
9
|
export declare function generateModels(reader: SchemaReader, config: PhpConfig): GeneratedFile[];
|
|
@@ -8,24 +8,34 @@ import { buildRelation } from './relation-builder.js';
|
|
|
8
8
|
import { baseFile, userFile, shadowingPropertyConflicts, resolveModularBasePath, resolveModularBaseNamespace, resolveSharedBaseNamespace, resolveGlobalTraitNamespace, resolveGlobalEnumNamespace, resolveBaseClass, resolveEditableClass, nestByGroup, nestEditableByGroup, } from './types.js';
|
|
9
9
|
import { enumClassName } from './enum-generator.js';
|
|
10
10
|
/** Generate base model and user model for all project-owned object schemas,
|
|
11
|
-
* plus
|
|
11
|
+
* plus, for each package schema the project extends, a user model that
|
|
12
|
+
* extends the package model and carries the extend's columns. */
|
|
12
13
|
export function generateModels(reader, config) {
|
|
13
14
|
const files = [];
|
|
14
15
|
// Project-owned schemas: base model + user model
|
|
15
16
|
for (const [name, schema] of Object.entries(reader.getProjectObjectSchemas())) {
|
|
16
17
|
files.push(...generateForSchema(name, schema, reader, config));
|
|
17
18
|
}
|
|
18
|
-
// Package schemas
|
|
19
|
-
//
|
|
19
|
+
// Package schemas the project extends with `kind: extend`: a trait carrying
|
|
20
|
+
// the added columns (#164) plus a user model in the project namespace that
|
|
21
|
+
// `use`s it — the trait needs a class to live in, and the package's own
|
|
22
|
+
// model cannot be that class once it is installed under `vendor/`.
|
|
23
|
+
//
|
|
24
|
+
// A package schema the project does NOT extend gets nothing: the package
|
|
25
|
+
// model is the model, and relations already point at it. Every such
|
|
26
|
+
// schema used to get an empty `extends Package<Name>` stub anyway, and a
|
|
27
|
+
// workspace of three packages saw sixteen of them on every fresh checkout
|
|
28
|
+
// after deleting them (#182). A project that wants a local subclass for one
|
|
29
|
+
// of these writes the five lines itself, or extends the schema.
|
|
20
30
|
for (const [name, schema] of Object.entries(reader.getPackageObjectSchemas())) {
|
|
21
31
|
const pkgNs = reader.resolveModelNamespace(name, config.models.namespace);
|
|
22
|
-
if (pkgNs
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
if (pkgNs === config.models.namespace)
|
|
33
|
+
continue;
|
|
34
|
+
const extended = extendedPropertyNames(schema);
|
|
35
|
+
if (extended.length === 0)
|
|
36
|
+
continue;
|
|
37
|
+
files.push(generateExtendAttributesTrait(name, schema, extended, reader, config));
|
|
38
|
+
files.push(generatePackageUserModel(name, pkgNs, config, schema.kind === 'pivot'));
|
|
29
39
|
}
|
|
30
40
|
return files;
|
|
31
41
|
}
|
|
@@ -1056,8 +1066,9 @@ function buildAccessors(expandedProperties) {
|
|
|
1056
1066
|
}
|
|
1057
1067
|
return methods.join('\n');
|
|
1058
1068
|
}
|
|
1059
|
-
/** Generate a user model for a package schema
|
|
1060
|
-
|
|
1069
|
+
/** Generate a user model for a package schema the project extends: it
|
|
1070
|
+
* extends the package's model class and `use`s the extend-attributes trait. */
|
|
1071
|
+
function generatePackageUserModel(name, packageNamespace, config, isPivot = false) {
|
|
1061
1072
|
const modelName = toPascalCase(name);
|
|
1062
1073
|
const modelNamespace = config.models.namespace;
|
|
1063
1074
|
// #178: a `kind: pivot` schema's editable model is emitted under the
|
|
@@ -1074,12 +1085,8 @@ function generatePackageUserModel(name, packageNamespace, config, hasExtendAttri
|
|
|
1074
1085
|
// predates its extend keeps the file it has, and `generateModels` warns with
|
|
1075
1086
|
// the one line to add — overwriting somebody's model to add an import is the
|
|
1076
1087
|
// worse of the two.
|
|
1077
|
-
const traitImport =
|
|
1078
|
-
|
|
1079
|
-
: '';
|
|
1080
|
-
const traitUse = hasExtendAttributes
|
|
1081
|
-
? ` use ${traitName};\n\n`
|
|
1082
|
-
: '';
|
|
1088
|
+
const traitImport = `use ${modelNamespace}\\Concerns\\${traitName};\n`;
|
|
1089
|
+
const traitUse = ` use ${traitName};\n\n`;
|
|
1083
1090
|
const content = `<?php
|
|
1084
1091
|
|
|
1085
1092
|
namespace ${modelNamespace};
|
|
@@ -1097,16 +1104,14 @@ class ${modelName} extends Package${modelName}
|
|
|
1097
1104
|
${traitUse} // Add your custom methods here
|
|
1098
1105
|
}
|
|
1099
1106
|
`;
|
|
1100
|
-
return userFile(`${config.models.path}/${modelName}.php`, content,
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
}
|
|
1109
|
-
: undefined);
|
|
1107
|
+
return userFile(`${config.models.path}/${modelName}.php`, content, {
|
|
1108
|
+
needle: `use ${traitName};`,
|
|
1109
|
+
hint: `this project's \`kind: extend\` adds columns to the package-owned `
|
|
1110
|
+
+ `${modelName} schema, but this model predates the trait that carries `
|
|
1111
|
+
+ `them, so those columns are still not fillable or cast. Add `
|
|
1112
|
+
+ `\`use ${config.models.namespace}\\Concerns\\${traitName};\` and `
|
|
1113
|
+
+ `\`use ${traitName};\` inside the class (#164).`,
|
|
1114
|
+
});
|
|
1110
1115
|
}
|
|
1111
1116
|
/**
|
|
1112
1117
|
* Detect whether a schema uses nested set (tree structure).
|