1 | <?php |
||
2 | namespace Salah3id\Domains\Commands; |
||
3 | |||
4 | use Illuminate\Console\Command; |
||
5 | use Illuminate\Support\Collection; |
||
6 | use Illuminate\Support\Str; |
||
7 | use Salah3id\Domains\Repository\Generators\FileAlreadyExistsException; |
||
8 | use Salah3id\Domains\Repository\Generators\MigrationGenerator; |
||
9 | use Salah3id\Domains\Repository\Generators\ModelGenerator; |
||
10 | use Salah3id\Domains\Repository\Generators\ModelRelationsGenerator; |
||
11 | use Salah3id\Domains\Repository\Generators\RepositoryEloquentGenerator; |
||
12 | use Salah3id\Domains\Repository\Generators\RepositoryInterfaceGenerator; |
||
13 | use Symfony\Component\Console\Input\InputArgument; |
||
14 | use Symfony\Component\Console\Input\InputOption; |
||
15 | use Salah3id\Domains\Traits\DomainCommandTrait; |
||
16 | |||
17 | /** |
||
18 | * Class RepositoryCommand |
||
19 | * @package Salah3id\Domains\Commands |
||
20 | * @author Anderson Andrade <[email protected]> |
||
21 | */ |
||
22 | class RepositoryCommand extends Command |
||
23 | { |
||
24 | |||
25 | use DomainCommandTrait; |
||
26 | |||
27 | /** |
||
28 | * The name of command. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $name = 'domain:repository'; |
||
33 | |||
34 | /** |
||
35 | * The description of command. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $description = 'Create a new repository.'; |
||
40 | |||
41 | /** |
||
42 | * The type of class being generated. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $type = 'Repository'; |
||
47 | |||
48 | /** |
||
49 | * @var Collection |
||
50 | */ |
||
51 | protected $generators = null; |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Execute the command. |
||
56 | * |
||
57 | * @see fire() |
||
58 | * @return void |
||
59 | */ |
||
60 | public function handle(){ |
||
61 | $this->laravel->call([$this, 'fire'], func_get_args()); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Execute the command. |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | public function fire() |
||
70 | { |
||
71 | $this->generators = new Collection(); |
||
72 | if($this->argument('domain') && $this->argument('domain-path')) { |
||
73 | $domain = $this->argument('domain'); |
||
74 | $domainPath = $this->argument('domain-path'); |
||
75 | } else { |
||
76 | $domain = $this->getDomainNameForRepo(); |
||
77 | $domainPath = $this->laravel['domains']->getDomainPath($domain); |
||
78 | } |
||
79 | |||
80 | $migrationGenerator = new MigrationGenerator([ |
||
81 | 'name' => 'create_' . Str::snake(Str::plural($this->argument('name'))) . '_table', |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
82 | 'fields' => $this->option('fillable'), |
||
83 | 'force' => $this->option('force'), |
||
84 | ],$domain,$domainPath); |
||
85 | |||
86 | if (!$this->option('skip-migration')) { |
||
87 | $this->generators->push($migrationGenerator); |
||
88 | } |
||
89 | |||
90 | $modelGenerator = new ModelGenerator([ |
||
91 | 'name' => $this->argument('name'), |
||
92 | 'fillable' => $this->option('fillable'), |
||
93 | 'force' => $this->option('force') |
||
94 | ],$domain,$domainPath); |
||
95 | |||
96 | $modelRelationsGenerator = new ModelRelationsGenerator([ |
||
97 | 'name' => $this->argument('name'), |
||
98 | 'force' => $this->option('force'), |
||
99 | ],$domain,$domainPath); |
||
100 | |||
101 | if (!$this->option('skip-model')) { |
||
102 | $this->generators->push($modelGenerator); |
||
103 | $this->generators->push($modelRelationsGenerator); |
||
104 | } |
||
105 | |||
106 | $this->generators->push(new RepositoryInterfaceGenerator([ |
||
107 | 'name' => $this->argument('name'), |
||
108 | 'force' => $this->option('force'), |
||
109 | ],$domain,$domainPath)); |
||
110 | |||
111 | foreach ($this->generators as $generator) { |
||
112 | $generator->run(); |
||
113 | } |
||
114 | |||
115 | $model = $modelGenerator->getRootNamespace() . '\\' . $modelGenerator->getName(); |
||
116 | $model = str_replace([ |
||
117 | "\\", |
||
118 | '/' |
||
119 | ], '\\', $model); |
||
120 | |||
121 | try { |
||
122 | (new RepositoryEloquentGenerator([ |
||
123 | 'name' => $this->argument('name'), |
||
124 | 'rules' => $this->option('rules'), |
||
125 | 'validator' => $this->option('validator'), |
||
126 | 'force' => $this->option('force'), |
||
127 | 'model' => $model |
||
128 | ],$domain,$domainPath))->run(); |
||
129 | $this->info("Repository created successfully."); |
||
130 | } catch (FileAlreadyExistsException $e) { |
||
131 | $this->error($this->type . ' already exists!'); |
||
132 | |||
133 | return false; |
||
0 ignored issues
–
show
|
|||
134 | } |
||
135 | } |
||
136 | |||
137 | |||
138 | /** |
||
139 | * The array of command arguments. |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | public function getArguments() |
||
144 | { |
||
145 | return [ |
||
146 | [ |
||
147 | 'name', |
||
148 | InputArgument::REQUIRED, |
||
149 | 'The name of class being generated.', |
||
150 | null |
||
151 | ], |
||
152 | ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'], |
||
153 | ['domain-path', InputArgument::OPTIONAL, 'The path of domain will be used.'], |
||
154 | ]; |
||
155 | } |
||
156 | |||
157 | |||
158 | /** |
||
159 | * The array of command options. |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function getOptions() |
||
164 | { |
||
165 | return [ |
||
166 | [ |
||
167 | 'fillable', |
||
168 | null, |
||
169 | InputOption::VALUE_OPTIONAL, |
||
170 | 'The fillable attributes.', |
||
171 | null |
||
172 | ], |
||
173 | [ |
||
174 | 'rules', |
||
175 | null, |
||
176 | InputOption::VALUE_OPTIONAL, |
||
177 | 'The rules of validation attributes.', |
||
178 | null |
||
179 | ], |
||
180 | [ |
||
181 | 'validator', |
||
182 | null, |
||
183 | InputOption::VALUE_OPTIONAL, |
||
184 | 'Adds validator reference to the repository.', |
||
185 | null |
||
186 | ], |
||
187 | [ |
||
188 | 'force', |
||
189 | 'f', |
||
190 | InputOption::VALUE_NONE, |
||
191 | 'Force the creation if file already exists.', |
||
192 | null |
||
193 | ], |
||
194 | [ |
||
195 | 'skip-migration', |
||
196 | null, |
||
197 | InputOption::VALUE_NONE, |
||
198 | 'Skip the creation of a migration file.', |
||
199 | null, |
||
200 | ], |
||
201 | [ |
||
202 | 'skip-model', |
||
203 | null, |
||
204 | InputOption::VALUE_NONE, |
||
205 | 'Skip the creation of a model.', |
||
206 | null, |
||
207 | ], |
||
208 | ]; |
||
209 | } |
||
210 | } |
||
211 |