1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rawilk\LaravelModules\Commands\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader; |
7
|
|
|
use Rawilk\LaravelModules\Support\Stub; |
8
|
|
|
use Rawilk\LaravelModules\Traits\ModuleCommands; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
|
11
|
|
|
class RepositoryMakeCommand extends GeneratorCommand |
12
|
|
|
{ |
13
|
|
|
use ModuleCommands; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $argumentName = 'repository'; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $signature = 'module:make-repository |
20
|
|
|
{repository : The name of the repository} |
21
|
|
|
{module? : The name of the module to create the repository for} |
22
|
|
|
{--base_class= : Override the default base repository class (from config)} |
23
|
|
|
{--model= : The model the repository is for}'; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $description = 'Generate a new repository for the specified module.'; |
27
|
|
|
|
28
|
|
|
protected function getDefaultNamespace(): string |
29
|
|
|
{ |
30
|
|
|
/** @var \Rawilk\LaravelModules\Contracts\Repository $module */ |
31
|
|
|
$module = $this->laravel['modules']; |
32
|
|
|
|
33
|
|
|
return $module->config('paths.generator.repository.namespace') ?: $module->config('paths.generator.repository.path', 'Repositories'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function getDestinationFilePath(): string |
37
|
|
|
{ |
38
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
39
|
|
|
|
40
|
|
|
$repositoryPath = GenerateConfigReader::read('repository'); |
41
|
|
|
|
42
|
|
|
return $path . $repositoryPath->getPath() . '/' . $this->getRepositoryName() . '.php'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function getTemplateContents(): string |
46
|
|
|
{ |
47
|
|
|
/** @var \Rawilk\LaravelModules\Module $module */ |
48
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
49
|
|
|
|
50
|
|
|
return (new Stub('/repository.stub', [ |
51
|
|
|
'NAMESPACE' => $this->getClassNamespace($module), |
52
|
|
|
'CLASS' => class_basename($this->getRepositoryName()), |
53
|
|
|
'BASE_CLASS' => $this->getBaseClass('repository'), |
54
|
|
|
'BASE_CLASS_SHORT' => $this->getBaseClass('repository', true), |
55
|
|
|
'MODEL' => $this->getModel(), |
56
|
|
|
'MODEL_NAMESPACE' => $this->getModel(false), |
57
|
|
|
]))->render(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function getModel(bool $returnBasename = true): string |
61
|
|
|
{ |
62
|
|
|
$model = Str::studly($this->option('model')); |
63
|
|
|
|
64
|
|
|
if (! $model) { |
65
|
|
|
$model = Model::class; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$model = str_replace('/', '\\', $model); |
69
|
|
|
|
70
|
|
|
return $returnBasename |
71
|
|
|
? class_basename($model) . '::class' |
72
|
|
|
: $model; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function getRepositoryName(): string |
76
|
|
|
{ |
77
|
|
|
$repository = Str::studly($this->argument('repository')); |
|
|
|
|
78
|
|
|
|
79
|
|
|
if (! Str::contains(strtolower($repository), 'repository')) { |
80
|
|
|
$repository .= 'Repository'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $repository; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|