@omnifyjp/ts 5.8.8 → 5.8.10
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.
- package/dist/php/factory-generator.js +47 -11
- package/dist/php/model-generator.js +32 -34
- package/package.json +1 -1
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { toPascalCase } from './naming-helper.js';
|
|
5
5
|
import { toFaker, compoundFaker, associationFaker } from './faker-mapper.js';
|
|
6
|
-
import { userFile
|
|
6
|
+
import { userFile } from './types.js';
|
|
7
7
|
/** Generate Factory classes for all project-owned visible object schemas. */
|
|
8
8
|
export function generateFactories(reader, config) {
|
|
9
9
|
const files = [];
|
|
@@ -15,16 +15,51 @@ export function generateFactories(reader, config) {
|
|
|
15
15
|
}
|
|
16
16
|
return files;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Issue #98 v5.8.9: resolve the EDITABLE Model namespace for a given
|
|
20
|
+
* schema. Factory `$model` + association FK references must land on the
|
|
21
|
+
* user-editable class (e.g. `App\\Models\\Banner`) — that's what
|
|
22
|
+
* services / controllers / tests / seeders type-hint against, and what
|
|
23
|
+
* the project owns. Pre-fix every factory pointed at the BASE
|
|
24
|
+
* (e.g. `App\\Omnify\\Models\\Cms\\Banner`); calling
|
|
25
|
+
* `Banner::factory()->create()` returned a base instance and every
|
|
26
|
+
* downstream signature failed with TypeError.
|
|
27
|
+
*
|
|
28
|
+
* Pivot schemas always nest under `Pivot/` regardless of any flag.
|
|
29
|
+
* Group nesting only applies when the layer's `userEditableGroupByFolder`
|
|
30
|
+
* is true (matches where the editable file actually lives).
|
|
31
|
+
*/
|
|
32
|
+
function resolveEditableModelNamespace(schemaName, config, reader) {
|
|
33
|
+
const targetSchema = reader.getSchema(schemaName);
|
|
34
|
+
// Modular structure: editable shares per-Schema modular path.
|
|
35
|
+
if (config.structure === 'modular') {
|
|
36
|
+
return config.models.namespace;
|
|
37
|
+
}
|
|
38
|
+
const editableNs = config.models.userEditableNamespace;
|
|
39
|
+
// Package schemas: use the package's model namespace verbatim
|
|
40
|
+
// (we don't generate editable stubs for package schemas).
|
|
41
|
+
const baseFromReader = reader.resolveModelNamespace(schemaName, config.models.namespace);
|
|
42
|
+
if (baseFromReader !== config.models.namespace) {
|
|
43
|
+
return baseFromReader;
|
|
44
|
+
}
|
|
45
|
+
if (!targetSchema)
|
|
46
|
+
return editableNs;
|
|
47
|
+
if (targetSchema.kind === 'pivot') {
|
|
48
|
+
return `${editableNs}\\Pivot`;
|
|
49
|
+
}
|
|
50
|
+
if (config.models.userEditableGroupByFolder && targetSchema.group) {
|
|
51
|
+
return `${editableNs}\\${toPascalCase(targetSchema.group)}`;
|
|
52
|
+
}
|
|
53
|
+
return editableNs;
|
|
54
|
+
}
|
|
18
55
|
function generateFactory(name, schema, reader, schemas, config) {
|
|
19
56
|
const modelName = toPascalCase(name);
|
|
20
|
-
// Issue #98 v5.8.
|
|
21
|
-
//
|
|
22
|
-
// the
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
const modelNamespace = config
|
|
26
|
-
? config.models.namespace
|
|
27
|
-
: nestByGroup({ path: '', namespace: config.models.namespace }, schema.group).namespace;
|
|
57
|
+
// Issue #98 v5.8.9: factories type the `$model` reference + associated
|
|
58
|
+
// FK references against the USER-EDITABLE class (`App\Models\Banner`)
|
|
59
|
+
// not the base (`App\Omnify\Models\Cms\Banner`). Service / controller /
|
|
60
|
+
// test signatures all type-hint against the editable; pointing
|
|
61
|
+
// factories at the base produced TypeErrors at every callsite.
|
|
62
|
+
const modelNamespace = resolveEditableModelNamespace(name, config, reader);
|
|
28
63
|
const factoryNamespace = config.factories.namespace;
|
|
29
64
|
const properties = (schema.properties ?? {});
|
|
30
65
|
const expandedProperties = reader.getExpandedProperties(name);
|
|
@@ -38,9 +73,10 @@ function generateFactory(name, schema, reader, schemas, config) {
|
|
|
38
73
|
const type = prop['type'] ?? 'String';
|
|
39
74
|
// Handle associations (foreign keys)
|
|
40
75
|
if (type === 'Association') {
|
|
41
|
-
// Resolve namespace per-target
|
|
76
|
+
// Resolve namespace per-target — also points at the EDITABLE
|
|
77
|
+
// (matches what `<Target>::factory()` resolves to via Eloquent).
|
|
42
78
|
const target = prop['target'] ?? '';
|
|
43
|
-
const targetNs = target ?
|
|
79
|
+
const targetNs = target ? resolveEditableModelNamespace(target, config, reader) : modelNamespace;
|
|
44
80
|
const result = associationFaker(propName, prop, targetNs, name);
|
|
45
81
|
if (result) {
|
|
46
82
|
attributes.push(result.fake);
|
|
@@ -579,48 +579,46 @@ function buildCasts(properties, expandedProperties, propertyOrder, reader, confi
|
|
|
579
579
|
return casts.map(c => ` ${c}`).join('\n') + '\n';
|
|
580
580
|
}
|
|
581
581
|
/**
|
|
582
|
-
* Issue #98 v5.8.
|
|
583
|
-
*
|
|
584
|
-
*
|
|
582
|
+
* Issue #98 v5.8.10: resolve the FQCN namespace of a related Model for
|
|
583
|
+
* inline `belongsTo / hasMany / belongsToMany / morphTo` references.
|
|
584
|
+
* Relations must resolve to the USER-EDITABLE class — that's what
|
|
585
|
+
* services / controllers / tests / seeders type-hint against, and what
|
|
586
|
+
* `<Target>::factory()` returns since v5.8.9. Pre-fix relations pointed
|
|
587
|
+
* at the BASE class FQN, so loaded relations were base instances and
|
|
588
|
+
* every downstream type-hinted call failed with `TypeError`. Same root
|
|
589
|
+
* cause + fix as the factory `$model` issue.
|
|
585
590
|
*
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
* 3.
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
* mirrors the base group. Nest by group so the FQCN matches the
|
|
596
|
-
* grouped editable.
|
|
597
|
-
* 5. flatBase: false + userEditableGroupByFolder: false (Laravel-
|
|
598
|
-
* canonical default) — the editable is FLAT. Don't nest; the
|
|
599
|
-
* FQCN matches the flat editable's location.
|
|
600
|
-
*
|
|
601
|
-
* Pre-v5.8.6 every relation emitted `\${modelNamespace}\${target}::class`
|
|
602
|
-
* (flat) regardless of where the target's file actually lived. Grouped
|
|
603
|
-
* projects (especially flatBase) hit `ErrorException: include(... no
|
|
604
|
-
* such file)` on every cross-Model reference.
|
|
591
|
+
* Resolution rules (mirror `factory-generator.ts:resolveEditableModelNamespace`):
|
|
592
|
+
* 1. Package schemas — return the package's model namespace verbatim
|
|
593
|
+
* (we don't generate editable stubs for package schemas).
|
|
594
|
+
* 2. Pivot schemas — nest under `<editableNs>\Pivot` regardless of
|
|
595
|
+
* any group / flag (matches the dedicated `Pivot/` subfolder).
|
|
596
|
+
* 3. `userEditableGroupByFolder: true` + target has group → nest by
|
|
597
|
+
* group so the FQCN matches where the editable file lives.
|
|
598
|
+
* 4. Default — flat editable namespace (Laravel-canonical
|
|
599
|
+
* `App\Models\<Name>`).
|
|
605
600
|
*/
|
|
606
601
|
function resolveRelatedModelNamespace(target, modelNamespace, config, reader) {
|
|
607
|
-
|
|
608
|
-
//
|
|
609
|
-
|
|
610
|
-
|
|
602
|
+
// Package schemas use their own model namespace (we don't generate
|
|
603
|
+
// editable stubs for them — relations dispatch to the package class).
|
|
604
|
+
const packageNs = reader.resolveModelNamespace(target, modelNamespace);
|
|
605
|
+
if (packageNs !== modelNamespace)
|
|
606
|
+
return packageNs;
|
|
607
|
+
// Modular structure: editable shares per-Schema modular path.
|
|
608
|
+
if (config.structure === 'modular') {
|
|
609
|
+
return config.models.namespace;
|
|
610
|
+
}
|
|
611
611
|
const targetSchema = reader.getSchema(target);
|
|
612
612
|
if (!targetSchema)
|
|
613
|
-
return
|
|
613
|
+
return config.models.userEditableNamespace;
|
|
614
|
+
const editableNs = config.models.userEditableNamespace;
|
|
614
615
|
if (targetSchema.kind === 'pivot') {
|
|
615
|
-
return `${
|
|
616
|
+
return `${editableNs}\\Pivot`;
|
|
616
617
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
const shouldNestByGroup = config.models.flatBase || config.models.userEditableGroupByFolder;
|
|
620
|
-
if (shouldNestByGroup && targetSchema.group) {
|
|
621
|
-
return `${baseNs}\\${toPascalCase(targetSchema.group)}`;
|
|
618
|
+
if (config.models.userEditableGroupByFolder && targetSchema.group) {
|
|
619
|
+
return `${editableNs}\\${toPascalCase(targetSchema.group)}`;
|
|
622
620
|
}
|
|
623
|
-
return
|
|
621
|
+
return editableNs;
|
|
624
622
|
}
|
|
625
623
|
function buildRelations(schemaName, properties, propertyOrder, modelNamespace, reader, config) {
|
|
626
624
|
const methods = [];
|