|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Recca0120\Generator\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
7
|
|
|
|
|
8
|
|
|
class ViewMakeCommand extends GeneratorCommand |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The console command name. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $name = 'generate:view'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The console command description. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $description = 'Create a new view'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The type of class being generated. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $type = 'View'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get the stub file for the generator. |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
1 |
|
protected function getStub() |
|
37
|
|
|
{ |
|
38
|
1 |
|
return $this->getStubResource('resources/views/scaffold/'.$this->view().'.blade.stub'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the default namespace for the class. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $rootNamespace |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
1 |
|
protected function getDefaultNamespace($rootNamespace) |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $rootNamespace.'\Http\Controllers'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Build the class with the given name. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
1 |
|
protected function buildClass($name) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$fullBaseClass = $this->getNamespace($name).'\Controller'; |
|
63
|
1 |
|
$fullBaseClass = class_exists($fullBaseClass) === true ? |
|
64
|
1 |
|
$fullBaseClass : 'App\Http\Controllers\Controller'; |
|
65
|
|
|
|
|
66
|
1 |
|
$fullBaseClass = $this->option('extend') ?: $fullBaseClass; |
|
67
|
1 |
|
$rootNamespace = trim($this->rootNamespace(), '\\'); |
|
68
|
1 |
|
$namespace = $this->getNamespace($name); |
|
69
|
1 |
|
$baseClass = ltrim(str_replace($namespace, '', $name), '\\'); |
|
70
|
1 |
|
$repositoryContractInterface = $rootNamespace.'\Repositories\Contracts\\'.$baseClass.'Repository'; |
|
71
|
1 |
|
$repositoryClass = $rootNamespace.'\Repositories\\'.$baseClass.'Repository'; |
|
|
|
|
|
|
72
|
1 |
|
$requestClass = $rootNamespace.'\Http\Requests\\'.$baseClass.'Request'; |
|
73
|
|
|
|
|
74
|
1 |
|
return $this->generator->setFullControllerClass($name.'Controller') |
|
75
|
1 |
|
->setFullBaseClass($fullBaseClass) |
|
|
|
|
|
|
76
|
1 |
|
->setFullRepositoryInterface($repositoryContractInterface) |
|
77
|
1 |
|
->setFullRequestClass($requestClass) |
|
78
|
1 |
|
->render($this->getStub()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the destination class path. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $name |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
1 |
|
protected function getPath($name) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$name = Str::replaceFirst($this->rootNamespace().'Http\Controllers\\', '', $name); |
|
91
|
1 |
|
$path = $this->laravel->basePath().'/resources/views/'; |
|
92
|
|
|
|
|
93
|
1 |
|
return $path .= implode('/', array_map(function ($path) { |
|
94
|
1 |
|
return Str::snake($path, '-'); |
|
95
|
1 |
|
}, explode('\\', $name))).'/'.$this->view().'.blade.php'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* view. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
1 |
|
protected function view() |
|
104
|
|
|
{ |
|
105
|
1 |
|
return $this->option('view') ?: 'index'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the console command options. |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
1 |
View Code Duplication |
protected function getOptions() |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
return [ |
|
116
|
1 |
|
['extend', '', InputOption::VALUE_OPTIONAL, 'controller extend.'], |
|
117
|
1 |
|
['view', '', InputOption::VALUE_OPTIONAL, 'view'], |
|
118
|
1 |
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.