|
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\Field; |
|
10
|
|
|
use samsoncms\api\generator\metadata\VirtualMetadata; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Virtual entity class generator. |
|
14
|
|
|
* |
|
15
|
|
|
* @package samsoncms\api\generator |
|
16
|
|
|
*/ |
|
17
|
|
|
class VirtualEntity extends RealEntity |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Class uses generation part. |
|
21
|
|
|
* |
|
22
|
|
|
* @param RealMetadata $metadata Entity metadata |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function createUses($metadata) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->generator |
|
27
|
|
|
->newLine('use samsonframework\core\ViewInterface;') |
|
28
|
|
|
->newLine('use samsonframework\orm\QueryInterface;') |
|
29
|
|
|
->newLine(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class definition generation part. |
|
34
|
|
|
* |
|
35
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function createDefinition($metadata) |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* TODO: Parent problem |
|
41
|
|
|
* Should be changed to merging fields instead of extending with OOP for structure_relation support |
|
42
|
|
|
* or creating traits and using them on shared parent entities. |
|
43
|
|
|
*/ |
|
44
|
|
|
$parentClass = null !== $metadata->parent |
|
45
|
|
|
? $metadata->parent->entityClassName |
|
46
|
|
|
: '\\'.\samsoncms\api\Entity::class; |
|
47
|
|
|
|
|
48
|
|
|
$this->generator |
|
49
|
|
|
->multiComment(array('"' . $metadata->entityRealName . '" database entity class')) |
|
50
|
|
|
->defClass($this->className, $parentClass) |
|
51
|
|
|
->newLine('use TableTrait;') |
|
52
|
|
|
->newLine();; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Class constants generation part. |
|
57
|
|
|
* |
|
58
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function createConstants($metadata) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->generator |
|
63
|
|
|
->commentVar('string', 'Entity full class name, use ::class instead') |
|
64
|
|
|
->defClassConst('ENTITY', $metadata->entityClassName) |
|
65
|
|
|
->commentVar('string', 'Entity manager full class name') |
|
66
|
|
|
->defClassConst('MANAGER', $metadata->entityClassName . 'Query') |
|
67
|
|
|
->commentVar('string', 'Entity database identifier') |
|
68
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
|
69
|
|
|
->commentVar('string', 'Not transliterated entity name') |
|
70
|
|
|
->defClassConst('NAME', $metadata->entityRealName); |
|
71
|
|
|
|
|
72
|
|
|
// Create all entity fields constants storing each additional field metadata |
|
73
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
|
74
|
|
|
$this->generator |
|
75
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name') |
|
76
|
|
|
->defClassConst('F_' . $fieldName, $fieldName) |
|
77
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier') |
|
78
|
|
|
->defClassConst('F_' . $fieldName . '_ID', $fieldID); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Class fields generation part. |
|
84
|
|
|
* |
|
85
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
|
86
|
|
|
*/ |
|
87
|
|
View Code Duplication |
protected function createFields($metadata) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
|
90
|
|
|
$this->generator |
|
91
|
|
|
->commentVar($metadata->types[$fieldID], $metadata->fieldDescriptions[$fieldID]) |
|
92
|
|
|
->defClassVar('$' . $fieldName, 'public'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Class methods generation part. |
|
98
|
|
|
* |
|
99
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function createMethods($metadata) |
|
102
|
|
|
{ |
|
103
|
|
|
$methods = []; |
|
104
|
|
|
// Generate Query::where() analog for specific field. |
|
105
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
|
106
|
|
|
try { |
|
107
|
|
|
// We need only gallery fields |
|
108
|
|
|
if ($metadata->allFieldCmsTypes[$fieldID] === Field::TYPE_GALLERY) { |
|
109
|
|
|
$galleryName = rtrim($fieldName, 'Gallery') . 'Gallery'; |
|
110
|
|
|
|
|
111
|
|
|
$code = "\n\t" . '/**'; |
|
112
|
|
|
$code .= "\n\t" . ' * Get ' . $fieldName . '(#' . $fieldID . ') gallery collection instance.'; |
|
113
|
|
|
$code .= "\n\t" . ' * @param ViewInterface $renderer Render instance'; |
|
114
|
|
|
$code .= "\n\t" . ' *'; |
|
115
|
|
|
$code .= "\n\t" . ' * @return GalleryCollection Gallery collection instance'; |
|
116
|
|
|
$code .= "\n\t" . ' */'; |
|
117
|
|
|
$code .= "\n\t" . 'public function ' . lcfirst($galleryName) . '(ViewInterface $renderer)'; |
|
118
|
|
|
$code .= "\n\t" . '{'; |
|
119
|
|
|
$code .= "\n\t\t" . 'return (new GalleryCollection($renderer, $this->query))->materialID($this->id)->materialFieldID(' . $fieldID . ');'; |
|
120
|
|
|
$code .= "\n\t" . '}'; |
|
121
|
|
|
|
|
122
|
|
|
$methods[] = $code; |
|
123
|
|
|
} |
|
124
|
|
|
} catch (\Exception $e) { |
|
125
|
|
|
throw new \Exception($metadata->entity . ' cms field type for [' . $fieldName . '] not found'); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// Add method text to generator |
|
130
|
|
|
$this->generator->text(implode("\n", $methods)); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
Let’s take a look at an example: