1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use KodiComponents\Support\HtmlAttributes; |
7
|
|
|
use SleepingOwl\Admin\Contracts\ActionInterface; |
8
|
|
|
use SleepingOwl\Admin\Contracts\Display\DisplayExtensionInterface; |
9
|
|
|
use SleepingOwl\Admin\Contracts\Display\Placable; |
10
|
|
|
use SleepingOwl\Admin\Contracts\DisplayInterface; |
11
|
|
|
use SleepingOwl\Admin\Contracts\FilterInterface; |
12
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
13
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
14
|
|
|
use SleepingOwl\Admin\Contracts\RepositoryInterface; |
15
|
|
|
use SleepingOwl\Admin\Display\Extension\Actions; |
16
|
|
|
use SleepingOwl\Admin\Display\Extension\Apply; |
17
|
|
|
use SleepingOwl\Admin\Display\Extension\Filters; |
18
|
|
|
use SleepingOwl\Admin\Display\Extension\Scopes; |
19
|
|
|
use SleepingOwl\Admin\Traits\Assets; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Display. |
23
|
|
|
* |
24
|
|
|
* @method Actions getActions() |
25
|
|
|
* @method $this setActions(ActionInterface $action, ...$actions) |
26
|
|
|
* |
27
|
|
|
* @method Filters getFilters() |
28
|
|
|
* @method $this setFilters(FilterInterface $filter, ...$filters) |
29
|
|
|
* |
30
|
|
|
* @method Apply getApply() |
31
|
|
|
* @method $this setApply(\Closure $apply, ...$applies) |
32
|
|
|
* |
33
|
|
|
* @method Scopes getScopes() |
34
|
|
|
* @method $this setScopes(array $scope, ...$scopes) |
35
|
|
|
*/ |
36
|
|
|
abstract class Display implements DisplayInterface |
37
|
|
|
{ |
38
|
|
|
use HtmlAttributes, Assets; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string|\Illuminate\View\View |
42
|
|
|
*/ |
43
|
|
|
protected $view; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $modelClass; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $with = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $title; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $repositoryClass = RepositoryInterface::class; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var RepositoryInterface |
67
|
|
|
*/ |
68
|
|
|
protected $repository; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var DisplayExtensionInterface[]|Collection |
72
|
|
|
*/ |
73
|
|
|
protected $extensions; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var bool |
77
|
|
|
*/ |
78
|
|
|
protected $initialized = false; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Display constructor. |
82
|
|
|
*/ |
83
|
|
|
public function __construct() |
84
|
|
|
{ |
85
|
|
|
$this->extensions = new Collection(); |
86
|
|
|
|
87
|
|
|
$this->extend('actions', new Actions()); |
88
|
|
|
$this->extend('filters', new Filters()); |
89
|
|
|
$this->extend('apply', new Apply()); |
90
|
|
|
$this->extend('scopes', new Scopes()); |
91
|
|
|
|
92
|
|
|
$this->initializePackage(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function isInitialized() |
99
|
|
|
{ |
100
|
|
|
return $this->initialized; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $name |
105
|
|
|
* @param DisplayExtensionInterface $extension |
106
|
|
|
* |
107
|
|
|
* @return DisplayExtensionInterface |
108
|
|
|
*/ |
109
|
|
|
public function extend($name, DisplayExtensionInterface $extension) |
110
|
|
|
{ |
111
|
|
|
$this->extensions->put($name, $extension); |
112
|
|
|
|
113
|
|
|
$extension->setDisplay($this); |
114
|
|
|
|
115
|
|
|
return $extension; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return Collection|\SleepingOwl\Admin\Contracts\Display\DisplayExtensionInterface[] |
120
|
|
|
*/ |
121
|
|
|
public function getExtensions() |
122
|
|
|
{ |
123
|
|
|
return $this->extensions; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return RepositoryInterface |
128
|
|
|
*/ |
129
|
|
|
public function getRepository() |
130
|
|
|
{ |
131
|
|
|
return $this->repository; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $repositoryClass |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function setRepositoryClass($repositoryClass) |
140
|
|
|
{ |
141
|
|
|
$this->repositoryClass = $repositoryClass; |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param array|string[] ...$relations |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function with($relations) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$this->with = array_flatten(func_get_args()); |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
public function initialize() |
162
|
|
|
{ |
163
|
|
|
if ($this->isInitialized()) { |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->repository = $this->makeRepository(); |
168
|
|
|
$this->repository->with($this->with); |
169
|
|
|
|
170
|
|
|
$this->extensions->each(function (DisplayExtensionInterface $extension) { |
171
|
|
|
if ($extension instanceof Initializable) { |
172
|
|
|
$extension->initialize(); |
173
|
|
|
} |
174
|
|
|
}); |
175
|
|
|
|
176
|
|
|
$this->includePackage(); |
177
|
|
|
|
178
|
|
|
$this->initialized = true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $modelClass |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function setModelClass($modelClass) |
187
|
|
|
{ |
188
|
|
|
if (is_null($this->modelClass)) { |
189
|
|
|
$this->modelClass = $modelClass; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getTitle() |
199
|
|
|
{ |
200
|
|
|
$titles = [ |
201
|
|
|
$this->title, |
202
|
|
|
]; |
203
|
|
|
|
204
|
|
|
$this->getExtensions()->each(function (DisplayExtensionInterface $extension) use (&$titles) { |
205
|
|
|
if (method_exists($extension, $method = 'getTitle')) { |
206
|
|
|
$titles[] = call_user_func([$extension, $method]); |
207
|
|
|
} |
208
|
|
|
}); |
209
|
|
|
|
210
|
|
|
return implode(' | ', array_filter($titles)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $title |
215
|
|
|
* |
216
|
|
|
* @return $this |
217
|
|
|
*/ |
218
|
|
|
public function setTitle($title) |
219
|
|
|
{ |
220
|
|
|
$this->title = $title; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get the instance as an array. |
227
|
|
|
* |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
|
|
public function toArray() |
231
|
|
|
{ |
232
|
|
|
return [ |
233
|
|
|
'title' => $this->getTitle(), |
234
|
|
|
'extensions' => $this->getExtensions()->toArray(), |
235
|
|
|
'attributes' => $this->htmlAttributesToString(), |
236
|
|
|
]; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
|
|
public function getView() |
243
|
|
|
{ |
244
|
|
|
return $this->view; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param string|\Illuminate\View\View $view |
249
|
|
|
* |
250
|
|
|
* @return $this |
251
|
|
|
*/ |
252
|
|
|
public function setView($view) |
253
|
|
|
{ |
254
|
|
|
$this->view = $view; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get the evaluated contents of the object. |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
public function render() |
265
|
|
|
{ |
266
|
|
|
$view = app('sleeping_owl.template')->view($this->getView(), $this->toArray()); |
267
|
|
|
|
268
|
|
|
$this->getExtensions()->filter(function($extension) { |
269
|
|
|
return $extension instanceof Placable; |
270
|
|
|
})->each(function($extension) use($view) { |
271
|
|
|
$html = app('sleeping_owl.template')->view($extension->getView(), $extension->toArray())->render(); |
272
|
|
|
|
273
|
|
|
if (! empty($html)) { |
274
|
|
|
$factory = $view->getFactory(); |
275
|
|
|
|
276
|
|
|
$factory->startSection($extension->getPlacement()); |
277
|
|
|
echo $html; |
278
|
|
|
$factory->stopSection(true); |
279
|
|
|
} |
280
|
|
|
}); |
281
|
|
|
|
282
|
|
|
return $view; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
public function __toString() |
289
|
|
|
{ |
290
|
|
|
return (string) $this->render(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param string $name |
295
|
|
|
* @param array $arguments |
296
|
|
|
* |
297
|
|
|
* @return DisplayExtensionInterface |
298
|
|
|
*/ |
299
|
|
|
public function __call($name, $arguments) |
300
|
|
|
{ |
301
|
|
|
$method = snake_case(substr($name, 3)); |
302
|
|
|
|
303
|
|
|
if (starts_with($name, 'get') and $this->extensions->has($method)) { |
|
|
|
|
304
|
|
|
return $this->extensions->get($method); |
305
|
|
|
} elseif (starts_with($name, 'set') and $this->extensions->has($method)) { |
|
|
|
|
306
|
|
|
$extension = $this->extensions->get($method); |
307
|
|
|
|
308
|
|
|
if (method_exists($extension, 'set')) { |
309
|
|
|
return call_user_func_array([$extension, 'set'], $arguments); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
throw new \BadMethodCallException("Call to undefined method [{$name}]"); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @return ModelConfigurationInterface |
318
|
|
|
*/ |
319
|
|
|
protected function getModelConfiguration() |
320
|
|
|
{ |
321
|
|
|
return app('sleeping_owl')->getModel($this->modelClass); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return \Illuminate\Foundation\Application|mixed |
326
|
|
|
* @throws \Exception |
327
|
|
|
*/ |
328
|
|
|
protected function makeRepository() |
329
|
|
|
{ |
330
|
|
|
$repository = app($this->repositoryClass, [$this->modelClass]); |
331
|
|
|
|
332
|
|
|
if (! ($repository instanceof RepositoryInterface)) { |
333
|
|
|
throw new \Exception('Repository class must be instanced of [RepositoryInterface]'); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $repository; |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.