-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttributeSchemaObserver.php
241 lines (197 loc) · 7.27 KB
/
AttributeSchemaObserver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
namespace Amethyst\Observers;
use Amethyst\Models\AttributeSchema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Symfony\Component\Yaml\Yaml;
class AttributeSchemaObserver
{
/**
* Handle the AttributeSchema "created" event.
*
* @param \Amethyst\Models\AttributeSchema $attributeSchema
*/
public function created(AttributeSchema $attributeSchema)
{
$this->reload($attributeSchema);
if ($attributeSchema->schema === 'BelongsTo' || $attributeSchema->schema === 'MorphTo') {
$this->syncRelationSchema($attributeSchema);
}
}
/**
* Handle the AttributeSchema "updated" event.
*
* @param \Amethyst\Models\AttributeSchema $attributeSchema
*/
public function updated(AttributeSchema $attributeSchema)
{
$this->reload($attributeSchema);
}
/**
* Handle the AttributeSchema "created" event.
*
* @param \Amethyst\Models\AttributeSchema $attributeSchema
*/
public function creating(AttributeSchema $attributeSchema)
{
$this->updating($attributeSchema, false);
}
/**
* Handle the AttributeSchema "saving" event.
*
* @param \Amethyst\Models\AttributeSchema $attributeSchema
*/
public function saving(AttributeSchema $attributeSchema)
{
$attributeSchema->getResolver()->saving();
}
/**
* Handle the AttributeSchema "updated" event.
*
* @param \Amethyst\Models\AttributeSchema $attributeSchema
* @param bool $onChange
*/
public function updating(AttributeSchema $attributeSchema, bool $onChange = true)
{
$data = app('amethyst')->findDataByName($attributeSchema->model);
$options = (object) Yaml::parse((string) $attributeSchema->options);
Schema::table($data->newEntity()->getTable(), function (Blueprint $table) use ($attributeSchema, $onChange) {
if ($onChange) {
$oldName = $attributeSchema->getOriginal()['name'];
if ($attributeSchema->name !== $oldName) {
$table->renameColumn($oldName, $attributeSchema->name);
}
}
});
Schema::table($data->newEntity()->getTable(), function (Blueprint $table) use ($attributeSchema, $onChange, $options) {
$method = !empty($options->column) ? $options->column : $this->getMethod($attributeSchema);
$arguments = $attributeSchema->getResolver()->getDatabaseArguments();
$column = $table->$method(...$arguments);
$attributeSchema->getResolver()->callDatabaseOptions($column);
$column = $column->nullable();
if ($onChange) {
$column->change();
}
});
}
/**
* Handle the AttributeSchema "deleting" event.
*
* @param \Amethyst\Models\AttributeSchema $attributeSchema
*/
public function deleting(AttributeSchema $attributeSchema)
{
$attributeSchema->getResolver()->deleting();
$data = app('amethyst')->findDataByName($attributeSchema->model);
Schema::table($data->newEntity()->getTable(), function (Blueprint $table) use ($attributeSchema) {
$column = $table->dropColumn($attributeSchema->name);
});
}
/**
* Handle the AttributeSchema "deleted" event.
*
* @param \Amethyst\Models\AttributeSchema $attributeSchema
*/
public function deleted(AttributeSchema $attributeSchema)
{
$this->reload($attributeSchema);
}
/**
* @param AttributeSchema $attributeSchema
*
* @return string
*/
public function getMethod(AttributeSchema $attributeSchema): string
{
return $attributeSchema->getResolver()->getInstanceAttribute()->getSchema();
}
public function reload(AttributeSchema $attributeSchema)
{
app('amethyst.attribute-schema')->reload();
$data = app('amethyst')->findDataByName($attributeSchema->model);
// Reloading manager
$data->boot();
$model = $data->newEntity();
// Reloading model
$model->ini($model->__config, true);
event(new \Railken\EloquentMapper\Events\EloquentMapUpdate($attributeSchema->model));
}
/**
* Sync with Relation schema for attributes BelongsTo and MorphTo.
*
* @param \Amethyst\Models\AttributeSchema $attributeSchema
*/
public function syncRelationSchema(AttributeSchema $attributeSchema)
{
$newOptions = (object) Yaml::parse($attributeSchema->options);
$originalOptions = (object) Yaml::parse(
$attributeSchema->getOriginal()['options']
?? Yaml::dump(['relationName' => $newOptions->relationName])
);
$newRelationName = $newOptions->relationName;
$oldRelationName = $originalOptions->relationName;
$this->renameRelationSchemaIfRequired(
$attributeSchema,
$originalOptions->relationName,
$newOptions->relationName
);
$this->changePayloadSchemaIfRequired(
$attributeSchema,
$newOptions->relationName,
$originalOptions,
$newOptions
);
}
public function renameRelationSchemaIfRequired(AttributeSchema $attributeSchema, $oldRelationName, $newRelationName)
{
if ($oldRelationName === $newRelationName) {
return;
}
$relation = app('amethyst')->get('relation-schema')->getRepository()->findOneBy([
'data' => $attributeSchema->model,
'name' => $oldRelationName,
]);
$relation->name = $newRelationName;
$relation->save();
}
public function changePayloadSchemaIfRequired(AttributeSchema $attributeSchema, $relationName, $oldPayload, $newPayload)
{
if ($oldPayload === $newPayload) {
return;
}
$relation = app('amethyst')->get('relation-schema')->getRepository()->findOneBy([
'data' => $attributeSchema->model,
'name' => $relationName,
]);
if ($relation) {
$relation->payload = $this->convertAttributeOptionsToRelationPayload($attributeSchema, $newPayload);
$relation->save();
} else {
app('amethyst')->get('relation-schema')->createOrFail([
'data' => $attributeSchema->model,
'name' => $relationName,
'type' => $attributeSchema->schema,
'payload' => $this->convertAttributeOptionsToRelationPayload($attributeSchema, $newPayload),
]);
}
}
public function convertAttributeOptionsToRelationPayload(AttributeSchema $attributeSchema, $payload)
{
$obj = [];
if ($attributeSchema->schema === 'BelongsTo') {
if (!empty($payload->relationData)) {
$obj['target'] = $payload->relationData;
}
}
if (!empty($payload->ownerKey)) {
$obj['ownerKey'] = $payload->ownerKey;
}
if (!empty($payload->relationKey)) {
$obj['keyName'] = $payload->relationKey;
}
if ($attributeSchema->schema === 'MorphTo') {
$obj['keyId'] = $attributeSchema->name;
}
return Yaml::dump($obj);
}
}