1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An Artisan command class responsible for making transformer classes. |
12
|
|
|
* |
13
|
|
|
* @package flugger/laravel-responder |
14
|
|
|
* @author Alexander Tømmerås <[email protected]> |
15
|
|
|
* @license The MIT License |
16
|
|
|
*/ |
17
|
|
|
class MakeTransformer extends GeneratorCommand |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The console command name. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $name = 'make:transformer'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Create a new transformer class'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The type of class being generated. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $type = 'Transformer'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the stub file for the generator. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
protected function getStub() |
46
|
|
|
{ |
47
|
|
|
if ($this->option('plain')) { |
48
|
|
|
return __DIR__ . '/../../resources/stubs/transformer.plain.stub'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return __DIR__ . '/../../resources/stubs/transformer.model.stub'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the default namespace for the class. |
56
|
|
|
* |
57
|
|
|
* @param string $rootNamespace |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
protected function getDefaultNamespace($rootNamespace) |
61
|
|
|
{ |
62
|
|
|
return $rootNamespace . '\Transformers'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Build the class with the given name. |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function buildClass($name) |
72
|
|
|
{ |
73
|
|
|
$replace = []; |
74
|
|
|
|
75
|
|
|
if (! $this->option('model') && ! $this->option('plain')) { |
76
|
|
|
$this->input->setOption('model', $this->resolveModelFromClassName()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($this->option('model')) { |
80
|
|
|
$replace = $this->buildModelReplacements($replace); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return str_replace(array_keys($replace), array_values($replace), parent::buildClass($name)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Resolve a model from the given class name. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
protected function resolveModelFromClassName() |
92
|
|
|
{ |
93
|
|
|
return 'App\\' . str_replace('Transformer', '', Arr::last(explode('/', $this->getNameInput()))); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Build the model replacement values. |
98
|
|
|
* |
99
|
|
|
* @param array $replace |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function buildModelReplacements(array $replace) |
103
|
|
|
{ |
104
|
|
|
if (! class_exists($modelClass = $this->parseModel($this->option('model')))) { |
105
|
|
|
if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) { |
106
|
|
|
$this->call('make:model', ['name' => $modelClass]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return array_merge($replace, [ |
111
|
|
|
'DummyFullModelClass' => $modelClass, |
112
|
|
|
'DummyModelClass' => class_basename($modelClass), |
113
|
|
|
'DummyModelVariable' => lcfirst(class_basename($modelClass)), |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the fully-qualified model class name. |
119
|
|
|
* |
120
|
|
|
* @param string $model |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
protected function parseModel($model) |
124
|
|
|
{ |
125
|
|
|
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { |
126
|
|
|
throw new InvalidArgumentException('Model name contains invalid characters.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$model = trim(str_replace('/', '\\', $model), '\\'); |
130
|
|
|
|
131
|
|
|
if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) { |
132
|
|
|
$model = $rootNamespace . $model; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $model; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the console command options. |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
protected function getOptions() |
144
|
|
|
{ |
145
|
|
|
return [ |
146
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a model transformer.'], |
147
|
|
|
['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain transformer.'], |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
} |