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\Virtual; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Entity class generator. |
13
|
|
|
* |
14
|
|
|
* @package samsoncms\api\generator |
15
|
|
|
*/ |
16
|
|
|
class Entity extends Generic |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Class uses generation part. |
20
|
|
|
* |
21
|
|
|
* @param Virtual $metadata Entity metadata |
22
|
|
|
*/ |
23
|
|
|
protected function createUses($metadata) |
24
|
|
|
{ |
25
|
|
|
$this->generator |
26
|
|
|
->newLine('use samsonframework\core\ViewInterface;') |
27
|
|
|
->newLine(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class definition generation part. |
32
|
|
|
* |
33
|
|
|
* @param Virtual $metadata Entity metadata |
34
|
|
|
*/ |
35
|
|
|
protected function createDefinition($metadata) |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* TODO: Parent problem |
39
|
|
|
* Should be changed to merging fields instead of extending with OOP for structure_relation support |
40
|
|
|
* or creating traits and using them on shared parent entities. |
41
|
|
|
*/ |
42
|
|
|
$parentClass = null !== $metadata->parent |
43
|
|
|
? $metadata->parent->entityClassName |
44
|
|
|
: '\\'.\samsoncms\api\Entity::class; |
45
|
|
|
|
46
|
|
|
$this->generator |
47
|
|
|
->multiComment(array('"' . $metadata->entityRealName . '" database entity class')) |
48
|
|
|
->defClass($metadata->entity, $parentClass) |
49
|
|
|
->newLine('use TableTrait;') |
50
|
|
|
->newLine();; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Class constants generation part. |
55
|
|
|
* |
56
|
|
|
* @param Virtual $metadata Entity metadata |
57
|
|
|
*/ |
58
|
|
|
protected function createConstants($metadata) |
59
|
|
|
{ |
60
|
|
|
$this->generator |
61
|
|
|
->commentVar('string', 'Entity full class name, use ::class instead') |
62
|
|
|
->defClassConst('ENTITY', $metadata->entityClassName) |
63
|
|
|
->commentVar('string', 'Entity manager full class name') |
64
|
|
|
->defClassConst('MANAGER', $metadata->entityClassName . 'Query') |
65
|
|
|
->commentVar('string', 'Entity database identifier') |
66
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
67
|
|
|
->commentVar('string', 'Not transliterated entity name') |
68
|
|
|
->defClassConst('NAME', $metadata->entityRealName); |
69
|
|
|
|
70
|
|
|
// Create all entity fields constants storing each additional field metadata |
71
|
|
|
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
72
|
|
|
$this->generator |
73
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name') |
74
|
|
|
->defClassConst('F_' . $fieldName, $fieldName) |
75
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier') |
76
|
|
|
->defClassConst('F_' . $fieldName . '_ID', $fieldID); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Class fields generation part. |
82
|
|
|
* |
83
|
|
|
* @param Virtual $metadata Entity metadata |
84
|
|
|
*/ |
85
|
|
|
protected function createFields($metadata) |
86
|
|
|
{ |
87
|
|
|
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
88
|
|
|
$this->generator |
89
|
|
|
->commentVar($metadata->allFieldTypes[$fieldID], $metadata->fieldDescriptions[$fieldID]) |
90
|
|
|
->defClassVar('$' . $fieldName, 'public'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Class static fields generation part. |
96
|
|
|
* |
97
|
|
|
* @param Virtual $metadata Entity metadata |
98
|
|
|
*/ |
99
|
|
|
protected function createStaticFields($metadata) |
100
|
|
|
{ |
101
|
|
|
return $this->generator |
102
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
103
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
104
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
105
|
|
|
->defClassVar('$_sql_select', 'public static ', $metadata->arSelect) |
106
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
107
|
|
|
->defClassVar('$_attributes', 'public static ', $metadata->arAttributes) |
108
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
109
|
|
|
->defClassVar('$_map', 'public static ', $metadata->arMap) |
110
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
111
|
|
|
->defClassVar('$_sql_from', 'public static ', $metadata->arFrom) |
112
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
113
|
|
|
->defClassVar('$_own_group', 'public static ', $metadata->arGroup) |
114
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
115
|
|
|
->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias) |
116
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
117
|
|
|
->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType) |
118
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
119
|
|
|
->defClassVar('$_relations', 'public static ', $metadata->arRelations) |
120
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
121
|
|
|
->defClassVar('$fieldIDs', 'protected static ', $metadata->allFieldIDs) |
122
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
123
|
|
|
->defClassVar('$fieldValueColumns', 'protected static ', $metadata->allFieldValueColumns); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
Let’s take a look at an example: