|
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 |
|
$fullBaseClass = $this->option('extend') ?: 'Recca0120\Repository\EloquentRepository'; |
|
64
|
|
|
|
|
65
|
1 |
|
$rootNamespace = trim($this->rootNamespace(), '\\'); |
|
66
|
1 |
|
$namespace = $this->getNamespace($name); |
|
67
|
1 |
|
$baseClass = ltrim(str_replace($namespace, '', $name), '\\'); |
|
68
|
1 |
|
$repositoryContractInterface = $namespace.'\Contracts\\'.$baseClass.'Repository'; |
|
69
|
1 |
|
$modelClass = $rootNamespace.'\\'.($this->option('model') ?: $baseClass); |
|
70
|
|
|
|
|
71
|
1 |
|
if (interface_exists($repositoryContractInterface) === false) { |
|
72
|
1 |
|
$this->call('g:repository-contract', ['name' => $baseClass]); |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
if (is_null($this->option('without-generator-model')) === true && class_exists($modelClass) === false) { |
|
76
|
1 |
|
$this->call('g:model', ['name' => $baseClass]); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
$render = $this->generator->setFullRepositoryClass($name.'Repository') |
|
80
|
1 |
|
->setFullBaseClass($fullBaseClass) |
|
81
|
1 |
|
->setFullModelClass($modelClass) |
|
82
|
1 |
|
->render($this->getStub()); |
|
83
|
|
|
|
|
84
|
1 |
|
$this->renderServiceProvider('Providers/AppServiceProvider'); |
|
85
|
|
|
|
|
86
|
1 |
|
return $render; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* renderServiceProvider. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $className |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
1 |
|
protected function renderServiceProvider($className) |
|
96
|
|
|
{ |
|
97
|
1 |
|
$file = parent::getPath($className); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
1 |
|
$this->files->put( |
|
100
|
1 |
|
$file, |
|
101
|
1 |
|
$this->generator->renderServiceProvider( |
|
102
|
1 |
|
$this->files->get($file) |
|
103
|
1 |
|
) |
|
104
|
1 |
|
); |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the destination class path. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
1 |
|
protected function getPath($name) |
|
115
|
|
|
{ |
|
116
|
1 |
|
return str_replace('.php', 'Repository.php', parent::getPath($name)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get the console command options. |
|
121
|
|
|
* |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
1 |
|
protected function getOptions() |
|
125
|
|
|
{ |
|
126
|
|
|
return [ |
|
127
|
1 |
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a repository for the given model.'], |
|
128
|
1 |
|
['extend', '', InputOption::VALUE_OPTIONAL, 'repository extend.'], |
|
129
|
1 |
|
['without-generator-model', '', InputOption::VALUE_OPTIONAL, 'don\'t generate model.'], |
|
130
|
1 |
|
]; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.