1
|
|
|
<?php |
2
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
3
|
|
|
/** |
4
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
5
|
|
|
* on 22.03.16 at 17:50 |
6
|
|
|
*/ |
7
|
|
|
namespace samsoncms\api\generator; |
8
|
|
|
|
9
|
|
|
use samsoncms\api\generator\metadata\RealMetadata; |
10
|
|
|
use samsoncms\api\Record; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Real database instances class generator. |
14
|
|
|
* |
15
|
|
|
* @package samsoncms\api\generator |
16
|
|
|
*/ |
17
|
|
|
class RealEntity extends Generic |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Class definition generation part. |
21
|
|
|
* |
22
|
|
|
* @param \samsoncms\api\generator\metadata\GenericMetadata $metadata Entity metadata |
23
|
|
|
*/ |
24
|
|
|
protected function createDefinition($metadata) |
25
|
|
|
{ |
26
|
|
|
$this->generator |
27
|
|
|
->multiComment(array('"' . $metadata->entity . '" database entity class')) |
28
|
|
|
->defClass($this->className, '\\' . \samsonframework\orm\Record::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class constants generation part. |
33
|
|
|
* |
34
|
|
|
* @param RealMetadata $metadata Entity metadata |
35
|
|
|
*/ |
36
|
|
|
protected function createConstants($metadata) |
37
|
|
|
{ |
38
|
|
|
$this->generator |
39
|
|
|
->commentVar('string', 'Entity full class name, use ::class instead') |
40
|
|
|
->defClassConst('ENTITY', $metadata->entityClassName) |
41
|
|
|
->commentVar('string', 'Entity manager full class name') |
42
|
|
|
->defClassConst('MANAGER', $metadata->entityClassName . 'Query') |
43
|
|
|
// FIXME: Only related to cms api tables |
44
|
|
|
->commentVar('string', 'Primary field name') |
45
|
|
|
->defClassConst('F_PRIMARY', $metadata->primaryField) |
46
|
|
|
->commentVar('string', 'Deletion field name') |
47
|
|
|
->defClassConst('F_DELETION', 'Active') |
48
|
|
|
; |
49
|
|
|
|
50
|
|
|
// Create all entity fields constants storing each additional field metadata |
51
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
52
|
|
|
$this->generator |
53
|
|
|
->commentVar('string', $fieldName . ' entity field name') |
54
|
|
|
->defClassConst('F_' . $fieldName, $fieldName); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Class static fields generation part. |
60
|
|
|
* |
61
|
|
|
* @param RealMetadata $metadata Entity metadata |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
protected function createStaticFields($metadata) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->generator |
66
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
67
|
|
|
->defClassVar('$_sql_select', 'public static ', $metadata->arSelect) |
|
|
|
|
68
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
69
|
|
|
->defClassVar('$_attributes', 'public static ', $metadata->arAttributes) |
|
|
|
|
70
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
71
|
|
|
->defClassVar('$_types', 'public static ', $metadata->arTypes) |
|
|
|
|
72
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
73
|
|
|
->defClassVar('$_table_attributes', 'public static ', $metadata->arTableAttributes) |
|
|
|
|
74
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
75
|
|
|
->defClassVar('$_map', 'public static ', $metadata->arMap) |
|
|
|
|
76
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
77
|
|
|
->defClassVar('$_sql_from', 'public static ', $metadata->arFrom) |
|
|
|
|
78
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
79
|
|
|
->defClassVar('$_own_group', 'public static ', $metadata->arGroup) |
|
|
|
|
80
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
81
|
|
|
->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias) |
|
|
|
|
82
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
83
|
|
|
->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType) |
|
|
|
|
84
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
85
|
|
|
->defClassVar('$_relations', 'public static ', $metadata->arRelations) |
|
|
|
|
86
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
87
|
|
|
->defClassVar('$fieldIDs', 'public static ', $metadata->fields); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Class fields generation part. |
92
|
|
|
* |
93
|
|
|
* @param RealMetadata $metadata Entity metadata |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
protected function createFields($metadata) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
98
|
|
|
$this->generator |
99
|
|
|
->commentVar($metadata->types[$fieldID], $fieldName . ' entity field') |
100
|
|
|
->defClassVar('$' . $fieldName, 'public'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.