1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Services\Capsules; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
trait HasCapsules |
8
|
|
|
{ |
9
|
|
|
protected function getAutoloader() |
10
|
|
|
{ |
11
|
|
|
return app()->bound('autoloader') |
12
|
|
|
? app('autoloader') |
13
|
|
|
: require base_path('vendor/autoload.php'); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function getCapsuleList() |
17
|
|
|
{ |
18
|
|
|
$path = $this->getCapsulesPath(); |
19
|
|
|
|
20
|
|
|
$list = collect(config('twill.capsules.list')); |
21
|
|
|
|
22
|
|
|
if (config('twill.capsules.loaded')) { |
23
|
|
|
return $list; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $list |
27
|
|
|
->where('enabled', true) |
28
|
|
|
->map(function ($capsule) use ($path) { |
29
|
|
|
return $this->makeCapsule($capsule, $path); |
30
|
|
|
}); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getCapsuleByModel($model) |
34
|
|
|
{ |
35
|
|
|
return $this->getCapsuleList() |
36
|
|
|
->where('singular', Str::studly($model)) |
37
|
|
|
->first(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getCapsuleByModule($module) |
41
|
|
|
{ |
42
|
|
|
return $this->getCapsuleList() |
43
|
|
|
->filter(function ($capsule) use ($module) { |
44
|
|
|
return Str::lower($capsule['plural']) == Str::lower($module); |
45
|
|
|
}) |
46
|
|
|
->first(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed |
51
|
|
|
*/ |
52
|
|
|
public function getCapsulesPath() |
53
|
|
|
{ |
54
|
|
|
return config('twill.capsules.path'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed |
59
|
|
|
*/ |
60
|
|
|
public function getCapsulesSubdir() |
61
|
|
|
{ |
62
|
|
|
$subdir = config('twill.capsules.namespaces.subdir'); |
63
|
|
|
|
64
|
|
|
return $subdir; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function makeCapsule($capsule, $basePath = null) |
68
|
|
|
{ |
69
|
|
|
$basePath = $basePath ?? $this->getCapsulesPath(); |
70
|
|
|
|
71
|
|
|
$capsule['name'] = Str::studly($capsule['name']); |
72
|
|
|
|
73
|
|
|
$capsule['module'] = Str::camel($capsule['name']); |
74
|
|
|
|
75
|
|
|
$capsule['plural'] = $name = $capsule['name']; |
76
|
|
|
|
77
|
|
|
$capsule['singular'] = $singular = |
78
|
|
|
$capsule['singular'] ?? Str::singular($name); |
79
|
|
|
|
80
|
|
|
$twillNamespace = config('twill.namespace'); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$capsule[ |
83
|
|
|
'namespace' |
84
|
|
|
] = $capsuleNamespace = $this->getManager()->capsuleNamespace( |
|
|
|
|
85
|
|
|
$capsule['name'] |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$capsule[ |
89
|
|
|
'database_namespace' |
90
|
|
|
] = "$capsuleNamespace\Database"; |
91
|
|
|
|
92
|
|
|
$capsule[ |
93
|
|
|
'seeds_namespace' |
94
|
|
|
] = "{$capsule['database_namespace']}\Seeds"; |
95
|
|
|
|
96
|
|
|
$capsule['model'] = $capsule['models'] = $models = |
97
|
|
|
"{$capsuleNamespace}\\" . |
98
|
|
|
config('twill.capsules.namespaces.models'); |
99
|
|
|
$capsule['repositories'] = $repositories = |
100
|
|
|
"{$capsuleNamespace}\\" . |
101
|
|
|
config('twill.capsules.namespaces.repositories'); |
102
|
|
|
$capsule['controllers'] = $controllers = |
103
|
|
|
"{$capsuleNamespace}\\" . |
104
|
|
|
config('twill.capsules.namespaces.controllers'); |
105
|
|
|
$capsule['requests'] = $requests = |
106
|
|
|
"{$capsuleNamespace}\\" . |
107
|
|
|
config('twill.capsules.namespaces.requests'); |
108
|
|
|
|
109
|
|
|
$capsule['psr4_path'] = "$basePath/{$name}" . (filled($this->getCapsulesSubdir()) ? $this->getCapsulesSubdir().'/' : ''); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$capsule['base_path'] = $basePath; |
112
|
|
|
|
113
|
|
|
$capsule['database_psr4_path'] = "$basePath/{$name}/database"; |
114
|
|
|
|
115
|
|
|
$capsule['seeds_psr4_path'] = "{$capsule['database_psr4_path']}/seeds"; |
116
|
|
|
|
117
|
|
|
$capsule['root_path'] = $root = $this->capsuleRootPath($capsule); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$capsule[ |
120
|
|
|
'migrations_dir' |
121
|
|
|
] = "{$capsule['root_path']}/database/migrations"; |
122
|
|
|
|
123
|
|
|
$capsule['views_dir'] = "{$capsule['root_path']}/resources/views"; |
124
|
|
|
|
125
|
|
|
$capsule['view_prefix'] = "{$name}.resources.views.admin"; |
126
|
|
|
|
127
|
|
|
$capsule['routes_file'] = "{$capsule['root_path']}/routes/admin.php"; |
128
|
|
|
|
129
|
|
|
$capsule['model'] = "{$models}\\{$singular}"; |
130
|
|
|
|
131
|
|
|
$capsule['models_dir'] = $this->namespaceToPath($capsule, $models); |
132
|
|
|
|
133
|
|
|
$capsule['translation'] = "{$models}\\{$singular}Translation"; |
134
|
|
|
|
135
|
|
|
$capsule['slug'] = "{$models}\\{$singular}Slug"; |
136
|
|
|
|
137
|
|
|
$capsule['revision'] = "{$models}\\{$singular}Revision"; |
138
|
|
|
|
139
|
|
|
$capsule['repository'] = "{$repositories}\\{$singular}Repository"; |
140
|
|
|
|
141
|
|
|
$capsule['repositories_dir'] = $this->namespaceToPath( |
142
|
|
|
$capsule, |
143
|
|
|
$repositories |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$capsule['controller'] = "{$controllers}\\{$singular}Controller"; |
147
|
|
|
|
148
|
|
|
$capsule['controllers_dir'] = $this->namespaceToPath( |
149
|
|
|
$capsule, |
150
|
|
|
$controllers |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$capsule['formRequest'] = "{$requests}\\{$singular}Request"; |
154
|
|
|
|
155
|
|
|
$capsule['requests_dir'] = $this->namespaceToPath($capsule, $requests); |
156
|
|
|
|
157
|
|
|
$this->registerPsr4Autoloader($capsule); |
158
|
|
|
|
159
|
|
|
return $capsule; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function registerPsr4Autoloader($capsule) |
163
|
|
|
{ |
164
|
|
|
$this->getAutoloader()->setPsr4( |
|
|
|
|
165
|
|
|
$capsule['namespace'] . '\\', |
166
|
|
|
$capsule['psr4_path'] |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$this->getAutoloader()->setPsr4( |
170
|
|
|
$capsule['database_namespace'] . '\\', |
171
|
|
|
$capsule['database_psr4_path'] |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$this->getAutoloader()->setPsr4( |
175
|
|
|
$capsule['database_namespace'] . '\\Seeds\\', |
176
|
|
|
$capsule['database_psr4_path'] . '/seeds' |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function capsuleRootPath($capsule) |
181
|
|
|
{ |
182
|
|
|
return config('twill.capsules.path') . '/' . $capsule['name'] ?? null; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function getCapsuleRepositoryClass($model) |
186
|
|
|
{ |
187
|
|
|
return $this->getCapsuleByModel($model)['repository'] ?? null; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getCapsuleTranslationClass($model) |
191
|
|
|
{ |
192
|
|
|
return $this->getCapsuleByModel($model)['translation'] ?? null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function getCapsuleSlugClass($model) |
196
|
|
|
{ |
197
|
|
|
return $this->getCapsuleByModel($model)['slug'] ?? null; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function getCapsuleRevisionClass($model) |
201
|
|
|
{ |
202
|
|
|
return $this->getCapsuleByModel($model)['revision'] ?? null; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getCapsuleFormRequestClass($model) |
206
|
|
|
{ |
207
|
|
|
return $this->getCapsuleByModel($model)['formRequest'] ?? null; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getCapsuleViewPrefix($capsule) |
211
|
|
|
{ |
212
|
|
|
return $this->getCapsuleByModule(Str::studly($capsule))[ |
213
|
|
|
'view_prefix' |
214
|
|
|
] ?? null; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function namespaceToPath($capsule, $namespace) |
218
|
|
|
{ |
219
|
|
|
return $this->capsuleNamespaceToPath( |
220
|
|
|
$namespace, |
221
|
|
|
$capsule['namespace'], |
222
|
|
|
$capsule['root_path'] |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function capsuleNamespaceToPath( |
227
|
|
|
$namespace, |
228
|
|
|
$capsuleNamespace, |
229
|
|
|
$rootPath |
230
|
|
|
) { |
231
|
|
|
$namespace = Str::after($namespace, $capsuleNamespace . '\\'); |
232
|
|
|
|
233
|
|
|
$subdir = $this->getCapsulesSubdir(); |
234
|
|
|
|
235
|
|
|
$subdir = filled($subdir) ? "{$subdir}/" : ''; |
236
|
|
|
|
237
|
|
|
return "{$rootPath}/{$subdir}" . str_replace('\\', '/', $namespace); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function getManager() |
241
|
|
|
{ |
242
|
|
|
return $this->manager = $this->manager ?? app('twill.capsules.manager'); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function seedCapsules($illuminateSeeder) |
246
|
|
|
{ |
247
|
|
|
$twillSeeder = app(CapsuleSeeder::class); |
248
|
|
|
|
249
|
|
|
$this->getCapsuleList()->each(function ($capsule) use ($twillSeeder, $illuminateSeeder) { |
250
|
|
|
if (filled($capsuleSeeder = $this->makeCapsuleSeeder($capsule))) { |
251
|
|
|
$twillSeeder->setCommand($illuminateSeeder->command); |
252
|
|
|
|
253
|
|
|
$twillSeeder->call($capsuleSeeder); |
254
|
|
|
} |
255
|
|
|
}); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function makeCapsuleSeeder($capsule) |
259
|
|
|
{ |
260
|
|
|
$seeder = "{$capsule['database_namespace']}\\Seeds\DatabaseSeeder"; |
261
|
|
|
|
262
|
|
|
if (class_exists($seeder)) { |
263
|
|
|
return $seeder; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return null; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function capsuleExists($module) |
270
|
|
|
{ |
271
|
|
|
return filled($this->getCapsuleByModule($module)); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|