|
1
|
|
|
<?php |
|
2
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
|
3
|
|
|
/** |
|
4
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
|
5
|
|
|
* on 22.03.16 at 15:46 |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace samsoncms\api\generator; |
|
8
|
|
|
|
|
9
|
|
|
use samsoncms\api\generator\metadata\GenericMetadata; |
|
10
|
|
|
use samsoncms\api\generator\metadata\Virtual; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Entity class generator. |
|
14
|
|
|
* |
|
15
|
|
|
* @package samsoncms\api\generator |
|
16
|
|
|
*/ |
|
17
|
|
|
class Entity extends Generic |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Class uses generation part. |
|
21
|
|
|
* |
|
22
|
|
|
* @param Virtual $metadata Entity metadata |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function createUses($metadata) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->generator |
|
27
|
|
|
->newLine('use samsonframework\core\ViewInterface;') |
|
28
|
|
|
->newLine(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class definition generation part. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Virtual $metadata Entity metadata |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function createDefinition($metadata) |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* TODO: Parent problem |
|
40
|
|
|
* Should be changed to merging fields instead of extending with OOP for structure_relation support |
|
41
|
|
|
* or creating traits and using them on shared parent entities. |
|
42
|
|
|
*/ |
|
43
|
|
|
$parentClass = null !== $metadata->parent |
|
44
|
|
|
? $metadata->parent->entityClassName |
|
45
|
|
|
: '\\'.\samsoncms\api\Entity::class; |
|
46
|
|
|
|
|
47
|
|
|
$this->generator |
|
48
|
|
|
->multiComment(array('"' . $metadata->entityRealName . '" database entity class')) |
|
49
|
|
|
->defClass($metadata->entity, $parentClass); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Class constants generation part. |
|
54
|
|
|
* |
|
55
|
|
|
* @param Virtual $metadata Entity metadata |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function createConstants($metadata) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->generator |
|
60
|
|
|
->commentVar('string', 'Entity full class name, use ::class instead') |
|
61
|
|
|
->defClassConst('ENTITY', $metadata->entityClassName) |
|
62
|
|
|
->commentVar('string', 'Entity manager full class name') |
|
63
|
|
|
->defClassConst('MANAGER', $metadata->entityClassName . 'Query') |
|
64
|
|
|
->commentVar('string', 'Entity database identifier') |
|
65
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
|
66
|
|
|
->commentVar('string', 'Not transliterated entity name') |
|
67
|
|
|
->defClassConst('NAME', $metadata->entityRealName); |
|
68
|
|
|
|
|
69
|
|
|
// Create all entity fields constants storing each additional field metadata |
|
70
|
|
|
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
|
71
|
|
|
$this->generator |
|
72
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name') |
|
73
|
|
|
->defClassConst('F_' . $fieldName, $fieldName) |
|
74
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier') |
|
75
|
|
|
->defClassConst('F_' . $fieldName . '_ID', $fieldID); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Class fields generation part. |
|
81
|
|
|
* |
|
82
|
|
|
* @param Virtual $metadata Entity metadata |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function createFields($metadata) |
|
85
|
|
|
{ |
|
86
|
|
|
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
|
87
|
|
|
$this->generator |
|
88
|
|
|
->commentVar($metadata->allFieldTypes[$fieldID], $metadata->fieldDescriptions[$fieldID]) |
|
89
|
|
|
->defClassVar('$' . $fieldName, 'public'); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Class methods generation part. |
|
95
|
|
|
* |
|
96
|
|
|
* @param Virtual $metadata Entity metadata |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function createMethods($metadata) |
|
99
|
|
|
{ |
|
100
|
|
|
$methods = []; |
|
101
|
|
|
/** @var Virtual $metadataInstance Iterate all metadata entities */ |
|
102
|
|
|
foreach (GenericMetadata::$instances as $metadataInstance) { |
|
103
|
|
|
if ($metadataInstance->type === Virtual::TYPE_TABLE) { |
|
104
|
|
|
// Create table virtual entity with correct name ending |
|
105
|
|
|
$tableEntity = rtrim($metadataInstance->entity, 'Table') . 'Table'; |
|
106
|
|
|
|
|
107
|
|
|
$code = "\n\t" . '/**'; |
|
108
|
|
|
$code .= "\n\t" . ' * Create virtual ' . $metadataInstance->entityRealName . ' table instance.'; |
|
109
|
|
|
$code .= "\n\t" . ' * @param ViewInterface $renderer Renderer instance'; |
|
110
|
|
|
$code .= "\n\t" . ' *'; |
|
111
|
|
|
$code .= "\n\t" . ' * @return $this Chaining'; |
|
112
|
|
|
$code .= "\n\t" . ' */'; |
|
113
|
|
|
$code .= "\n\t" . 'public function ' . lcfirst($tableEntity) . '(ViewInterface $renderer)'; |
|
114
|
|
|
$code .= "\n\t" . '{'; |
|
115
|
|
|
$code .= "\n\t\t" . 'return new ' . $tableEntity . '($renderer, $this->id);'; |
|
116
|
|
|
$code .= "\n\t" . '}'; |
|
117
|
|
|
|
|
118
|
|
|
$methods[] = $code; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// Add method text to generator |
|
123
|
|
|
$this->generator->text(implode("\n", $methods)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Class static fields generation part. |
|
128
|
|
|
* |
|
129
|
|
|
* @param Virtual $metadata Entity metadata |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function createStaticFields($metadata) |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->generator |
|
134
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
|
135
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
|
136
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
137
|
|
|
->defClassVar('$_sql_select', 'public static ', $metadata->arSelect) |
|
138
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
139
|
|
|
->defClassVar('$_attributes', 'public static ', $metadata->arAttributes) |
|
140
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
141
|
|
|
->defClassVar('$_map', 'public static ', $metadata->arMap) |
|
142
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
143
|
|
|
->defClassVar('$_sql_from', 'public static ', $metadata->arFrom) |
|
144
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
145
|
|
|
->defClassVar('$_own_group', 'public static ', $metadata->arGroup) |
|
146
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
147
|
|
|
->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias) |
|
148
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
149
|
|
|
->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType) |
|
150
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
151
|
|
|
->defClassVar('$_relations', 'public static ', $metadata->arRelations) |
|
152
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
153
|
|
|
->defClassVar('$fieldIDs', 'protected static ', $metadata->allFieldIDs) |
|
154
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
|
155
|
|
|
->defClassVar('$fieldValueColumns', 'protected static ', $metadata->allFieldValueColumns); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
//[PHPCOMPRESSOR(remove,end)] |