1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ikechukwukalu\Magicmake\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
#[AsCommand(name: 'magic:contract')] |
11
|
|
|
class MagicContractCommand extends GeneratorCommand |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The console command name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'magic:contract'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The name of the console command. |
22
|
|
|
* |
23
|
|
|
* This name is used to identify the command during lazy loading. |
24
|
|
|
* |
25
|
|
|
* @var string|null |
26
|
|
|
* |
27
|
|
|
* @deprecated |
28
|
|
|
*/ |
29
|
|
|
protected static $defaultName = 'magic:contract'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The console command description. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $description = 'Create a new magic contract class'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The type of class being generated. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $type = 'Contract'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Build the class with the given name. |
47
|
|
|
* |
48
|
|
|
* @param string $name |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
protected function buildClass($name) |
52
|
|
|
{ |
53
|
|
|
if ($this->alreadyExists(str_replace('.php', '', $this->getPath($name)))) { |
54
|
|
|
$this->components->error("This Contract already exists"); |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$ary = explode("\\", $name); |
59
|
|
|
$model = $ary[count($ary) - 1]; |
60
|
|
|
$modelVariable = $this->option('variable'); |
61
|
|
|
$modelUnderScore = $this->option('underscore'); |
62
|
|
|
$name = "{$name}RepositoryInterface"; |
63
|
|
|
|
64
|
|
|
$stub = str_replace( |
65
|
|
|
['DummyModel', '{{ model }}'], class_basename($model), parent::buildClass($name) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$stub = str_replace( |
69
|
|
|
['DummyModelVariable', '{{ modelVariable }}'], trim($modelVariable, '\\'), $stub |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return str_replace( |
73
|
|
|
['DummyModelUnderScore', '{{ modelUnderScore }}'], trim($modelUnderScore, '\\'), $stub |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Determine if the class already exists. |
80
|
|
|
* |
81
|
|
|
* @param string $rawName |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
protected function alreadyExists($rawName) |
85
|
|
|
{ |
86
|
|
|
return class_exists($rawName) || |
87
|
|
|
$this->files->exists($this->getPath($this->qualifyClass($rawName))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the stub file for the generator. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getStub() |
96
|
|
|
{ |
97
|
|
|
return __DIR__.'/stubs/contract.stub'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Resolve the fully-qualified path to the stub. |
102
|
|
|
* |
103
|
|
|
* @param string $stub |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function resolveStubPath($stub) |
107
|
|
|
{ |
108
|
|
|
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) |
109
|
|
|
? $customPath |
110
|
|
|
: __DIR__.$stub; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the default namespace for the class. |
115
|
|
|
* |
116
|
|
|
* @param string $rootNamespace |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
protected function getDefaultNamespace($rootNamespace) |
120
|
|
|
{ |
121
|
|
|
return $rootNamespace.'\Contracts'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the console command options. |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
protected function getOptions() |
130
|
|
|
{ |
131
|
|
|
return [ |
132
|
|
|
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the contract already exists'], |
133
|
|
|
['variable', 'var', InputOption::VALUE_REQUIRED, 'Create a model variable value for this contract class'], |
134
|
|
|
['underscore', 'u', InputOption::VALUE_REQUIRED, 'Create a model underscore value for this contract class'], |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the destination class path. |
140
|
|
|
* |
141
|
|
|
* @param string $name |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
protected function getPath($name) |
145
|
|
|
{ |
146
|
|
|
$name = "{$name}RepositoryInterface"; |
147
|
|
|
$name = Str::replaceFirst($this->rootNamespace(), '', $name); |
148
|
|
|
|
149
|
|
|
return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php'; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|