1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Generator\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
|
9
|
|
|
class RepositoryMakeCommand extends GeneratorCommand |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The console command name. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $name = 'g:repository'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Create a new repository class'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The type of class being generated. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $type = 'Repository'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the stub file for the generator. |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
1 |
|
protected function getStub() |
38
|
|
|
{ |
39
|
1 |
|
return __DIR__.'/../../resources/stubs/Repositories/Repository.stub'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the default namespace for the class. |
44
|
|
|
* |
45
|
|
|
* @param string $rootNamespace |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
1 |
|
protected function getDefaultNamespace($rootNamespace) |
50
|
|
|
{ |
51
|
1 |
|
return $rootNamespace.'\Repositories'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Build the class with the given name. |
56
|
|
|
* |
57
|
|
|
* @param string $name |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
protected function buildClass($name) |
62
|
|
|
{ |
63
|
1 |
|
$modelClass = $this->parseModel( |
|
|
|
|
64
|
1 |
|
$this->option('model') ?: $this->rootNamespace().class_basename($name) |
|
|
|
|
65
|
1 |
|
); |
66
|
|
|
|
67
|
1 |
|
$fullBaseClass = $this->option('extend') ?: 'Recca0120\Repository\EloquentRepository'; |
68
|
|
|
|
69
|
1 |
|
$rootNamespace = trim($this->rootNamespace(), '\\'); |
70
|
1 |
|
$namespace = $this->getNamespace($name); |
71
|
1 |
|
$baseClass = ltrim(str_replace($namespace, '', $name), '\\'); |
72
|
1 |
|
$repositoryContractInterface = $namespace.'\Contracts\\'.$baseClass.'Repository'; |
73
|
1 |
|
$modelClass = $rootNamespace.'\\'.$baseClass; |
74
|
|
|
|
75
|
1 |
|
if (interface_exists($repositoryContractInterface) === false) { |
76
|
1 |
|
$this->call('g:repository-contract', ['name' => $baseClass]); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
if (is_null($this->option('without-generator-model')) === true && class_exists($modelClass) === false) { |
80
|
1 |
|
$this->call('g:model', ['name' => $baseClass]); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
$render = $this->generator->setFullRepositoryClass($name.'Repository') |
84
|
1 |
|
->setFullBaseClass($fullBaseClass) |
|
|
|
|
85
|
1 |
|
->setFullModelClass($modelClass) |
86
|
1 |
|
->render($this->getStub()); |
87
|
|
|
|
88
|
1 |
|
$this->renderServiceProvider('Providers/AppServiceProvider'); |
89
|
|
|
|
90
|
1 |
|
return $render; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* renderServiceProvider. |
95
|
|
|
* |
96
|
|
|
* @param string $className |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
protected function renderServiceProvider($className) |
100
|
|
|
{ |
101
|
1 |
|
$file = parent::getPath($className); |
|
|
|
|
102
|
|
|
|
103
|
1 |
|
$this->files->put( |
104
|
1 |
|
$file, |
105
|
1 |
|
$this->generator->renderServiceProvider( |
106
|
1 |
|
$this->files->get($file) |
107
|
1 |
|
) |
108
|
1 |
|
); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the destination class path. |
113
|
|
|
* |
114
|
|
|
* @param string $name |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
1 |
|
protected function getPath($name) |
119
|
|
|
{ |
120
|
1 |
|
return str_replace('.php', 'Repository.php', parent::getPath($name)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the fully-qualified model class name. |
125
|
|
|
* |
126
|
|
|
* @param string $model |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
1 |
|
protected function parseModel($model) |
131
|
|
|
{ |
132
|
1 |
|
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { |
133
|
|
|
throw new InvalidArgumentException('Model name contains invalid characters.'); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
$model = trim(str_replace('/', '\\', $model), '\\'); |
137
|
|
|
|
138
|
1 |
|
if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) { |
|
|
|
|
139
|
1 |
|
$model = $rootNamespace.$model; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
1 |
|
return $model; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the console command options. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
1 |
|
protected function getOptions() |
151
|
|
|
{ |
152
|
|
|
return [ |
153
|
1 |
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a repository for the given model.'], |
154
|
1 |
|
['extend', '', InputOption::VALUE_OPTIONAL, 'repository extend.'], |
155
|
1 |
|
['without-generator-model', '', InputOption::VALUE_OPTIONAL, 'don\'t generate model.'], |
156
|
1 |
|
]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.