Repository navigation
Expand file tree
/
Copy pathGeneratorCommand.php
More file actions
299 lines (253 loc) · 8.5 KB
/
Copy pathGeneratorCommand.php
File metadata and controls
299 lines (253 loc) · 8.5 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Devtool\Generator;
use Hyperf\CodeParser\Project;
use Hyperf\Collection\Arr;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Stringable\Str;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
abstract class GeneratorCommand extends Command
{
protected ?InputInterface $input = null;
protected ?OutputInterface $output = null;
public function configure()
{
foreach ($this->getArguments() as $argument) {
$this->addArgument(...$argument);
}
foreach ($this->getOptions() as $option) {
$this->addOption(...$option);
}
}
/**
* Execute the console command.
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$this->input = $input;
$this->output = $output;
$name = $this->qualifyClass($this->getNameInput());
$path = $this->getPath($name);
// First we will check to see if the class already exists. If it does, we don't want
// to create the class and overwrite the user's code. So, we will bail out so the
// code is untouched. Otherwise, we will continue generating this class' files.
if (($input->getOption('force') === false) && $this->alreadyExists($this->getNameInput())) {
$output->writeln(sprintf('<fg=red>%s</>', $name . ' already exists!'));
return 0;
}
// Next, we will generate the path to the location where this class' file should get
// written. Then, we will build the class and make the proper replacements on the
// stub files so that it gets the correctly formatted namespace and class name.
$this->makeDirectory($path);
file_put_contents($path, $this->buildClass($name));
$output->writeln(sprintf('<info>%s</info>', $name . ' created successfully.'));
$this->openWithIde($path);
return 0;
}
/**
* Parse the class name and format according to the root namespace.
*/
protected function qualifyClass(string $name): string
{
$name = ltrim($name, '\/');
$name = str_replace('/', '\\', $name);
$namespace = $this->input->getOption('namespace');
if (empty($namespace)) {
$namespace = $this->getDefaultNamespace();
}
return $namespace . '\\' . $name;
}
/**
* Determine if the class already exists.
*/
protected function alreadyExists(string $rawName): bool
{
return is_file($this->getPath($this->qualifyClass($rawName)));
}
/**
* Get the destination class path.
*/
protected function getPath(string $name): string
{
if ($path = $this->input->getOption('path')) {
$className = Str::afterLast($name, '\\');
if (str_starts_with($path, '/')) {
return rtrim($path, '/') . '/' . $className . '.php';
}
return BASE_PATH . '/' . trim($path, '/') . '/' . $className . '.php';
}
$project = new Project();
return BASE_PATH . '/' . $project->path($name);
}
/**
* Build the directory for the class if necessary.
*/
protected function makeDirectory(string $path): string
{
if (! is_dir(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
return $path;
}
/**
* Build the class with the given name.
*/
protected function buildClass(string $name): string
{
$stub = file_get_contents($this->getStub());
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
}
/**
* Replace the namespace for the given stub.
*/
protected function replaceNamespace(string &$stub, string $name): static
{
$stub = str_replace(
['%NAMESPACE%'],
[$this->getNamespace($name)],
$stub
);
return $this;
}
/**
* Get the full namespace for a given class, without the class name.
*/
protected function getNamespace(string $name): string
{
return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
}
/**
* Replace the class name for the given stub.
*/
protected function replaceClass(string $stub, string $name): string
{
$class = str_replace($this->getNamespace($name) . '\\', '', $name);
return str_replace('%CLASS%', $class, $stub);
}
/**
* Get the desired class name from the input.
*/
protected function getNameInput(): string
{
return trim($this->input->getArgument('name'));
}
/**
* Get the console command arguments.
*/
protected function getArguments(): array
{
return [
['name', InputArgument::REQUIRED, 'The name of the class'],
];
}
/**
* Get the console command options.
*/
protected function getOptions(): array
{
return [
['force', 'f', InputOption::VALUE_NONE, 'Whether force to rewrite.'],
['namespace', 'N', InputOption::VALUE_OPTIONAL, 'The namespace for class.', null],
['path', null, InputOption::VALUE_OPTIONAL, 'The location where the file should be created.', null],
];
}
/**
* Get the custom config for generator.
*/
protected function getConfig(): array
{
$class = Arr::last(explode('\\', static::class));
$class = Str::replaceLast('Command', '', $class);
$key = 'devtool.generator.' . Str::snake($class, '.');
return $this->getContainer()->get(ConfigInterface::class)->get($key) ?? [];
}
protected function getContainer(): ContainerInterface
{
return ApplicationContext::getContainer();
}
/**
* Get the stub file for the generator.
*/
abstract protected function getStub(): string;
/**
* Get the default namespace for the class.
*/
abstract protected function getDefaultNamespace(): string;
/**
* Get the editor file opener URL by its name.
*/
protected function getEditorUrl(string $ide): string
{
switch ($ide) {
case 'sublime':
return 'subl://open?url=file://%s';
case 'textmate':
return 'txmt://open?url=file://%s';
case 'cursor':
return 'cursor://file/%s';
case 'emacs':
return 'emacs://open?url=file://%s';
case 'macvim':
return 'mvim://open/?url=file://%s';
case 'phpstorm':
return 'phpstorm://open?file=%s';
case 'idea':
return 'idea://open?file=%s';
case 'vscode':
return 'vscode://file/%s';
case 'vscode-insiders':
return 'vscode-insiders://file/%s';
case 'vscode-remote':
return 'vscode://vscode-remote/%s';
case 'vscode-insiders-remote':
return 'vscode-insiders://vscode-remote/%s';
case 'atom':
return 'atom://core/open/file?filename=%s';
case 'nova':
return 'nova://core/open/file?filename=%s';
case 'netbeans':
return 'netbeans://open/?f=%s';
case 'xdebug':
return 'xdebug://%s';
default:
return '';
}
}
/**
* Open resulted file path with the configured IDE.
*/
protected function openWithIde(string $path): void
{
$ide = (string) $this->getContainer()->get(ConfigInterface::class)->get('devtool.ide');
$openEditorUrl = $this->getEditorUrl($ide);
if (! $openEditorUrl) {
return;
}
$url = sprintf($openEditorUrl, $path);
switch (PHP_OS_FAMILY) {
case 'Windows':
exec('explorer ' . $url);
break;
case 'Linux':
exec('xdg-open ' . $url);
break;
case 'Darwin':
exec('open ' . $url);
break;
}
}
}