1 | <?php |
||
2 | |||
3 | namespace Salah3id\Domains\Commands; |
||
4 | |||
5 | use Illuminate\Console\Command; |
||
6 | use Salah3id\Domains\Exceptions\FileAlreadyExistException; |
||
7 | use Salah3id\Domains\Generators\FileGenerator; |
||
8 | |||
9 | abstract class GeneratorCommand extends Command |
||
10 | { |
||
11 | /** |
||
12 | * The name of 'name' argument. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $argumentName = ''; |
||
17 | |||
18 | /** |
||
19 | * Get template contents. |
||
20 | * |
||
21 | * @return string |
||
22 | */ |
||
23 | abstract protected function getTemplateContents(); |
||
24 | |||
25 | /** |
||
26 | * Get the destination file path. |
||
27 | * |
||
28 | * @return string |
||
29 | */ |
||
30 | abstract protected function getDestinationFilePath(); |
||
31 | |||
32 | /** |
||
33 | * Execute the console command. |
||
34 | */ |
||
35 | public function handle(): int |
||
36 | { |
||
37 | $path = str_replace('\\', '/', $this->getDestinationFilePath()); |
||
38 | |||
39 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) { |
||
40 | $this->laravel['files']->makeDirectory($dir, 0777, true); |
||
41 | } |
||
42 | |||
43 | $contents = $this->getTemplateContents(); |
||
44 | |||
45 | try { |
||
46 | $this->components->task("Generating file {$path}",function () use ($path,$contents) { |
||
47 | $overwriteFile = $this->hasOption('force') ? $this->option('force') : false; |
||
48 | (new FileGenerator($path, $contents))->withFileOverwrite($overwriteFile)->generate(); |
||
49 | }); |
||
50 | |||
51 | } catch (FileAlreadyExistException $e) { |
||
52 | $this->components->error("File : {$path} already exists."); |
||
53 | |||
54 | return E_ERROR; |
||
55 | } |
||
56 | |||
57 | return 0; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get class name. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function getClass() |
||
66 | { |
||
67 | return class_basename($this->argument($this->argumentName)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Get default namespace. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getDefaultNamespace(): string |
||
76 | { |
||
77 | return ''; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Get class namespace. |
||
82 | * |
||
83 | * @param \Salah3id\Domains\Domain $domain |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function getClassNamespace($domain) |
||
88 | { |
||
89 | $extra = str_replace($this->getClass(), '', $this->argument($this->argumentName)); |
||
90 | |||
91 | $extra = str_replace('/', '\\', $extra); |
||
92 | |||
93 | $namespace = $this->laravel['domains']->config('namespace'); |
||
94 | |||
95 | $namespace .= '\\' . $domain->getStudlyName(); |
||
96 | |||
97 | $namespace .= '\\' . $this->getDefaultNamespace(); |
||
98 | |||
99 | $namespace .= '\\' . $extra; |
||
100 | |||
101 | $namespace = str_replace('/', '\\', $namespace); |
||
102 | |||
103 | return trim($namespace, '\\'); |
||
104 | } |
||
105 | } |
||
106 |