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\Entity; |
10
|
|
|
use samsoncms\api\Field; |
11
|
|
|
use samsoncms\api\generator\metadata\VirtualMetadata; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Virtual entity class generator. |
15
|
|
|
* |
16
|
|
|
* @package samsoncms\api\generator |
17
|
|
|
*/ |
18
|
|
|
class VirtualEntity extends RealEntity |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Class uses generation part. |
22
|
|
|
* |
23
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
24
|
|
|
*/ |
25
|
|
|
protected function createUses($metadata) |
26
|
|
|
{ |
27
|
|
|
$this->generator |
28
|
|
|
->newLine('use samsonframework\core\ViewInterface;') |
29
|
|
|
->newLine('use samsonframework\orm\QueryInterface;') |
30
|
|
|
->newLine(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class definition generation part. |
35
|
|
|
* |
36
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
37
|
|
|
*/ |
38
|
|
|
protected function createDefinition($metadata) |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* TODO: Parent problem |
42
|
|
|
* Should be changed to merging fields instead of extending with OOP for structure_relation support |
43
|
|
|
* or creating traits and using them on shared parent entities. |
44
|
|
|
*/ |
45
|
|
|
$parentClass = null !== $metadata->parent |
46
|
|
|
? $metadata->parent->entityClassName |
47
|
|
|
: '\\'.\samsoncms\api\Entity::class; |
48
|
|
|
|
49
|
|
|
$this->generator |
50
|
|
|
->multiComment(array('"' . $metadata->entityRealName . '" database entity class')) |
51
|
|
|
->defClass($this->className, $parentClass) |
52
|
|
|
->newLine('use \samsoncms\api\generated\TableTrait;') |
53
|
|
|
->newLine(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Class constants generation part. |
58
|
|
|
* |
59
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
60
|
|
|
*/ |
61
|
|
|
protected function createConstants($metadata) |
62
|
|
|
{ |
63
|
|
|
$this->generator |
64
|
|
|
->commentVar('string', 'Entity full class name, use ::class instead') |
65
|
|
|
->defClassConst('ENTITY', $metadata->entityClassName) |
66
|
|
|
->commentVar('string', 'Entity manager full class name') |
67
|
|
|
->defClassConst('MANAGER', $metadata->entityClassName . 'Query') |
68
|
|
|
->commentVar('string', 'Entity database identifier') |
69
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
70
|
|
|
->commentVar('string', 'Not transliterated entity name') |
71
|
|
|
->defClassConst('NAME', $metadata->entityRealName); |
72
|
|
|
|
73
|
|
|
// Create all entity fields constants storing each additional field metadata |
74
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
75
|
|
|
$this->generator |
76
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name') |
77
|
|
|
->defClassConst('F_' . $fieldName, $fieldName) |
78
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier') |
79
|
|
|
->defClassConst('F_' . $fieldName . '_ID', $fieldID); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Class static fields generation part. |
85
|
|
|
* |
86
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
protected function createStaticFields($metadata) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$this->generator |
91
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
92
|
|
|
->defClassVar('$_sql_select', 'public static ', $metadata->arSelect) |
|
|
|
|
93
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
94
|
|
|
->defClassVar('$_attributes', 'public static ', $metadata->arAttributes) |
|
|
|
|
95
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
96
|
|
|
->defClassVar('$_map', 'public static ', $metadata->arMap) |
|
|
|
|
97
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
98
|
|
|
->defClassVar('$_sql_from', 'public static ', $metadata->arFrom) |
|
|
|
|
99
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
100
|
|
|
->defClassVar('$_own_group', 'public static ', $metadata->arGroup) |
|
|
|
|
101
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
102
|
|
|
->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias) |
|
|
|
|
103
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
104
|
|
|
->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType) |
|
|
|
|
105
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
106
|
|
|
->defClassVar('$_relations', 'public static ', $metadata->arRelations) |
|
|
|
|
107
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
108
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
109
|
|
|
->defClassVar('$fieldIDs', 'public static', $metadata->fields) |
110
|
|
|
->commentVar('array', 'Collection of additional fields value column names') |
111
|
|
|
->defClassVar('$fieldValueColumns', 'public static', $metadata->allFieldValueColumns) |
112
|
|
|
; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Class fields generation part. |
117
|
|
|
* |
118
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
protected function createFields($metadata) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
123
|
|
|
$this->generator |
124
|
|
|
->commentVar($metadata->types[$fieldID], $metadata->fieldDescriptions[$fieldID]) |
125
|
|
|
->defClassVar('$' . $fieldName, 'public'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Class methods generation part. |
131
|
|
|
* |
132
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
133
|
|
|
*/ |
134
|
|
|
protected function createMethods($metadata) |
135
|
|
|
{ |
136
|
|
|
$methods = []; |
137
|
|
|
// Generate Query::where() analog for specific field. |
138
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
139
|
|
|
try { |
140
|
|
|
// We need only gallery fields |
141
|
|
|
if ($metadata->allFieldCmsTypes[$fieldID] === Field::TYPE_GALLERY) { |
142
|
|
|
$galleryName = preg_replace('/Gallery$/i', '', $fieldName) . 'Gallery'; |
143
|
|
|
|
144
|
|
|
$code = "\n\t" . '/**'; |
145
|
|
|
$code .= "\n\t" . ' * Get ' . $fieldName . '(#' . $fieldID . ') gallery collection instance.'; |
146
|
|
|
$code .= "\n\t" . ' * @param ViewInterface $renderer Render instance'; |
147
|
|
|
$code .= "\n\t" . ' *'; |
148
|
|
|
$code .= "\n\t" . ' * @return GalleryCollection Gallery collection instance'; |
149
|
|
|
$code .= "\n\t" . ' */'; |
150
|
|
|
$code .= "\n\t" . 'public function create' . ucfirst($galleryName) . '(ViewInterface $renderer)'; |
151
|
|
|
$code .= "\n\t" . '{'; |
152
|
|
|
$code .= "\n\t\t" . '$gallery = (new GalleryCollection($renderer, $this->query))->materialID($this->id);'; |
153
|
|
|
$code .= "\n\t\t" . 'if(null !== ($materialFieldID = (new MaterialFieldQuery($this->query))->materialID($this->id)->fieldID('.$fieldID.')->first())) {'; |
154
|
|
|
$code .= "\n\t\t\t" . '$gallery->materialFieldID($materialFieldID->id);'; |
155
|
|
|
$code .= "\n\t\t" . '}'; |
156
|
|
|
$code .= "\n\t\t" . 'return $gallery;'; |
157
|
|
|
$code .= "\n\t" . '}'; |
158
|
|
|
|
159
|
|
|
$methods[] = $code; |
160
|
|
|
} |
161
|
|
|
} catch (\Exception $e) { |
162
|
|
|
throw new \Exception($metadata->entity . ' cms field type for [' . $fieldName . '] not found'); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Add method text to generator |
167
|
|
|
$this->generator->text(implode("\n", $methods)); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
171
|
|
|
|
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.