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