|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ViewModels\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Console\GeneratorCommand; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
|
|
9
|
|
|
class ViewModelMakeCommand extends GeneratorCommand |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The console command name. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $name = 'make:view-model'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The console command description. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $description = 'Create a new view-model class'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The type of class being generated. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $type = 'View model'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Execute the console command. |
|
34
|
|
|
* |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function handle() |
|
38
|
|
|
{ |
|
39
|
|
|
if (parent::handle() === false && ! $this->option('force')) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the destination class path. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $name |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getPath($name) |
|
51
|
|
|
{ |
|
52
|
|
|
$name = Str::replaceFirst($this->rootNamespace(), '', $name); |
|
53
|
|
|
|
|
54
|
|
|
if ($this->hasNamespace($name)) { |
|
55
|
|
|
return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->laravel['path'].'/'.str_replace('\\', '/', 'ViewModels/' . $name).'.php'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the stub file for the generator. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getStub() |
|
67
|
|
|
{ |
|
68
|
|
|
return __DIR__.'/stubs/view-model.stub'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Replace the namespace for the given stub. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $stub |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* @return $this |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function replaceNamespace(&$stub, $name) |
|
79
|
|
|
{ |
|
80
|
|
|
$namespace = $this->hasNamespace($name) ? |
|
81
|
|
|
$this->getNamespace($name) : |
|
82
|
|
|
$this->getNamespace($name) . '\\ViewModels'; |
|
83
|
|
|
|
|
84
|
|
|
$stub = str_replace('DummyNamespace', $namespace, $stub); |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Check wheter the name param has |
|
91
|
|
|
* namespace or not. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $stub |
|
|
|
|
|
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function hasNamespace($name) |
|
98
|
|
|
{ |
|
99
|
|
|
return str_contains($name, '\\'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get the console command options. |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function getOptions() |
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
|
|
['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists'], |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.