1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Helpers; |
4
|
|
|
|
5
|
|
|
use A17\Twill\Facades\TwillRoutes; |
6
|
|
|
use Illuminate\Contracts\Translation\Translator; |
7
|
|
|
use Illuminate\Database\Migrations\Migrator; |
8
|
|
|
use Illuminate\Support\Facades\App; |
9
|
|
|
use Illuminate\Support\Facades\Config; |
10
|
|
|
use Illuminate\Support\Facades\File; |
11
|
|
|
use Illuminate\Support\Facades\View; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
|
14
|
|
|
class Capsule |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $path; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
public $enabled; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
public $packageCapsule = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
public $loaded = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
private $singular; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $namespace; |
50
|
|
|
|
51
|
|
|
public function __construct( |
52
|
|
|
string $name, |
53
|
|
|
string $namespace, |
54
|
|
|
string $path, |
55
|
|
|
string $singular = null, |
56
|
|
|
bool $enabled = true, |
57
|
|
|
bool $packageCapsule = false |
58
|
|
|
) { |
59
|
|
|
$this->name = $name; |
60
|
|
|
$this->path = $path; |
61
|
|
|
$this->enabled = $enabled; |
62
|
|
|
$this->namespace = $namespace; |
63
|
|
|
$this->singular = $singular; |
64
|
|
|
$this->packageCapsule = $packageCapsule; |
65
|
|
|
|
66
|
|
|
$this->boot(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function boot(): void |
70
|
|
|
{ |
71
|
|
|
$this->autoloadConfigFiles(); |
72
|
|
|
$this->registerServiceProvider(); |
73
|
|
|
$this->registerViews(); |
74
|
|
|
$this->loadMigrations(); |
75
|
|
|
|
76
|
|
|
if ($this->packageCapsule) { |
77
|
|
|
$this->registerConfig(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->registerRoutes(); |
81
|
|
|
$this->loadTranslations(); |
82
|
|
|
|
83
|
|
|
$this->loaded = true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function registerServiceProvider(): void |
87
|
|
|
{ |
88
|
|
|
$serviceProviderName = $this->name . 'CapsuleServiceProvider'; |
89
|
|
|
|
90
|
|
|
if (File::exists($this->path . '/' . $serviceProviderName . '.php')) { |
91
|
|
|
// @todo: test. |
92
|
|
|
App::register($this->namespace . '\\' . $serviceProviderName); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function autoloadConfigFiles(): void |
97
|
|
|
{ |
98
|
|
|
$files = $this->getConfig()['autoload']['files'] ?? null; |
99
|
|
|
|
100
|
|
|
if (blank($files)) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
collect($files)->each(function ($file) { |
105
|
|
|
if (file_exists($file)) { |
106
|
|
|
require_once $file; |
107
|
|
|
} |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function registerViews(): void |
112
|
|
|
{ |
113
|
|
|
View::addLocation(Str::replaceLast('/' . $this->name, '', $this->path)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function loadMigrations(): void |
117
|
|
|
{ |
118
|
|
|
$callback = function (Migrator $migrator) { |
119
|
|
|
$migrator->path($this->getMigrationsPath()); |
120
|
|
|
}; |
121
|
|
|
|
122
|
|
|
App()->afterResolving('migrator', $callback); |
123
|
|
|
|
124
|
|
|
if (app()->resolved('migrator')) { |
125
|
|
|
$callback(App::make('migrator')); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function registerRoutes(): void { |
130
|
|
|
TwillRoutes::registerCapsuleRoutes(App::get('router'), $this); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function loadTranslations(): void { |
134
|
|
|
$callback = function (Translator $translator) { |
135
|
|
|
$translator->addNamespace($this->getLanguagesPath(), 'twill:capsules:' . $this->getModule()); |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
App()->afterResolving('translator', $callback); |
139
|
|
|
|
140
|
|
|
if (app()->resolved('translator')) { |
141
|
|
|
$callback(App::make('translator')); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getBasePath(string $path): string |
146
|
|
|
{ |
147
|
|
|
$exploded = explode('/', $path); |
148
|
|
|
return implode('/', array_pop($exploded)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getModule(): string |
152
|
|
|
{ |
153
|
|
|
return Str::camel($this->name); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getDisplayName(): string |
157
|
|
|
{ |
158
|
|
|
return Str::studly($this->name); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getPlural(): string |
162
|
|
|
{ |
163
|
|
|
return $this->name; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getSingular(): string |
167
|
|
|
{ |
168
|
|
|
return $this->singular ?? Str::singular($this->name); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getBaseNamespace(): string |
172
|
|
|
{ |
173
|
|
|
$explodedNamespace = explode('\\', $this->namespace); |
174
|
|
|
return implode('\\', array_pop($explodedNamespace)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getDatabaseNamespace(): string |
178
|
|
|
{ |
179
|
|
|
return $this->namespace . '\\Database'; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function getDatabasePsr4Path(): string |
183
|
|
|
{ |
184
|
|
|
return $this->path . '/database'; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function getSeedsNamespace(): string |
188
|
|
|
{ |
189
|
|
|
return $this->namespace . '\\Seeds\\Database'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getSeedsPsr4Path(): string |
193
|
|
|
{ |
194
|
|
|
return $this->getDatabasePsr4Path() . '/seeds'; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getMigrationsPath(): string |
198
|
|
|
{ |
199
|
|
|
return $this->getDatabasePsr4Path() . '/migrations'; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getResourcesPath(): string |
203
|
|
|
{ |
204
|
|
|
return $this->getPsr4Path() . '/resources'; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function getLanguagesPath(): string |
208
|
|
|
{ |
209
|
|
|
return $this->getResourcesPath() . '/lang'; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getViewsPath(): string |
213
|
|
|
{ |
214
|
|
|
return $this->getResourcesPath() . '/views'; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function getModelNamespace(): string |
218
|
|
|
{ |
219
|
|
|
// @todo: config('twill.capsules.namespaces.models'); |
220
|
|
|
return $this->namespace . '\\Models'; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function getModelsDir(): string |
224
|
|
|
{ |
225
|
|
|
return $this->getPsr4Path() . '/Models'; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function getRepositoriesNamespace(): string |
229
|
|
|
{ |
230
|
|
|
// @todo: config('twill.capsules.namespaces.repositories'); |
231
|
|
|
return $this->namespace . '\\Repositories'; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function getRepositoriesDir(): string |
235
|
|
|
{ |
236
|
|
|
return $this->getPsr4Path() . '/Repositories'; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function getControllersNamespace(): string |
240
|
|
|
{ |
241
|
|
|
// @todo: config('twill.capsules.namespaces.controllers'); |
242
|
|
|
return $this->namespace . '\\Http\\Controllers'; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function getControllersDir(): string |
246
|
|
|
{ |
247
|
|
|
return $this->getPsr4Path() . '/Http/Controllers'; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function getRequestsNamespace(): string |
251
|
|
|
{ |
252
|
|
|
// @todo: config('twill.capsules.namespaces.requests'); |
253
|
|
|
return $this->namespace . '\\Http\\Requests'; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function getRequestsDir(): string |
257
|
|
|
{ |
258
|
|
|
return $this->getPsr4Path() . '/Http/Requests'; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function getPsr4Path(): string |
262
|
|
|
{ |
263
|
|
|
// @todo: config('twill.capsules.namespaces.subdir'); |
264
|
|
|
return $this->path; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function getViewPrefix(): string |
268
|
|
|
{ |
269
|
|
|
return "{$this->getModule()}.resources.views.admin"; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function getRoutesFile(): string |
273
|
|
|
{ |
274
|
|
|
return $this->getPsr4Path() . '/routes/admin.php'; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function routesFileExists(): bool |
278
|
|
|
{ |
279
|
|
|
return file_exists($this->getRoutesFile()); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function getModel(): string |
283
|
|
|
{ |
284
|
|
|
return $this->getModelNamespace() . '\\' . $this->getSingular(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function getTranslationModel(): string |
288
|
|
|
{ |
289
|
|
|
return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Translation'; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function getSlugModel(): string |
293
|
|
|
{ |
294
|
|
|
return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Slug'; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function getRevisionModel(): string |
298
|
|
|
{ |
299
|
|
|
return $this->getModelNamespace() . '\\' . $this->getSingular() . 'Revision'; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function getRepositoryClass(): string |
303
|
|
|
{ |
304
|
|
|
return $this->getRepositoriesNamespace() . '\\' . $this->getSingular() . 'Repository'; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function getControllerClass(): string |
308
|
|
|
{ |
309
|
|
|
return $this->getControllersNamespace() . '\\' . $this->getSingular() . 'Controller'; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function getFormRequestClass(): string |
313
|
|
|
{ |
314
|
|
|
return $this->getRequestsNamespace() . '\\' . $this->getSingular() . 'Request'; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function getConfigFile(): string |
318
|
|
|
{ |
319
|
|
|
return $this->path . '/config.php'; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function getConfig(): array |
323
|
|
|
{ |
324
|
|
|
if (file_exists($this->getConfigFile())) { |
325
|
|
|
return require $this->getConfigFile(); |
326
|
|
|
} |
327
|
|
|
return []; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function registerConfig(): void |
331
|
|
|
{ |
332
|
|
|
$config = Config::get('twill-navigation', []); |
333
|
|
|
|
334
|
|
|
$config[$this->name] = [ |
335
|
|
|
'title' => $this->name, |
336
|
|
|
'module' => true, |
337
|
|
|
]; |
338
|
|
|
|
339
|
|
|
Config::set('twill-navigation', $config); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
public function getType(): string { |
343
|
|
|
return ''; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|