-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityDefinition.php
More file actions
204 lines (164 loc) · 6.01 KB
/
EntityDefinition.php
File metadata and controls
204 lines (164 loc) · 6.01 KB
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
<?php
namespace CustomEntity;
use MapasCulturais\App;
use MapasCulturais\Definitions\FileGroup;
use MapasCulturais\Definitions\Metadata;
use MapasCulturais\i;
class EntityDefinition
{
public readonly string $entityClassName;
public readonly string $controllerClassName;
public readonly EntityGenerator $entityGenerator;
public readonly ControllerGenerator $controllerGenerator;
public readonly EntityCssGenerator $entityCssGenerator;
public readonly array $editSections;
public readonly array $singleSections;
function __construct(
public readonly string $slug,
public readonly OwnerPart $owner,
/**
* Nome do ícone definido no init do componente mc-icon ou um ícone qualquer do iconify.
* Se o valor informado conter o caracter : (dois pontos) será considerado que é um ícone do iconify.
*/
public readonly string $icon = 'app',
public readonly string $color = '#19d758',
public readonly array $texts = [],
/** @var Part[] */
public readonly array $parts = [],
?array $editSections = null,
?array $singleSections = null,
public ?string $entity = null,
public ?string $table = null,
) {
$this->entity = $this->entity ?: ucfirst($this->slug);
$this->table = $this->table ?: $this->slug;
$this->entityGenerator = new EntityGenerator($this);
$this->controllerGenerator = new ControllerGenerator($this);
$this->entityCssGenerator = new EntityCssGenerator($this);
$this->entityClassName = $this->entityGenerator->className;
$this->controllerClassName = $this->controllerGenerator->className;
$this->editSections = $editSections ?? [
'more-info' => i::__('Mais informações'),
];
$this->singleSections = $singleSections ?? [
'more-info' => i::__('Mais informações'),
];
}
function init()
{
foreach ($this->getParts() as $part) {
$part->init($this);
}
$app = App::i();
$self = $this;
// define o ícone da entidade
$app->hook('component(mc-icon).iconset', function(&$iconset) use($self) {
if(strpos($self->icon, ':')){
$iconset[$self->slug] = $self->icon;
} else {
$iconset[$self->slug] = $iconset[$self->icon] ?? '';
}
});
$app->hook('GET(<<*>>):before', function () use($self, $app) {
/** @var EntityDefinition $this */
$app->view->enqueueStyle('app-v2', 'custom-entity--' . $self->slug, 'css/' . $self->entityCssGenerator->filename);
});
}
function register()
{
$app = App::i();
foreach ($this->getFileGroups() as $group) {
$app->registerFileGroup($this->slug, $group);
}
foreach ($this->getParts() as $part) {
$part->register($this);
foreach ($part->getEntityMetadata() as $key => $config) {
$definition = new Metadata($key, $config);
$app->registerMetadata($definition, $this->entityClassName);
}
}
}
function text(string $text): string
{
$replacements = [
'suas entidades',
'sua entidade',
'das entidades',
'da entidade',
'minhas entidades',
'minha entidade',
'as entidades',
'a entidade',
'entidades',
'entidade',
];
foreach ($replacements as $from) {
$to = $this->texts[$from] ?? false;
if (!$to) {
continue;
}
$text = str_replace(ucwords($from), ucwords($to), $text);
$text = str_replace(ucfirst($from), ucfirst($to), $text);
$text = str_replace(mb_strtolower($from), mb_strtolower($to), $text);
$text = str_replace(mb_strtoupper($from), mb_strtoupper($to), $text);
}
return $text;
}
function renderTemplate(string $filename, array $traits = [], array $replacements = []): string
{
$content = file_get_contents(__DIR__ . '/templates/' . $filename);
$content = str_replace('_ENTITY_NAME_', $this->entity, $content);
$content = str_replace('_ENTITY_TABLE_', $this->table, $content);
$content = str_replace('_ENTITY_SLUG_', $this->slug, $content);
foreach ($traits as $trait) {
$trait = preg_replace('#^' . __NAMESPACE__ . '\\\#', '', $trait);
$content = str_replace('/** TRAITS **/', "/** TRAITS **/\n use $trait;", $content);
}
foreach ($replacements as $from => $to) {
$content = str_replace($from, $to, $content);
}
return $content;
}
/**
* @return Part[]
*/
function getParts(?Part $parent = null): array
{
$parts = $parent ? $parent->getSubParts() : $this->parts;
foreach ($parts as $part) {
$parts = array_merge($parts, $part->getSubParts());
}
return [$this->owner, ...$parts];
}
function getValidations(): array
{
$validations = [];
foreach ($this->getParts() as $part) {
foreach ($part->getEntityValidations() as $prop => $property_validations) {
if (isset($validations[$prop])) {
$validations[$prop] = array_merge($validations[$prop], $property_validations);
} else {
$validations[$prop] = $property_validations;
}
}
}
return $validations;
}
/**
* @return FileGroup[]
*/
function getFileGroups(): array
{
$file_groups = [];
foreach ($this->getParts() as $part) {
$file_groups = array_merge($file_groups, $part->getFileGroups());
}
return $file_groups;
}
public function getSingleSections(): array
{
$sections = [];
// foreach($this->entityGenerator)
return $sections;
}
}