@omnifyjp/ts 1.1.5 → 1.1.7
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.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Generates the abstract BaseModel class.
|
|
3
|
+
* Morph map registration is handled exclusively by OmnifyServiceProvider.
|
|
3
4
|
*/
|
|
4
|
-
import { SchemaReader } from './schema-reader.js';
|
|
5
5
|
import type { GeneratedFile, PhpConfig } from './types.js';
|
|
6
|
-
/** Generate the abstract BaseModel class
|
|
7
|
-
export declare function generateBaseModel(
|
|
6
|
+
/** Generate the abstract BaseModel class. */
|
|
7
|
+
export declare function generateBaseModel(config: PhpConfig): GeneratedFile[];
|
|
@@ -1,30 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Generates the abstract BaseModel class.
|
|
3
|
+
* Morph map registration is handled exclusively by OmnifyServiceProvider.
|
|
3
4
|
*/
|
|
4
|
-
import { toPascalCase } from './naming-helper.js';
|
|
5
5
|
import { baseFile } from './types.js';
|
|
6
|
-
/** Generate the abstract BaseModel class
|
|
7
|
-
export function generateBaseModel(
|
|
6
|
+
/** Generate the abstract BaseModel class. */
|
|
7
|
+
export function generateBaseModel(config) {
|
|
8
8
|
const baseNamespace = config.models.baseNamespace;
|
|
9
|
-
const modelNamespace = config.models.namespace;
|
|
10
|
-
// Build morph map entries from project-owned visible object schemas
|
|
11
|
-
const morphMap = {};
|
|
12
|
-
for (const name of Object.keys(reader.getProjectVisibleObjectSchemas())) {
|
|
13
|
-
const modelName = toPascalCase(name);
|
|
14
|
-
morphMap[modelName] = `\\${modelNamespace}\\${modelName}::class`;
|
|
15
|
-
}
|
|
16
|
-
// Sort for consistent output
|
|
17
|
-
const sortedKeys = Object.keys(morphMap).sort();
|
|
18
|
-
const morphEntries = sortedKeys
|
|
19
|
-
.map(k => ` '${k}' => ${morphMap[k]},`)
|
|
20
|
-
.join('\n');
|
|
21
9
|
const content = `<?php
|
|
22
10
|
|
|
23
11
|
namespace ${baseNamespace};
|
|
24
12
|
|
|
25
13
|
/**
|
|
26
14
|
* Base model class for all Omnify-generated models.
|
|
27
|
-
* Contains model mapping for polymorphic relations.
|
|
28
15
|
*
|
|
29
16
|
* DO NOT EDIT - This file is auto-generated by Omnify.
|
|
30
17
|
* Any changes will be overwritten on next generation.
|
|
@@ -33,35 +20,9 @@ namespace ${baseNamespace};
|
|
|
33
20
|
*/
|
|
34
21
|
|
|
35
22
|
use Illuminate\\Database\\Eloquent\\Model;
|
|
36
|
-
use Illuminate\\Database\\Eloquent\\Relations\\Relation;
|
|
37
23
|
|
|
38
24
|
abstract class BaseModel extends Model
|
|
39
25
|
{
|
|
40
|
-
/**
|
|
41
|
-
* Model class map for polymorphic relations.
|
|
42
|
-
*/
|
|
43
|
-
protected static array $modelMap = [
|
|
44
|
-
${morphEntries}
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Boot the model and register morph map.
|
|
49
|
-
*/
|
|
50
|
-
protected static function boot(): void
|
|
51
|
-
{
|
|
52
|
-
parent::boot();
|
|
53
|
-
|
|
54
|
-
// Register morph map for polymorphic relations
|
|
55
|
-
Relation::enforceMorphMap(static::$modelMap);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Get the model class for a given morph type.
|
|
60
|
-
*/
|
|
61
|
-
public static function getModelClass(string $morphType): ?string
|
|
62
|
-
{
|
|
63
|
-
return static::$modelMap[$morphType] ?? null;
|
|
64
|
-
}
|
|
65
26
|
}
|
|
66
27
|
`;
|
|
67
28
|
return [
|
package/dist/php/index.js
CHANGED
|
@@ -29,7 +29,7 @@ export function generatePhp(data, overrides) {
|
|
|
29
29
|
const config = derivePhpConfig(overrides);
|
|
30
30
|
const files = [];
|
|
31
31
|
// Infrastructure files
|
|
32
|
-
files.push(...generateBaseModel(
|
|
32
|
+
files.push(...generateBaseModel(config));
|
|
33
33
|
files.push(...generateTrait(config));
|
|
34
34
|
files.push(...generateServiceProvider(reader, config));
|
|
35
35
|
// Per-schema files
|
|
@@ -167,6 +167,12 @@ function generateUserModel(name, config) {
|
|
|
167
167
|
const factoryNamespace = config.factories.namespace;
|
|
168
168
|
const content = `<?php
|
|
169
169
|
|
|
170
|
+
/**
|
|
171
|
+
* ${modelName} Model
|
|
172
|
+
*
|
|
173
|
+
* SAFE TO EDIT - This file is never overwritten by Omnify.
|
|
174
|
+
*/
|
|
175
|
+
|
|
170
176
|
namespace ${modelNamespace};
|
|
171
177
|
|
|
172
178
|
use ${baseNamespace}\\${modelName}BaseModel;
|
|
@@ -174,23 +180,12 @@ use ${factoryNamespace}\\${modelName}Factory;
|
|
|
174
180
|
use Illuminate\\Database\\Eloquent\\Factories\\HasFactory;
|
|
175
181
|
|
|
176
182
|
/**
|
|
177
|
-
* ${modelName}
|
|
178
|
-
*
|
|
179
|
-
* This file is generated once and can be customized.
|
|
180
|
-
* Add your custom methods and logic here.
|
|
183
|
+
* ${modelName} — add project-specific model logic here.
|
|
181
184
|
*/
|
|
182
185
|
class ${modelName} extends ${modelName}BaseModel
|
|
183
186
|
{
|
|
184
187
|
use HasFactory;
|
|
185
188
|
|
|
186
|
-
/**
|
|
187
|
-
* Create a new model instance.
|
|
188
|
-
*/
|
|
189
|
-
public function __construct(array $attributes = [])
|
|
190
|
-
{
|
|
191
|
-
parent::__construct($attributes);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
189
|
/**
|
|
195
190
|
* Create a new factory instance for the model.
|
|
196
191
|
*/
|
|
@@ -199,7 +194,7 @@ class ${modelName} extends ${modelName}BaseModel
|
|
|
199
194
|
return ${modelName}Factory::new();
|
|
200
195
|
}
|
|
201
196
|
|
|
202
|
-
//
|
|
197
|
+
//
|
|
203
198
|
}
|
|
204
199
|
`;
|
|
205
200
|
return userFile(`${config.models.path}/${modelName}.php`, content);
|
|
@@ -113,6 +113,35 @@ use ${localesClass};
|
|
|
113
113
|
|
|
114
114
|
abstract class ${modelName}${action}RequestBase extends FormRequest
|
|
115
115
|
{
|
|
116
|
+
/**
|
|
117
|
+
* Determine if the user is authorized to make this request.
|
|
118
|
+
* Override this method in the concrete request class to add authorization logic.
|
|
119
|
+
*/
|
|
120
|
+
public function authorize(): bool
|
|
121
|
+
{
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Get the validation rules that apply to the request.
|
|
127
|
+
*
|
|
128
|
+
* @return array<string, \\Illuminate\\Contracts\\Validation\\ValidationRule|array<mixed>|string>
|
|
129
|
+
*/
|
|
130
|
+
public function rules(): array
|
|
131
|
+
{
|
|
132
|
+
return $this->schemaRules();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Get custom attributes for validator errors.
|
|
137
|
+
*
|
|
138
|
+
* @return array<string, string>
|
|
139
|
+
*/
|
|
140
|
+
public function attributes(): array
|
|
141
|
+
{
|
|
142
|
+
return $this->schemaAttributes();
|
|
143
|
+
}
|
|
144
|
+
|
|
116
145
|
/**
|
|
117
146
|
* Get the schema-defined validation rules.
|
|
118
147
|
*
|
|
@@ -167,54 +196,19 @@ function generateUserRequest(name, config, action) {
|
|
|
167
196
|
|
|
168
197
|
namespace ${requestNamespace};
|
|
169
198
|
|
|
170
|
-
use Illuminate\\Http\\Request;
|
|
171
199
|
use ${baseNamespace}\\${modelName}${action}RequestBase;
|
|
172
200
|
|
|
201
|
+
/**
|
|
202
|
+
* ${modelName}${action}Request — add project-specific authorization and validation here.
|
|
203
|
+
*
|
|
204
|
+
* Inherited from base:
|
|
205
|
+
* - authorize(): bool (returns true — override for auth checks)
|
|
206
|
+
* - rules(): array (returns schemaRules() — override to add custom rules)
|
|
207
|
+
* - attributes(): array (returns schemaAttributes() — override to rename fields)
|
|
208
|
+
*/
|
|
173
209
|
class ${modelName}${action}Request extends ${modelName}${action}RequestBase
|
|
174
210
|
{
|
|
175
|
-
|
|
176
|
-
* Determine if the user is authorized to make this request.
|
|
177
|
-
*/
|
|
178
|
-
public function authorize(): bool
|
|
179
|
-
{
|
|
180
|
-
return true;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Get the validation rules that apply to the request.
|
|
185
|
-
*
|
|
186
|
-
* @return array<string, \\Illuminate\\Contracts\\Validation\\ValidationRule|array<mixed>|string>
|
|
187
|
-
*/
|
|
188
|
-
public function rules(): array
|
|
189
|
-
{
|
|
190
|
-
return array_merge($this->schemaRules(), [
|
|
191
|
-
// Custom rules here
|
|
192
|
-
]);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Get custom attributes for validator errors.
|
|
197
|
-
*
|
|
198
|
-
* @return array<string, string>
|
|
199
|
-
*/
|
|
200
|
-
public function attributes(): array
|
|
201
|
-
{
|
|
202
|
-
return array_merge($this->schemaAttributes(), [
|
|
203
|
-
// Custom attributes here
|
|
204
|
-
]);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Get the error messages for the defined validation rules.
|
|
209
|
-
*
|
|
210
|
-
* @return array<string, string>
|
|
211
|
-
*/
|
|
212
|
-
public function messages(): array
|
|
213
|
-
{
|
|
214
|
-
return [
|
|
215
|
-
// Custom messages here
|
|
216
|
-
];
|
|
217
|
-
}
|
|
211
|
+
//
|
|
218
212
|
}
|
|
219
213
|
`;
|
|
220
214
|
return userFile(`${config.requests.path}/${modelName}${action}Request.php`, content);
|
|
@@ -89,6 +89,16 @@ use Illuminate\\Http\\Resources\\Json\\JsonResource;
|
|
|
89
89
|
|
|
90
90
|
class ${modelName}ResourceBase extends JsonResource
|
|
91
91
|
{
|
|
92
|
+
/**
|
|
93
|
+
* Transform the resource into an array.
|
|
94
|
+
*
|
|
95
|
+
* @return array<string, mixed>
|
|
96
|
+
*/
|
|
97
|
+
public function toArray(Request $request): array
|
|
98
|
+
{
|
|
99
|
+
return $this->schemaArray($request);
|
|
100
|
+
}
|
|
101
|
+
|
|
92
102
|
/**
|
|
93
103
|
* Transform the schema-defined fields into an array.
|
|
94
104
|
*
|
|
@@ -118,34 +128,17 @@ function generateUserResource(name, config) {
|
|
|
118
128
|
|
|
119
129
|
namespace ${resourceNamespace};
|
|
120
130
|
|
|
121
|
-
use Illuminate\\Http\\Request;
|
|
122
131
|
use ${baseNamespace}\\${modelName}ResourceBase;
|
|
123
132
|
|
|
133
|
+
/**
|
|
134
|
+
* ${modelName}Resource — add project-specific serialization here.
|
|
135
|
+
*
|
|
136
|
+
* Inherited from base:
|
|
137
|
+
* - toArray(Request \\$request): array (returns schemaArray(\\$request) — override to add fields)
|
|
138
|
+
*/
|
|
124
139
|
class ${modelName}Resource extends ${modelName}ResourceBase
|
|
125
140
|
{
|
|
126
|
-
|
|
127
|
-
* Transform the resource into an array.
|
|
128
|
-
*
|
|
129
|
-
* @return array<string, mixed>
|
|
130
|
-
*/
|
|
131
|
-
public function toArray(Request $request): array
|
|
132
|
-
{
|
|
133
|
-
return array_merge($this->schemaArray($request), [
|
|
134
|
-
// Custom fields here
|
|
135
|
-
]);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Get additional data that should be returned with the resource array.
|
|
140
|
-
*
|
|
141
|
-
* @return array<string, mixed>
|
|
142
|
-
*/
|
|
143
|
-
public function with(Request $request): array
|
|
144
|
-
{
|
|
145
|
-
return [
|
|
146
|
-
// Additional metadata here
|
|
147
|
-
];
|
|
148
|
-
}
|
|
141
|
+
//
|
|
149
142
|
}
|
|
150
143
|
`;
|
|
151
144
|
return userFile(`${config.resources.path}/${modelName}Resource.php`, content);
|