|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PWWEB\Artomator; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Console\GeneratorCommand; |
|
7
|
|
|
|
|
8
|
|
|
abstract class Artomator extends GeneratorCommand |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The package of class being generated. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $package = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Model class name. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $modelClass = ''; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Request class name. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $requestClass = ''; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Build the model replacement values. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $replace The existing replacements to append to. |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function buildModelReplacements(array $replace = []) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($this->modelClass == '') { |
|
41
|
|
|
$this->modelClass = $this->parseModel((string) $this->getNameInput()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($this->requestClass == '') { |
|
45
|
|
|
$this->requestClass = $this->parseRequest((string) $this->getNameInput()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$table = Str::snake(Str::pluralStudly(str_replace('/', '', $this->argument('name')))); |
|
49
|
|
|
|
|
50
|
|
|
return array_merge( |
|
51
|
|
|
$replace, |
|
52
|
|
|
[ |
|
53
|
|
|
'DummyFullModelClass' => $this->modelClass, |
|
54
|
|
|
'DummyRequestClass' => $this->requestClass, |
|
55
|
|
|
'DummyModelClass' => class_basename($this->modelClass), |
|
56
|
|
|
'DummyModelVariable' => lcfirst(class_basename($this->modelClass)), |
|
57
|
|
|
'DummyPluralModelClass' => Str::pluralStudly(class_basename($this->modelClass)), |
|
58
|
|
|
'DummySnakeCaseClass' => $table, |
|
59
|
|
|
'DummyPackageVariable' => lcfirst($this->package) . ".", |
|
60
|
|
|
'DummyPackagePlaceholder' => config('app.name'), |
|
61
|
|
|
'DummyCopyrightPlaceholder' => config('artomator.copyright'), |
|
62
|
|
|
'DummyLicensePlaceholder' => config('artomator.license'), |
|
63
|
|
|
'DummyAuthorPlaceholder' => $this->parseAuthors(config('artomator.authors')), |
|
64
|
|
|
] |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the formatted author(s) from the config file. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string[] $authors Authors array. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string Formmated string of authors. |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function parseAuthors($authors) |
|
76
|
|
|
{ |
|
77
|
|
|
if (is_array($authors) === false and is_string($authors) === false) { |
|
|
|
|
|
|
78
|
|
|
throw new InvalidArgumentException('Authors must be an array of strings or a string.'); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$formatted = ''; |
|
82
|
|
|
|
|
83
|
|
|
if (is_array($authors) === true) { |
|
84
|
|
|
if (is_string($authors[0]) === false) { |
|
85
|
|
|
throw new InvalidArgumentException('The array of authors must be strings.'); |
|
86
|
|
|
} |
|
87
|
|
|
$formatted .= array_shift($authors); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($authors as $author) { |
|
90
|
|
|
if (is_string($author) === false) { |
|
91
|
|
|
throw new InvalidArgumentException('The array of authors must be strings.'); |
|
92
|
|
|
} |
|
93
|
|
|
$formatted .= "\n * @author " . $author; |
|
94
|
|
|
} |
|
95
|
|
|
} else { |
|
96
|
|
|
$formatted .= $authors; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $formatted; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get the fully-qualified model class name. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $model The model name to return the FQN for. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
* |
|
109
|
|
|
* @throws \InvalidArgumentException |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function parseModel($model) |
|
112
|
|
|
{ |
|
113
|
|
|
if (preg_match('([^A-Za-z0-9_/\\\\])', $model) === true) { |
|
114
|
|
|
throw new InvalidArgumentException('Model name contains invalid characters.'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->package = (strstr($model, '/', true) ?? null); |
|
118
|
|
|
|
|
119
|
|
|
$model = trim(str_replace('/', '\\', $model), '\\'); |
|
120
|
|
|
|
|
121
|
|
|
if (Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace()) === false) { |
|
122
|
|
|
$model = $rootNamespace . 'Models\\' . $model; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $model; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get the fully-qualified request class name. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $model The model to return the FQN for. |
|
132
|
|
|
* |
|
133
|
|
|
* @return string |
|
134
|
|
|
* |
|
135
|
|
|
* @throws \InvalidArgumentException |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function parseRequest($model) |
|
138
|
|
|
{ |
|
139
|
|
|
if (preg_match('([^A-Za-z0-9_/\\\\])', $model) === true) { |
|
140
|
|
|
throw new InvalidArgumentException('Model name contains invalid characters.'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$model = trim(str_replace('/', '\\', $model), '\\'); |
|
144
|
|
|
|
|
145
|
|
|
if (Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace()) === false) { |
|
146
|
|
|
$model = $rootNamespace . 'Http\\Requests\\' . $model; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $model; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|