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 = config('twill.capsules.path'); |
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', $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
|
|
|
public function makeCapsule($capsule, $basePath) |
50
|
|
|
{ |
51
|
|
|
$capsule['name'] = Str::studly($capsule['name']); |
52
|
|
|
|
53
|
|
|
$capsule['module'] = Str::camel($capsule['name']); |
54
|
|
|
|
55
|
|
|
$capsule['plural'] = $name = $capsule['name']; |
56
|
|
|
|
57
|
|
|
$capsule['singular'] = $singular = |
58
|
|
|
$capsule['singular'] ?? Str::singular($name); |
59
|
|
|
|
60
|
|
|
$twillNamespace = config('twill.namespace'); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$capsule[ |
63
|
|
|
'namespace' |
64
|
|
|
] = $capsuleNamespace = $this->getManager()->capsuleNamespace( |
|
|
|
|
65
|
|
|
$capsule['name'] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$capsule['model'] = $capsule['models'] = $models = |
69
|
|
|
"{$capsuleNamespace}\\" . |
70
|
|
|
config('twill.capsules.namespaces.models'); |
71
|
|
|
$capsule['repositories'] = $repositories = |
72
|
|
|
"{$capsuleNamespace}\\" . |
73
|
|
|
config('twill.capsules.namespaces.repositories'); |
74
|
|
|
$capsule['controllers'] = $controllers = |
75
|
|
|
"{$capsuleNamespace}\\" . |
76
|
|
|
config('twill.capsules.namespaces.controllers'); |
77
|
|
|
$capsule['requests'] = $requests = |
78
|
|
|
"{$capsuleNamespace}\\" . |
79
|
|
|
config('twill.capsules.namespaces.requests'); |
80
|
|
|
|
81
|
|
|
$capsule['psr4_path'] = "$basePath/{$name}/app"; |
82
|
|
|
|
83
|
|
|
$capsule['root_path'] = $root = $this->capsuleRootPath($capsule); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$capsule[ |
86
|
|
|
'migrations_dir' |
87
|
|
|
] = "{$capsule['root_path']}/database/migrations"; |
88
|
|
|
|
89
|
|
|
$capsule['views_dir'] = "{$capsule['root_path']}/resources/views"; |
90
|
|
|
|
91
|
|
|
$capsule['view_prefix'] = "{$name}.resources.views.admin"; |
92
|
|
|
|
93
|
|
|
$capsule['routes_file'] = "{$capsule['root_path']}/routes/admin.php"; |
94
|
|
|
|
95
|
|
|
$capsule['model'] = "{$models}\\{$singular}"; |
96
|
|
|
|
97
|
|
|
$capsule['models_dir'] = $this->namespaceToPath($capsule, $models); |
98
|
|
|
|
99
|
|
|
$capsule['translation'] = "{$models}\\{$singular}Translation"; |
100
|
|
|
|
101
|
|
|
$capsule['slug'] = "{$models}\\{$singular}Slug"; |
102
|
|
|
|
103
|
|
|
$capsule['revision'] = "{$models}\\{$singular}Revision"; |
104
|
|
|
|
105
|
|
|
$capsule['repository'] = "{$repositories}\\{$singular}Repository"; |
106
|
|
|
|
107
|
|
|
$capsule['repositories_dir'] = $this->namespaceToPath( |
108
|
|
|
$capsule, |
109
|
|
|
$repositories |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$capsule['controller'] = "{$controllers}\\{$singular}Controller"; |
113
|
|
|
|
114
|
|
|
$capsule['controllers_dir'] = $this->namespaceToPath( |
115
|
|
|
$capsule, |
116
|
|
|
$controllers |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$capsule['formRequest'] = "{$requests}\\{$singular}Request"; |
120
|
|
|
|
121
|
|
|
$capsule['requests_dir'] = $this->namespaceToPath($capsule, $requests); |
122
|
|
|
|
123
|
|
|
$this->registerPsr4Autoloader($capsule); |
124
|
|
|
|
125
|
|
|
return $capsule; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function registerPsr4Autoloader($capsule) |
129
|
|
|
{ |
130
|
|
|
$this->getAutoloader()->setPsr4( |
|
|
|
|
131
|
|
|
$capsule['namespace'] . '\\', |
132
|
|
|
$capsule['psr4_path'] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function capsuleRootPath($capsule) |
137
|
|
|
{ |
138
|
|
|
return config('twill.capsules.path') . '/' . $capsule['name'] ?? null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getCapsuleRepositoryClass($model) |
142
|
|
|
{ |
143
|
|
|
return $this->getCapsuleByModel($model)['repository'] ?? null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getCapsuleTranslationClass($model) |
147
|
|
|
{ |
148
|
|
|
return $this->getCapsuleByModel($model)['translation'] ?? null; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getCapsuleSlugClass($model) |
152
|
|
|
{ |
153
|
|
|
return $this->getCapsuleByModel($model)['slug'] ?? null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getCapsuleRevisionClass($model) |
157
|
|
|
{ |
158
|
|
|
return $this->getCapsuleByModel($model)['revision'] ?? null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getCapsuleFormRequestClass($model) |
162
|
|
|
{ |
163
|
|
|
return $this->getCapsuleByModel($model)['formRequest'] ?? null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getCapsuleViewPrefix($capsule) |
167
|
|
|
{ |
168
|
|
|
return $this->getCapsuleByModule(Str::studly($capsule))[ |
169
|
|
|
'view_prefix' |
170
|
|
|
] ?? null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function namespaceToPath($capsule, $namespace) |
174
|
|
|
{ |
175
|
|
|
return $this->capsuleNamespaceToPath( |
176
|
|
|
$namespace, |
177
|
|
|
$capsule['namespace'], |
178
|
|
|
$capsule['root_path'] |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function capsuleNamespaceToPath( |
183
|
|
|
$namespace, |
184
|
|
|
$capsuleNamespace, |
185
|
|
|
$rootPath |
186
|
|
|
) { |
187
|
|
|
$namespace = Str::after($namespace, $capsuleNamespace . '\\'); |
188
|
|
|
|
189
|
|
|
$subdir = config('twill.capsules.namespaces.subdir'); |
190
|
|
|
|
191
|
|
|
$subdir = filled($subdir) ? "{$subdir}/" : ''; |
192
|
|
|
|
193
|
|
|
return "{$rootPath}/{$subdir}" . str_replace('\\', '/', $namespace); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getManager() |
197
|
|
|
{ |
198
|
|
|
return $this->manager = $this->manager ?? app('twill.capsules.manager'); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|