|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\Console\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Illuminate\Console\GeneratorCommand; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Cortex\Foundation\Traits\ConsoleMakeModuleCommand; |
|
12
|
|
|
|
|
13
|
|
|
class TransformerMakeCommand extends GeneratorCommand |
|
14
|
|
|
{ |
|
15
|
|
|
use ConsoleMakeModuleCommand; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The console command name. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $name = 'make:transformer'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The console command description. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $description = 'Create a new transformer class'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The type of class being generated. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $type = 'Transformer'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Build the class with the given name. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function buildClass($name) |
|
45
|
|
|
{ |
|
46
|
|
|
$stub = parent::buildClass($name); |
|
47
|
|
|
|
|
48
|
|
|
$model = $this->option('model'); |
|
49
|
|
|
|
|
50
|
|
|
return $model ? $this->replaceModel($stub, $model) : $stub; |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Replace the model for the given stub. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $stub |
|
57
|
|
|
* @param string $model |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function replaceModel($stub, $model) |
|
61
|
|
|
{ |
|
62
|
|
|
$model = str_replace('/', '\\', $model); |
|
63
|
|
|
|
|
64
|
|
|
$namespaceModel = $this->rootNamespace().'\Models\\'.$model; |
|
65
|
|
|
|
|
66
|
|
|
if (Str::startsWith($model, '\\')) { |
|
67
|
|
|
$stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub); |
|
68
|
|
|
} else { |
|
69
|
|
|
$stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$stub = str_replace( |
|
73
|
|
|
"use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$model = class_basename(trim($model, '\\')); |
|
77
|
|
|
|
|
78
|
|
|
$stub = str_replace('DummyModel', $model, $stub); |
|
79
|
|
|
|
|
80
|
|
|
return str_replace('dummyModel', Str::camel($model), $stub); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the stub file for the generator. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getStub() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->option('model') |
|
91
|
|
|
? __DIR__.'/../../../resources/stubs/transformer.stub' |
|
92
|
|
|
: __DIR__.'/../../../resources/stubs/transformer.plain.stub'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the default namespace for the class. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $rootNamespace |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function getDefaultNamespace($rootNamespace) |
|
102
|
|
|
{ |
|
103
|
|
|
return $rootNamespace.'\Transformers'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get the console command arguments. |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
|
|
|
|
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getArguments() |
|
112
|
|
|
{ |
|
113
|
|
|
return [ |
|
114
|
|
|
['name', InputArgument::REQUIRED, 'The name of the transformer.'], |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the console command arguments. |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
|
|
|
|
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getOptions() |
|
124
|
|
|
{ |
|
125
|
|
|
return [ |
|
126
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the transformer applies to.'], |
|
127
|
|
|
['module', 'd', InputOption::VALUE_REQUIRED, 'The module name to generate the file within.'], |
|
128
|
|
|
]; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.