1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\EloquentRepository\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Illuminate\Console\GeneratorCommand; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
10
|
|
|
|
11
|
|
|
class RepositoryMakeCommand extends GeneratorCommand |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name and signature of the console command. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'make:repository'; |
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Create a new eloquent model repository'; |
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
|
|
|
protected function getStub() |
38
|
|
|
{ |
39
|
|
|
$stub = null; |
40
|
|
|
|
41
|
|
|
if ($this->option('model')) { |
42
|
|
|
$stub = '/stubs/repository.model.stub'; |
43
|
|
|
} else { |
44
|
|
|
$stub = '/stubs/repository.stub'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return __DIR__ . $stub; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Build the class with the given name. |
52
|
|
|
* |
53
|
|
|
* @param string $name |
54
|
|
|
* @return string |
55
|
|
|
* @throws FileNotFoundException |
56
|
|
|
*/ |
57
|
|
|
protected function buildClass($name) |
58
|
|
|
{ |
59
|
|
|
$replace = []; |
60
|
|
|
|
61
|
|
|
if ($this->option('model')) { |
62
|
|
|
$replace = $this->buildModelReplacements($replace); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return str_replace( |
66
|
|
|
array_keys($replace), |
67
|
|
|
array_values($replace), |
68
|
|
|
parent::buildClass($name) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Build the model replacement values. |
74
|
|
|
* |
75
|
|
|
* @param array $replace |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
protected function buildModelReplacements(array $replace) |
79
|
|
|
{ |
80
|
|
|
$modelClass = $this->parseModel($this->option('model')); |
|
|
|
|
81
|
|
|
|
82
|
|
|
if (! class_exists($modelClass)) { |
83
|
|
|
if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) { |
84
|
|
|
$this->call('make:model', ['name' => $modelClass]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return array_merge($replace, [ |
89
|
|
|
'DummyFullModelClass' => $modelClass, |
90
|
|
|
'DummyModelClass' => class_basename($modelClass), |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the fully-qualified model class name. |
96
|
|
|
* |
97
|
|
|
* @param string $model |
98
|
|
|
* @return string |
99
|
|
|
* |
100
|
|
|
* @throws InvalidArgumentException |
101
|
|
|
*/ |
102
|
|
|
protected function parseModel($model) |
103
|
|
|
{ |
104
|
|
|
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { |
105
|
|
|
throw new InvalidArgumentException('Model name contains invalid characters.'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$model = trim(str_replace('/', '\\', $model), '\\'); |
109
|
|
|
|
110
|
|
|
if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) { |
111
|
|
|
$model = $rootNamespace . $model; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $model; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the console command options. |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function getOptions() |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a repository for the given model.'], |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the default namespace for the class. |
131
|
|
|
* |
132
|
|
|
* @param string $rootNamespace |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function getDefaultNamespace($rootNamespace) |
136
|
|
|
{ |
137
|
|
|
return $rootNamespace . '\Repositories'; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|