-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.php
More file actions
213 lines (192 loc) · 5.43 KB
/
Generator.php
File metadata and controls
213 lines (192 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* Gii Generator for Backoffice Themes
*
* @author Putra Sudaryanto <putra@ommu.id>
* @contact (+62)856-299-4114
* @copyright Copyright (c) 2017 OMMU (www.ommu.id)
* @created date 7 September 2017, 08:23 WIB
* @link https://github.com/ommu/gii-template
*/
namespace ommu\gii;
use Yii;
use yii\helpers\VarDumper;
use yii\helpers\Inflector;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
class Generator extends \yii\gii\Generator
{
/**
* @return string name of the code generator
*/
public function getName()
{
//do something
}
/**
* Generates the code based on the current user input and the specified code template files.
* This is the main method that child classes should implement.
* Please refer to [[\yii\gii\generators\controller\Generator::generate()]] as an example
* on how to implement this method.
* @return CodeFile[] a list of code files to be created.
*/
public function generate()
{
//do something
}
/**
* Generates a string depending on enableI18N property
*
* @param string $string the text be generated
* @param array $placeholders the placeholders to use by `Yii::t()`
* @return string
*/
public function generateString($string = '', $placeholders = [])
{
$string = addslashes($string);
if ($this->enableI18N) {
$ph = self::VarDumper($placeholders);
$str = "Yii::t('" . $this->messageCategory . "', '" . $string . "'" . $ph . ")";
} else {
// No I18N, replace placeholders by real words, if any
if (!empty($placeholders)) {
$phKeys = array_map(function($word) {
return '{' . $word . '}';
}, array_keys($placeholders));
$phValues = array_values($placeholders);
$str = "'" . str_replace($phKeys, $phValues, $string) . "'";
} else {
// No placeholders, just the given string
$str = "'" . $string . "'";
}
}
return $str;
}
/**
* Generates a string depending on enableI18N property
*
* @param string $string the text be generated
* @param array $placeholders the placeholders to use by `Yii::t()`
* @return string
*/
public static function VarDumper($placeholders)
{
// If there are placeholders, use them
if (!empty($placeholders)) {
$count = count($placeholders);
$i=0;
$ph = ', [';
foreach($placeholders as $key => $val) {
$i++;
if($i == $count)
$ph .= preg_match('/^[$]/', $val) ? "'$key' => $val" : "'$key' => '$val'";
else
$ph .= preg_match('/^[$]/', $val) ? "'$key' => $val, " : "'$key' => '$val', ";
}
$ph .= ']';
} else {
$ph = '';
}
return $ph;
}
/**
* set name relation with underscore
*/
public function setRelation($names, $model=false)
{
if($model == true) {
$patterns = array();
$patterns[0] = '(_ommu)';
$patterns[1] = '(_core)';
$patterns[2] = '(_swt)';
$char=range("A","Z");
foreach($char as $val) {
if(strpos($names, $val) !== false) {
$names = str_replace($val, '_'.strtolower($val), $names);
}
}
$return = trim(preg_replace($patterns, '', $names), '_');
$return = array_map('ucfirst', explode('_', $return));
//print_r($return);
if(count($return) != 1)
return end($return);
else {
if(is_array($return))
return implode('', $return);
else
return $return;
}
} else {
$key = $names;
if (!empty($key) && strcasecmp($key, 'id')) {
if (substr_compare($key, 'id', -2, 2, true) === 0) {
$key = rtrim(substr($key, 0, -2), '_');
} elseif (substr_compare($key, 'id', 0, 2, true) === 0) {
$key = ltrim(substr($key, 2, strlen($key)), '_');
}
}
if(strtolower($key) == 'cat')
$key = 'category';
$key = Inflector::singularize(Inflector::id2camel($key, '_'));
return lcfirst($key);
}
}
public function getForeignKeys($tableForeignKeys)
{
$column = [];
if(!empty($tableForeignKeys)) {
foreach($tableForeignKeys as $val) {
// Only variables should be passed by reference
$arrKey = array_keys($val);
$arrVal = array_values($val);
$column[array_pop($arrKey)] = array_shift($arrVal);
}
}
return $column;
}
// Parse yaml dari file
// contoh: `
// name: susilo
// `
// $fileName -> demo.yaml
// @return php array/object
public function loadYaml($fileName)
{
$fname = join('/', [dirname(Yii::getAlias('@app')), $fileName]);
if(!file_exists($fname)) {
$errMsg = 'File "author.yaml" tidak dapat ditemukan, dimohon untuk membuat file ';
$errMsg .= '"author.yaml" pada folder ' . Yii::getAlias('@webroot');
throw new \Exception($errMsg);
}
try {
$data = Yaml::parse(file_get_contents($fname));
} catch(ParseException $e) {
echo $e->getMessage();
}
return $data;
}
/**
* @inheritdoc
*/
public function validateNewClass($attribute, $params) {
$class = ltrim($this->$attribute, '\\');
if(($pos = strrpos($class, '\\')) === false) {
$this->addError($attribute, "The class name must contain fully qualified namespace name.");
}else {
$ns = substr($class, 0, $pos);
$path = Yii::getAlias('@' . str_replace('\\', '/', $ns), false);
if ($path === false) {
$this->addError($attribute, "The class namespace is invalid: $ns");
}elseif (!is_dir($path)) {
@mkdir($path);
// $this->addError($attribute, "Please make sure the directory containing this class exists: $path");
}
}
}
public function setRelationFixed($relation, $tableColumns)
{
if(array_key_exists($relation, $tableColumns))
return join('', [$relation, 'Rltn']);
return $relation;
}
}