1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rawilk\LaravelModules\Commands\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Rawilk\LaravelModules\Exceptions\FileAlreadyExists; |
8
|
|
|
use Rawilk\LaravelModules\Generators\FileGenerator; |
9
|
|
|
use Rawilk\LaravelModules\Module; |
10
|
|
|
|
11
|
|
|
abstract class GeneratorCommand extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name of the 'name' argument. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $argumentName = ''; |
19
|
|
|
|
20
|
|
|
abstract protected function getDestinationFilePath(): string; |
21
|
|
|
|
22
|
|
|
abstract protected function getTemplateContents(): string; |
23
|
|
|
|
24
|
|
|
public function handle(): void |
25
|
|
|
{ |
26
|
|
|
$path = str_replace('\\', '/', $this->getDestinationFilePath()); |
27
|
|
|
|
28
|
|
|
if (! $this->laravel['files']->isDirectory($dir = dirname($path))) { |
29
|
|
|
$this->laravel['files']->makeDirectory($dir, 0777, true); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$contents = $this->getTemplateContents(); |
33
|
|
|
|
34
|
|
|
try { |
35
|
|
|
$overwriteFile = $this->hasOption('force') ? $this->option('force') : false; |
36
|
|
|
(new FileGenerator($path, $contents))->withFileOverwrite($overwriteFile)->generate(); |
37
|
|
|
|
38
|
|
|
$this->info("Created: {$path}"); |
39
|
|
|
} catch (FileAlreadyExists $e) { |
40
|
|
|
$this->error("File: {$path} already exists."); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the base class to extend for a given class type. |
46
|
|
|
* |
47
|
|
|
* @param string $classType |
48
|
|
|
* @param bool $returnBasename |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
protected function getBaseClass(string $classType, bool $returnBasename = false): string |
52
|
|
|
{ |
53
|
|
|
$baseClass = $this->hasOption('base_class') && $this->option('base_class') !== null |
54
|
|
|
? $this->option('base_class') |
55
|
|
|
: $this->laravel['modules']->config("base_classes.{$classType}"); |
56
|
|
|
|
57
|
|
|
return $returnBasename ? class_basename($baseClass) : str_replace('/', '\\', $baseClass); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getClass(): string |
61
|
|
|
{ |
62
|
|
|
return class_basename($this->argument($this->argumentName)); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function getClassNamespace(Module $module): string |
66
|
|
|
{ |
67
|
|
|
$namespace = $this->laravel['modules']->config('namespace'); |
68
|
|
|
|
69
|
|
|
$namespace .= '\\' . $module->getStudlyName(); |
70
|
|
|
|
71
|
|
|
$namespace .= '\\' . $this->getDefaultNamespace(); |
72
|
|
|
|
73
|
|
|
$namespace .= '\\' . $this->getExtraNamespace($this->argument($this->argumentName)); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$namespace = str_replace('/', '\\', $namespace); |
76
|
|
|
|
77
|
|
|
return trim($namespace, '\\'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function getDefaultNamespace(): string |
81
|
|
|
{ |
82
|
|
|
return ''; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function getFileName(): string |
86
|
|
|
{ |
87
|
|
|
return Str::studly($this->argument($this->argumentName)); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function getExtraNamespace(string $path): string |
91
|
|
|
{ |
92
|
|
|
$path = str_replace('/', '\\', $path); |
93
|
|
|
$pieces = explode("\\", $path); |
94
|
|
|
|
95
|
|
|
return implode("\\", array_slice($pieces, 0, count($pieces) - 1) ); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|