|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Nwidart\Modules\Support\Stub; |
|
7
|
|
|
use Nwidart\Modules\Commands\GeneratorCommand; |
|
8
|
|
|
use Nwidart\Modules\Console\Traits\Definitions; |
|
9
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
|
10
|
|
|
|
|
11
|
|
|
abstract class Command extends GeneratorCommand |
|
12
|
|
|
{ |
|
13
|
|
|
use Definitions; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The name to be appended to the generated resources. |
|
17
|
|
|
* |
|
18
|
|
|
* @var null|string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $appendable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Stub file of the resource |
|
24
|
|
|
* |
|
25
|
|
|
* @var null|string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $stubFile; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Getter for appendable |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function appendable() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->appendable; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Getter for the stub file |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function stubFile() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->stubFile; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Replacements for the stub file |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function replaces() |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
//... |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get stub file contents |
|
63
|
|
|
* |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getTemplateContents() |
|
67
|
|
|
{ |
|
68
|
|
|
return (new Stub($this->stubFile(), $this->replaces()))->render(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get destination file path. |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getDestinationFilePath() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->getModulePath() . "/" . |
|
|
|
|
|
|
79
|
|
|
GenerateConfigReader::read($this->getGeneratorConfigKey())->getPath() . "/" . |
|
|
|
|
|
|
80
|
|
|
$this->resolveFilename() . '.' . $this->outputExtension; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Method to apply necessary functionality |
|
85
|
|
|
* before console command gets executed |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function before() |
|
90
|
|
|
{ |
|
91
|
|
|
// ... |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Method to apply necessary functionality |
|
96
|
|
|
* after console command has executed. |
|
97
|
|
|
* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
public function after() |
|
101
|
|
|
{ |
|
102
|
|
|
// ... |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Execute the console command |
|
107
|
|
|
* |
|
108
|
|
|
* @return int |
|
109
|
|
|
*/ |
|
110
|
|
|
public function handle(): int |
|
111
|
|
|
{ |
|
112
|
|
|
$this->before(); |
|
113
|
|
|
|
|
114
|
|
|
if (parent::handle() != E_ERROR) { |
|
115
|
|
|
$this->after(); |
|
116
|
|
|
} |
|
117
|
|
|
return 0; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get and resolve the filename. |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getFileName(): string |
|
126
|
|
|
{ |
|
127
|
|
|
|
|
128
|
|
|
$name = Str::studly($this->argument($this->argumentName)); |
|
|
|
|
|
|
129
|
|
|
if ($this->appendable() && !Str::contains(strtolower($name), strtolower($this->appendable()))) { |
|
|
|
|
|
|
130
|
|
|
$name .= Str::studly($this->appendable()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return Str::singular(Str::studly($name)); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: