|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\generator\name; |
|
3
|
|
|
|
|
4
|
|
|
use keeko\framework\utils\NameUtils; |
|
5
|
|
|
use keeko\tools\model\Relationship; |
|
6
|
|
|
use phootwork\lang\Text; |
|
7
|
|
|
use Propel\Generator\Model\Table; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Generates action names |
|
11
|
|
|
* |
|
12
|
|
|
* @author gossi |
|
13
|
|
|
*/ |
|
14
|
|
|
class ActionNameGenerator extends AbstractModelNameGenerator { |
|
15
|
|
|
|
|
16
|
|
|
// |
|
17
|
|
|
// Model generators |
|
18
|
|
|
// |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Generates the name for a list action |
|
22
|
|
|
* |
|
23
|
|
|
* @param Table $model |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public function generateModelPaginate(Table $model) { |
|
27
|
|
|
return $model->getOriginCommonName() . '-paginate'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Generates the name for a create action |
|
32
|
|
|
* |
|
33
|
|
|
* @param Table $model |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function generateModelCreate(Table $model) { |
|
37
|
|
|
return $model->getOriginCommonName() . '-create'; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Generates the name for a read action |
|
42
|
|
|
* |
|
43
|
|
|
* @param Table $model |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function generateModelRead(Table $model) { |
|
47
|
|
|
return $model->getOriginCommonName() . '-read'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Generates the name for an update action |
|
52
|
|
|
* |
|
53
|
|
|
* @param Table $model |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function generateModelUpdate(Table $model) { |
|
57
|
|
|
return $model->getOriginCommonName() . '-update'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Generates the name for a delete action |
|
62
|
|
|
* |
|
63
|
|
|
* @param Table $model |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function generateModelDelete(Table $model) { |
|
67
|
|
|
return $model->getOriginCommonName() . '-delete'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// |
|
71
|
|
|
// Relationship generators |
|
72
|
|
|
// |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Generates the name for a relationship read action |
|
76
|
|
|
* |
|
77
|
|
|
* @param Relationship $relationship |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function generateRelationshipRead(Relationship $relationship) { |
|
81
|
|
|
return Text::create('{model}-to-{related}-relationship-read')->supplant([ |
|
82
|
|
|
'{model}' => $relationship->getModel()->getOriginCommonName(), |
|
83
|
|
|
'{related}' => NameUtils::toSnakeCase($relationship->getRelatedTypeName()) |
|
84
|
|
|
])->toString(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Generates the name for a relationship update action |
|
89
|
|
|
* |
|
90
|
|
|
* @param Relationship $relationship |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function generateRelationshipUpdate(Relationship $relationship) { |
|
94
|
|
|
return Text::create('{model}-to-{related}-relationship-update')->supplant([ |
|
95
|
|
|
'{model}' => $relationship->getModel()->getOriginCommonName(), |
|
96
|
|
|
'{related}' => NameUtils::toSnakeCase($relationship->getRelatedTypeName()) |
|
97
|
|
|
])->toString(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Generates the name for a relationship add action |
|
102
|
|
|
* |
|
103
|
|
|
* @param Relationship $relationship |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function generateRelationshipAdd(Relationship $relationship) { |
|
107
|
|
|
return Text::create('{model}-to-{related}-relationship-add')->supplant([ |
|
108
|
|
|
'{model}' => $relationship->getModel()->getOriginCommonName(), |
|
109
|
|
|
'{related}' => NameUtils::toSnakeCase($relationship->getRelatedTypeName()) |
|
110
|
|
|
])->toString(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Generates the name for a relationship remove action |
|
115
|
|
|
* |
|
116
|
|
|
* @param Relationship $relationship |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function generateRelationshipRemove(Relationship $relationship) { |
|
120
|
|
|
return Text::create('{model}-to-{related}-relationship-remove')->supplant([ |
|
121
|
|
|
'{model}' => $relationship->getModel()->getOriginCommonName(), |
|
122
|
|
|
'{related}' => NameUtils::toSnakeCase($relationship->getRelatedTypeName()) |
|
123
|
|
|
])->toString(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function parseName($actionName) { |
|
127
|
|
|
if (strpos($actionName, 'relationship') !== false) { |
|
128
|
|
|
return $this->parseRelationship($actionName); |
|
129
|
|
|
} else { |
|
130
|
|
|
return $this->parseModel($actionName); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function parseModel($actionName) { |
|
135
|
|
|
return [ |
|
136
|
|
|
'modelName' => substr($actionName, 0, strpos($actionName, '-')), |
|
137
|
|
|
'type' => substr($actionName, strrpos($actionName, '-') + 1) |
|
138
|
|
|
]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function parseRelationship($actionName) { |
|
142
|
|
|
$matches = []; |
|
143
|
|
|
preg_match('/([a-z_]+)-to-([a-z_]+)-relationship.*/i', $actionName, $matches); |
|
144
|
|
|
|
|
145
|
|
|
return [ |
|
146
|
|
|
'prefix' => substr($actionName, 0, strpos($actionName, 'relationship') + 12), |
|
147
|
|
|
'type' => substr($actionName, strrpos($actionName, '-') + 1), |
|
148
|
|
|
'modelName' => $matches[1], |
|
149
|
|
|
'relatedName' => $matches[2] |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
} |