1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rexlabs\Laravel\Smokescreen\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Contracts\View\Factory; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
class MakeTransformerCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'make:transformer {model : The namespaced model to transform. e.g. "App\User"}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Create a new transformer class'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The type of class being generated. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $type = 'Transformer'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The view factory. |
35
|
|
|
* |
36
|
|
|
* @var \Illuminate\Contracts\View\Factory |
37
|
|
|
*/ |
38
|
|
|
protected $factory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The includes listing. |
42
|
|
|
* |
43
|
|
|
* @var \Rexlabs\Laravel\Smokescreen\Console\IncludesListing |
44
|
|
|
*/ |
45
|
|
|
protected $includesListing; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The properties listing. |
49
|
|
|
* |
50
|
|
|
* @var \Rexlabs\Laravel\Smokescreen\Console\PropertiesListing |
51
|
|
|
*/ |
52
|
|
|
protected $propertiesListing; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the dependencies. |
56
|
|
|
* |
57
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
58
|
|
|
* @param \Illuminate\Contracts\View\Factory $factory |
59
|
|
|
* @param \Rexlabs\Laravel\Smokescreen\Console\IncludesListing $includesListing |
60
|
|
|
* @param \Rexlabs\Laravel\Smokescreen\Console\PropertiesListing $propertiesListing |
61
|
|
|
*/ |
62
|
|
|
public function __construct( |
63
|
|
|
Filesystem $files, |
64
|
|
|
Factory $factory, |
65
|
|
|
IncludesListing $includesListing, |
66
|
|
|
PropertiesListing $propertiesListing |
67
|
|
|
) { |
68
|
|
|
parent::__construct($files); |
69
|
|
|
|
70
|
|
|
$this->factory = $factory; |
71
|
|
|
$this->includesListing = $includesListing; |
72
|
|
|
$this->propertiesListing = $propertiesListing; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the stub file for the generator. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
protected function getStub() |
81
|
|
|
{ |
82
|
|
|
return 'smokescreen::transformer'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Execute the console command. |
87
|
|
|
* |
88
|
|
|
* @return bool|null |
89
|
|
|
*/ |
90
|
|
|
public function handle() |
91
|
|
|
{ |
92
|
|
|
$this->alertIfModelIsMissing(); |
93
|
|
|
|
94
|
|
|
parent::handle(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Display an error if the specified model does not exist. |
99
|
|
|
* |
100
|
|
|
*/ |
101
|
|
|
protected function alertIfModelIsMissing() |
102
|
|
|
{ |
103
|
|
|
if (!class_exists($name = $this->argument('model'))) { |
|
|
|
|
104
|
|
|
exit($this->error("The model [$name] does not exist, please create it first.")); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the default namespace for the class. |
110
|
|
|
* |
111
|
|
|
* @param string $rootNamespace |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
protected function getDefaultNamespace($rootNamespace) |
115
|
|
|
{ |
116
|
|
|
return config('smokescreen.transformer_namespace'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Build the class with the given name. |
121
|
|
|
* |
122
|
|
|
* @param string $name |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
protected function buildClass($name) |
126
|
|
|
{ |
127
|
|
|
$model = $this->argument('model'); |
128
|
|
|
|
129
|
|
|
$data = [ |
130
|
|
|
'namespace' => $this->getNamespace($name), |
131
|
|
|
'modelName' => $this->getModelName(), |
132
|
|
|
'transformerName' => $this->getNameInput(), |
133
|
|
|
'includes' => $this->includesListing->listForEloquent($model), |
|
|
|
|
134
|
|
|
'properties' => $this->propertiesListing->listForEloquent($model), |
|
|
|
|
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
return $this->factory->make($this->getStub(), $data)->render(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the desired class name from the input. |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
protected function getNameInput() |
146
|
|
|
{ |
147
|
|
|
return $this->getModelName() . 'Transformer'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Retrieve the model name. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
protected function getModelName() : string |
156
|
|
|
{ |
157
|
|
|
return class_basename($this->argument('model')); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|