1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* An Artisan command class responsible for making transformer classes. |
10
|
|
|
* |
11
|
|
|
* @package flugger/laravel-responder |
12
|
|
|
* @author Alexander Tømmerås <[email protected]> |
13
|
|
|
* @license The MIT License |
14
|
|
|
*/ |
15
|
|
|
class MakeTransformer extends GeneratorCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The console command name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name = 'make:transformer'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Create a new transformer class'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The type of class being generated. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $type = 'Transformer'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the stub file for the generator. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
protected function getStub() |
44
|
|
|
{ |
45
|
|
|
if ($this->option('model')) { |
46
|
|
|
return __DIR__ . '../../resources/stubs/transformer.model.stub'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return __DIR__ . '../../resources/stubs/transformer.plain.stub'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the default namespace for the class. |
54
|
|
|
* |
55
|
|
|
* @param string $rootNamespace |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
protected function getDefaultNamespace($rootNamespace) |
59
|
|
|
{ |
60
|
|
|
return $rootNamespace . '\Transformers'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Build the class with the given name. |
65
|
|
|
* |
66
|
|
|
* @param string $name |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
protected function buildClass($name) |
70
|
|
|
{ |
71
|
|
|
$replace = []; |
72
|
|
|
|
73
|
|
|
if ($this->option('model')) { |
74
|
|
|
$replace = $this->buildModelReplacements($replace); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return str_replace(array_keys($replace), array_values($replace), parent::buildClass($name)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Build the model replacement values. |
82
|
|
|
* |
83
|
|
|
* @param array $replace |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
protected function buildModelReplacements(array $replace) |
87
|
|
|
{ |
88
|
|
|
$model = $this->qualifyClass($this->option('model')); |
|
|
|
|
89
|
|
|
|
90
|
|
|
return array_merge($replace, [ |
91
|
|
|
'DummyFullModelClass' => $model, |
92
|
|
|
'DummyModelClass' => class_basename($model), |
93
|
|
|
'DummyModelVariable' => lcfirst(class_basename($model)), |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the console command options. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function getOptions() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a model transformerl.'], |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
} |
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.