|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\Console\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Console\ConfirmableTrait; |
|
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 ConfirmableTrait; |
|
16
|
|
|
use ConsoleMakeModuleCommand; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The console command name. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name = 'make:transformer'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The console command description. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $description = 'Create a new transformer class'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The type of class being generated. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $type = 'Transformer'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Build the class with the given name. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $name |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function buildClass($name): string |
|
47
|
|
|
{ |
|
48
|
|
|
$stub = parent::buildClass($name); |
|
49
|
|
|
|
|
50
|
|
|
$model = $this->option('model'); |
|
51
|
|
|
|
|
52
|
|
|
return $model ? $this->replaceModel($stub, $model) : $stub; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Replace the model for the given stub. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $stub |
|
59
|
|
|
* @param string $model |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function replaceModel($stub, $model): string |
|
64
|
|
|
{ |
|
65
|
|
|
$model = str_replace('/', '\\', $model); |
|
66
|
|
|
|
|
67
|
|
|
$namespaceModel = $this->rootNamespace().'\Models\\'.$model; |
|
68
|
|
|
|
|
69
|
|
|
if (starts_with($model, '\\')) { |
|
|
|
|
|
|
70
|
|
|
$stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub); |
|
71
|
|
|
} else { |
|
72
|
|
|
$stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$stub = str_replace( |
|
76
|
|
|
"use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
$model = class_basename(trim($model, '\\')); |
|
80
|
|
|
|
|
81
|
|
|
$stub = str_replace('DummyModel', $model, $stub); |
|
82
|
|
|
|
|
83
|
|
|
return str_replace('dummyModel', camel_case($model), $stub); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the stub file for the generator. |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getStub(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->option('model') |
|
94
|
|
|
? __DIR__.'/../../../resources/stubs/transformer.stub' |
|
95
|
|
|
: __DIR__.'/../../../resources/stubs/transformer.plain.stub'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the default namespace for the class. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $rootNamespace |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getDefaultNamespace($rootNamespace): string |
|
106
|
|
|
{ |
|
107
|
|
|
return $rootNamespace.'\Transformers'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the console command arguments. |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getArguments(): array |
|
116
|
|
|
{ |
|
117
|
|
|
return [ |
|
118
|
|
|
['name', InputArgument::REQUIRED, 'The name of the transformer.'], |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get the console command arguments. |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function getOptions(): array |
|
128
|
|
|
{ |
|
129
|
|
|
return [ |
|
130
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the transformer applies to.'], |
|
131
|
|
|
['module', 'd', InputOption::VALUE_REQUIRED, 'The module name to generate the file within.'], |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.