1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* An Artisan command for generating a new transformer class. |
10
|
|
|
* |
11
|
|
|
* @package Laravel Responder |
12
|
|
|
* @author Alexander Tømmerås <[email protected]> |
13
|
|
|
* @license The MIT License |
14
|
|
|
*/ |
15
|
|
|
class MakeTransformer extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'make:transformer |
23
|
|
|
{name : The name of the transformer class} |
24
|
|
|
{--pivot : Include a transformer method for pivot table data} |
25
|
|
|
{--model= : The namespace to the model being transformed}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Create a new transformer class'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The file system instance. |
36
|
|
|
* |
37
|
|
|
* @var Filesystem |
38
|
|
|
*/ |
39
|
|
|
protected $files; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new command instance. |
43
|
|
|
* |
44
|
|
|
* @param Filesystem $files |
45
|
|
|
*/ |
46
|
|
|
public function __construct( Filesystem $files ) |
47
|
|
|
{ |
48
|
|
|
parent::__construct(); |
49
|
|
|
|
50
|
|
|
$this->files = $files; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Execute the console command. |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function handle() |
59
|
|
|
{ |
60
|
|
|
$this->generateTransformer(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Generate the transformer class. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
protected function generateTransformer() |
69
|
|
|
{ |
70
|
|
|
$name = (string) $this->argument( 'name' ); |
71
|
|
|
$path = $this->laravel->basePath() . '/app/Transformers/' . $name . '.php'; |
72
|
|
|
|
73
|
|
|
if ( $this->files->exists( $path ) ) { |
74
|
|
|
return $this->error( $name . ' already exists!' ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->makeDirectory( $path ); |
78
|
|
|
|
79
|
|
|
$stubPath = $this->option( 'pivot' ) ? 'resources/stubs/transformer.pivot.stub' : 'resources/stubs/transformer.stub'; |
80
|
|
|
$stub = $this->files->get( __DIR__ . '/../../' . $stubPath ); |
81
|
|
|
|
82
|
|
|
$this->files->put( $path, $this->makeTransformer( $name, $stub ) ); |
83
|
|
|
|
84
|
|
|
$this->info( 'Transformer created successfully.' ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Build a transformers directory if one doesn't exist. |
89
|
|
|
* |
90
|
|
|
* @param string $path |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
protected function makeDirectory( string $path ) |
94
|
|
|
{ |
95
|
|
|
if ( ! $this->files->isDirectory( dirname( $path ) ) ) { |
96
|
|
|
$this->files->makeDirectory( dirname( $path ), 0777, true, true ); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Build the transformer class using the given name and stub. |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
* @param string $stub |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function makeTransformer( string $name, string $stub ):string |
108
|
|
|
{ |
109
|
|
|
$stub = $this->replaceNamespace( $stub ); |
110
|
|
|
$stub = $this->replaceClass( $stub, $name ); |
111
|
|
|
$stub = $this->replaceModel( $stub, $name ); |
112
|
|
|
|
113
|
|
|
return $stub; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Replace the namespace for the given stub. |
118
|
|
|
* |
119
|
|
|
* @param string $stub |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
protected function replaceNamespace( string $stub ):string |
123
|
|
|
{ |
124
|
|
|
if ( method_exists( $this->laravel, 'getNameSpace' ) ) { |
125
|
|
|
$namespace = $this->laravel->getNamespace() . 'Transformers'; |
126
|
|
|
} else { |
127
|
|
|
$namespace = 'App\Transformers'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$stub = str_replace( 'DummyNamespace', $namespace, $stub ); |
131
|
|
|
|
132
|
|
|
return $stub; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Replace the class name for the given stub. |
137
|
|
|
* |
138
|
|
|
* @param string $stub |
139
|
|
|
* @param string $name |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
protected function replaceClass( string $stub, string $name ):string |
143
|
|
|
{ |
144
|
|
|
$stub = str_replace( 'DummyClass', $name, $stub ); |
145
|
|
|
|
146
|
|
|
return $stub; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Replace the model for the given stub. |
151
|
|
|
* |
152
|
|
|
* @param string $stub |
153
|
|
|
* @param string $name |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
protected function replaceModel( string $stub, string $name ):string |
157
|
|
|
{ |
158
|
|
|
$model = $this->getModelNamespace( $name ); |
159
|
|
|
$class = $this->getClassFromNamespace( $model ); |
160
|
|
|
|
161
|
|
|
$stub = str_replace( 'DummyModelNamespace', $model, $stub ); |
162
|
|
|
$stub = str_replace( 'DummyModelClass', $class, $stub ); |
163
|
|
|
$stub = str_replace( 'DummyModelVariable', camel_case( $class ), $stub ); |
164
|
|
|
|
165
|
|
|
return $stub; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the full class path for the model. |
170
|
|
|
* |
171
|
|
|
* @param string $name |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
protected function getModelNamespace( string $name ):string |
175
|
|
|
{ |
176
|
|
|
if ( $this->option( 'model' ) ) { |
177
|
|
|
return $this->option( 'model' ); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return 'App\\' . str_replace( 'Transformer', '', $name ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the full class path for the transformer. |
185
|
|
|
* |
186
|
|
|
* @param string $namespace |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
protected function getClassFromNamespace( string $namespace ):string |
190
|
|
|
{ |
191
|
|
|
return last( explode( '\\', $namespace ) ); |
192
|
|
|
} |
193
|
|
|
} |