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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/2/11
* Time: 15:16
*/
namespace Meibuyu\Micro\Generator;
use Hyperf\Utils\CodeGen\Project;
use Hyperf\Utils\Filesystem\Filesystem;
use Hyperf\Utils\Str;
use Meibuyu\Micro\Generator\Stubs\Stub;
abstract class Generator
{
/**
* The filesystem instance.
*
* @var Filesystem
*/
protected $filesystem;
/**
* The array of options.
*
* @var array
*/
protected $options;
/**
* The shortname of stub.
*
* @var string
*/
protected $stub;
/**
* The class-specific output paths.
*
* @var string
*/
protected $classPath = '';
/**
* The file extension.
* @var string
*/
protected $extension = '';
protected $_name;
protected $_namespace;
/**
* Create new instance of this class.
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->filesystem = new Filesystem;
$this->options = $options;
$this->makeName();
$this->makeNamespace();
}
/**
* Make name.
*/
public function makeName()
{
$name = $this->name;
if (Str::contains($this->name, '\\')) {
$name = str_replace('\\', '/', $this->name);
}
if (Str::contains($this->name, '/')) {
$name = str_replace('/', '/', $this->name);
}
$this->_name = Str::studly(str_replace(' ', '/', ucwords(str_replace('/', ' ', $name))));
}
/**
* Make namespace
*/
public function makeNamespace()
{
$classNamespace = str_replace('/', '\\', $this->classPath);
$this->_namespace = 'App\\' . $classNamespace . '\\';
}
/**
* Get extension.
*
* @return string
*/
public function getExtension()
{
return $this->extension;
}
/**
* Run the generator.
*
* @return int
* @throws FileAlreadyExistsException
*/
public function run()
{
$path = $this->getPath();
if ($this->filesystem->exists($path)) {
throw new FileAlreadyExistsException($path);
}
if (!$this->filesystem->isDirectory($dir = dirname($path))) {
$this->filesystem->makeDirectory($dir, 0777, true, true);
}
return $this->filesystem->put($path, $this->getStub());
}
/**
* Get destination path for generated file.
*
* @return string
*/
public function getPath()
{
$project = new Project();
return BASE_PATH . '/' . $project->path($this->_namespace . $this->_name . $this->extension);
}
/**
* Get stub template for generated file.
*
* @return string
*/
public function getStub()
{
return (new Stub(__DIR__ . '/Stubs/' . $this->stub . '.stub', $this->getReplacements()))->render();
}
/**
* Get template replacements.
*
* @return array
*/
public function getReplacements()
{
return [
'date' => $this->getDate(),
'time' => $this->getTime(),
'class' => $this->getClass(),
'namespace' => $this->getNamespace(),
];
}
public function getDate()
{
return date('Y/m/d', time());
}
public function getTime()
{
return date('h:i', time());
}
/**
* Get class name.
*
* @return string
*/
public function getClass()
{
return Str::studly(class_basename($this->_name));
}
/**
* Get class namespace.
*
* @return string
*/
public function getNamespace()
{
$segments = explode('/', $this->_name);
array_pop($segments);
return 'namespace ' . rtrim($this->_namespace . implode($segments, '\\'), '\\') . ';';
}
/**
* Get value from options by given key.
*
* @param string $key
* @param string|null $default
*
* @return string
*/
public function getOption($key, $default = null)
{
if (!array_key_exists($key, $this->options)) {
return $default;
}
return $this->options[$key] ?: $default;
}
/**
* Helper method for "getOption".
*
* @param string $key
* @param string|null $default
*
* @return string
*/
public function option($key, $default = null)
{
return $this->getOption($key, $default);
}
/**
* Handle call to __get method.
*
* @param string $key
*
* @return string|mixed
*/
public function __get($key)
{
if (property_exists($this, $key)) {
return $this->{$key};
}
return $this->option($key);
}
}