1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rexlabs\Laravel\Smokescreen\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
|
7
|
|
|
class MakeTransformerCommand extends GeneratorCommand |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name and signature of the console command. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'make:transformer {model : The namespaced model to transform. e.g. "App\User"}'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Create a new transformer class'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The type of class being generated. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $type = 'Transformer'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the stub file for the generator. |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
protected function getStub() |
36
|
|
|
{ |
37
|
|
|
return __DIR__ . '/transformer.stub'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Execute the console command. |
42
|
|
|
* |
43
|
|
|
* @return bool|null |
44
|
|
|
*/ |
45
|
|
|
public function handle() |
46
|
|
|
{ |
47
|
|
|
if (!class_exists($name = $this->argument('model'))) { |
|
|
|
|
48
|
|
|
$confirmed = $this->confirm( |
49
|
|
|
'The model to transform does not exist, do you want to generate it?' |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
if ($confirmed) { |
53
|
|
|
$this->call('make:model', compact('name')); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
parent::handle(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the default namespace for the class. |
62
|
|
|
* |
63
|
|
|
* @param string $rootNamespace |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
protected function getDefaultNamespace($rootNamespace) |
67
|
|
|
{ |
68
|
|
|
return config('smokescreen.transformer_namespace'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Build the class with the given name. |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function buildClass($name) |
78
|
|
|
{ |
79
|
|
|
$stub = parent::buildClass($name); |
80
|
|
|
|
81
|
|
|
return $this->replaceModel($stub); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retrieve the given content replacing the model in it. |
86
|
|
|
* |
87
|
|
|
* @param string $content |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
protected function replaceModel(string $content) : string |
91
|
|
|
{ |
92
|
|
|
$from = [ |
93
|
|
|
'DummyModelName', |
94
|
|
|
'dummyModelName', |
95
|
|
|
'DummyModel', |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$to = [ |
99
|
|
|
$this->getModelName(), |
100
|
|
|
lcfirst($this->getModelName()), |
101
|
|
|
$this->argument('model'), |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
return str_replace($from, $to, $content); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the desired class name from the input. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function getNameInput() |
113
|
|
|
{ |
114
|
|
|
return $this->getModelName() . 'Transformer'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Retrieve the model name. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
protected function getModelName() : string |
123
|
|
|
{ |
124
|
|
|
return class_basename($this->argument('model')); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|