1
|
|
|
<?php |
2
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: nazarenko |
6
|
|
|
* Date: 29.03.2016 |
7
|
|
|
* Time: 11:11 |
8
|
|
|
*/ |
9
|
|
|
namespace samsoncms\api\generator; |
10
|
|
|
|
11
|
|
|
use samsoncms\api\generator\metadata\VirtualMetadata; |
12
|
|
|
use samsonphp\generator\Generator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Table class generator. |
16
|
|
|
* |
17
|
|
|
* @package samsoncms\api\generator |
18
|
|
|
* @deprecated |
19
|
|
|
*/ |
20
|
|
|
class Table extends Generic |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Query constructor. |
24
|
|
|
* |
25
|
|
|
* @param Generator $generator |
26
|
|
|
* @param $metadata |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Generator $generator, $metadata) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($generator, $metadata); |
31
|
|
|
|
32
|
|
|
$this->className = preg_replace('/Table$/i', '', $this->className) . 'Table'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class uses generation part. |
37
|
|
|
* |
38
|
|
|
* @param \samsoncms\api\generator\metadata\GalleryMetadata $metadata Entity metadata |
39
|
|
|
*/ |
40
|
|
|
protected function createUses($metadata) |
41
|
|
|
{ |
42
|
|
|
$this->generator |
43
|
|
|
->newLine('use samsonframework\core\ViewInterface;') |
44
|
|
|
->newLine('use samsonframework\orm\QueryInterface;') |
45
|
|
|
->newLine('use samson\activerecord\dbQuery;') |
46
|
|
|
->newLine(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Class definition generation part. |
51
|
|
|
* |
52
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
protected function createDefinition($metadata) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->generator |
57
|
|
|
->multiComment(array( |
58
|
|
|
'Class for rendering "' . $metadata->entityRealName . '" table', |
59
|
|
|
'@deprecated Use ' . $this->className . 'Collection instead' |
60
|
|
|
)) |
61
|
|
|
->defClass($this->className, '\\'.\samsoncms\api\field\Table::class) |
62
|
|
|
->newLine('use \\'.\samsoncms\api\Renderable::class.';') |
63
|
|
|
->newLine(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Class constants generation part. |
68
|
|
|
* |
69
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
70
|
|
|
*/ |
71
|
|
|
protected function createConstants($metadata) |
72
|
|
|
{ |
73
|
|
|
$this->generator |
74
|
|
|
->commentVar('string', 'Entity full class name, use ::class instead') |
75
|
|
|
->defClassConst('ENTITY', $metadata->entityClassName) |
76
|
|
|
->commentVar('string', 'Entity database identifier') |
77
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
78
|
|
|
->commentVar('string', 'Not transliterated entity name') |
79
|
|
|
->defClassConst('NAME', $metadata->entityRealName); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Class static fields generation part. |
84
|
|
|
* |
85
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
86
|
|
|
*/ |
87
|
|
|
protected function createStaticFields($metadata) |
88
|
|
|
{ |
89
|
|
|
$this->generator |
90
|
|
|
->commentVar('array', 'Collection of real additional field names') |
91
|
|
|
->defClassVar('$fieldsRealNames', 'public static', $metadata->realNames); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Class methods generation part. |
96
|
|
|
* |
97
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
98
|
|
|
*/ |
99
|
|
|
protected function createMethods($metadata) |
100
|
|
|
{ |
101
|
|
|
$methods = []; |
102
|
|
|
// TODO: Add different method generation depending on their field type |
103
|
|
|
// Generate Query::where() analog for specific field. |
104
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
105
|
|
|
$code = "\n\t" . '/**'; |
106
|
|
|
$code .= "\n\t" . ' * Get collection of ' . $fieldName . '(#' . $fieldID . ') table column values.'; |
107
|
|
|
$code .= "\n\t" . ' * @see \samsoncms\api\field\Table::values($fieldID)'; |
108
|
|
|
$code .= "\n\t" . ' * @param string $relation Field to value condition relation'; |
109
|
|
|
$code .= "\n\t" . ' *'; |
110
|
|
|
$code .= "\n\t" . ' * @return ' . $metadata->types[$fieldID] . '[] ' . $fieldName . ' values collection'; |
111
|
|
|
$code .= "\n\t" . ' */'; |
112
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '()'; |
113
|
|
|
$code .= "\n\t" . '{'; |
114
|
|
|
$code .= "\n\t\t" . 'return $this->values(' . $fieldID . ');'; |
115
|
|
|
$code .= "\n\t" . '}'; |
116
|
|
|
|
117
|
|
|
$methods[] = $code; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Add method text to generator |
121
|
|
|
$this->generator->text(implode("\n", $methods)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Class constructor generation part. |
126
|
|
|
* |
127
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
128
|
|
|
*/ |
129
|
|
|
protected function createConstructor($metadata) |
130
|
|
|
{ |
131
|
|
|
$class = "\n\t" . '/**'; |
132
|
|
|
$class .= "\n\t" . ' * @param ViewInterface $renderer Rendering instance'; |
133
|
|
|
$class .= "\n\t" . ' * @param int $materialID material identifier'; |
134
|
|
|
$class .= "\n\t" . ' * @param QueryInterface $query Database query instance'; |
135
|
|
|
$class .= "\n\t" . ' * @param string $locale locale'; |
136
|
|
|
$class .= "\n\t" . ' * @deprecated Use ' . $this->className . 'Collection instead'; |
137
|
|
|
$class .= "\n\t" . ' */'; |
138
|
|
|
$class .= "\n\t" . 'public function __construct(ViewInterface $renderer, $materialID, QueryInterface $query = null, $locale = null)'; |
139
|
|
|
$class .= "\n\t" . '{'; |
140
|
|
|
$class .= "\n\t\t" . '$this->renderer = $renderer;'; |
141
|
|
|
$class .= "\n\t\t" . 'parent::__construct(null !== $query ? $query : new dbQuery(), array('. $metadata->entityID .'), $materialID, $locale);'; |
142
|
|
|
$class .= "\n\t" . '}' . "\n"; |
143
|
|
|
|
144
|
|
|
$this->generator->text($class); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
148
|
|
|
|
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.