1 | <?php |
||||
2 | |||||
3 | namespace SleepingOwl\Admin\Display; |
||||
4 | |||||
5 | use Exception; |
||||
6 | use Illuminate\Support\Arr; |
||||
7 | use Illuminate\Support\Str; |
||||
8 | use KodiComponents\Support\HtmlAttributes; |
||||
9 | use SleepingOwl\Admin\Contracts\Display\DisplayExtensionInterface; |
||||
10 | use SleepingOwl\Admin\Contracts\Display\DisplayInterface; |
||||
11 | use SleepingOwl\Admin\Contracts\Display\Extension\ActionInterface; |
||||
12 | use SleepingOwl\Admin\Contracts\Display\Extension\FilterInterface; |
||||
13 | use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
||||
14 | use SleepingOwl\Admin\Contracts\Repositories\RepositoryInterface; |
||||
15 | use SleepingOwl\Admin\Display\Extension\Actions; |
||||
16 | use SleepingOwl\Admin\Display\Extension\ActionsForm; |
||||
17 | use SleepingOwl\Admin\Display\Extension\Apply; |
||||
18 | use SleepingOwl\Admin\Display\Extension\Filters; |
||||
19 | use SleepingOwl\Admin\Display\Extension\Links; |
||||
20 | use SleepingOwl\Admin\Display\Extension\Scopes; |
||||
21 | use SleepingOwl\Admin\Form\FormElements; |
||||
22 | use SleepingOwl\Admin\Traits\Assets; |
||||
23 | use SleepingOwl\Admin\Traits\Renderable; |
||||
24 | |||||
25 | /** |
||||
26 | * Class Display. |
||||
27 | * |
||||
28 | * @method Actions getActions() |
||||
29 | * @method $this setActions(ActionInterface|array $action, ...$actions) |
||||
30 | * |
||||
31 | * @method ActionsForm getActionsForm() |
||||
32 | * @method $this setActionsForm(ActionInterface|array|FormElements $action, ...$actions) |
||||
33 | * |
||||
34 | * @method Filters getFilters() |
||||
35 | * @method $this setFilters(FilterInterface $filter, ...$filters) |
||||
36 | * |
||||
37 | * @method Apply getApply() |
||||
38 | * @method $this setApply(\Closure $apply, ...$applies) |
||||
39 | * |
||||
40 | * @method Scopes getScopes() |
||||
41 | * @method $this setScopes(array $scope, ...$scopes) |
||||
42 | */ |
||||
43 | abstract class Display implements DisplayInterface |
||||
44 | { |
||||
45 | use HtmlAttributes, Assets, Renderable; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
46 | |||||
47 | /** |
||||
48 | * @var string |
||||
49 | */ |
||||
50 | protected $modelClass; |
||||
51 | |||||
52 | /** |
||||
53 | * @var array |
||||
54 | */ |
||||
55 | protected $with = []; |
||||
56 | |||||
57 | /** |
||||
58 | * @var string |
||||
59 | */ |
||||
60 | protected $title; |
||||
61 | |||||
62 | /** |
||||
63 | * @var string |
||||
64 | */ |
||||
65 | protected $repositoryClass = RepositoryInterface::class; |
||||
66 | |||||
67 | /** |
||||
68 | * @var RepositoryInterface |
||||
69 | */ |
||||
70 | protected $repository; |
||||
71 | |||||
72 | /** |
||||
73 | * @var DisplayExtensionInterface[]|ExtensionCollection |
||||
74 | */ |
||||
75 | protected $extensions; |
||||
76 | |||||
77 | /** |
||||
78 | * @var bool |
||||
79 | */ |
||||
80 | protected $initialized = false; |
||||
81 | |||||
82 | /** |
||||
83 | * Display constructor. |
||||
84 | */ |
||||
85 | public function __construct() |
||||
86 | { |
||||
87 | $this->extensions = new ExtensionCollection(); |
||||
88 | |||||
89 | $this->extend('actions_form', new ActionsForm()); |
||||
90 | $this->extend('actions', new Actions()); |
||||
91 | $this->extend('filters', new Filters()); |
||||
92 | $this->extend('apply', new Apply()); |
||||
93 | $this->extend('scopes', new Scopes()); |
||||
94 | $this->extend('links', new Links()); |
||||
95 | |||||
96 | $this->initializePackage(); |
||||
97 | } |
||||
98 | |||||
99 | /** |
||||
100 | * @return bool |
||||
101 | */ |
||||
102 | public function isInitialized() |
||||
103 | { |
||||
104 | return $this->initialized; |
||||
105 | } |
||||
106 | |||||
107 | /** |
||||
108 | * @param string $name |
||||
109 | * @param DisplayExtensionInterface $extension |
||||
110 | * |
||||
111 | * @return DisplayExtensionInterface |
||||
112 | */ |
||||
113 | public function extend($name, DisplayExtensionInterface $extension) |
||||
114 | { |
||||
115 | $this->extensions->put($name, $extension); |
||||
116 | |||||
117 | $extension->setDisplay($this); |
||||
118 | |||||
119 | return $extension; |
||||
120 | } |
||||
121 | |||||
122 | /** |
||||
123 | * @return ExtensionCollection|DisplayExtensionInterface[] |
||||
124 | */ |
||||
125 | public function getExtensions() |
||||
126 | { |
||||
127 | return $this->extensions; |
||||
128 | } |
||||
129 | |||||
130 | /** |
||||
131 | * @return RepositoryInterface |
||||
132 | */ |
||||
133 | public function getRepository() |
||||
134 | { |
||||
135 | return $this->repository; |
||||
136 | } |
||||
137 | |||||
138 | /** |
||||
139 | * @param $repositoryClass |
||||
140 | * |
||||
141 | * @return $this |
||||
142 | */ |
||||
143 | public function setRepositoryClass($repositoryClass) |
||||
144 | { |
||||
145 | $this->repositoryClass = $repositoryClass; |
||||
146 | |||||
147 | return $this; |
||||
148 | } |
||||
149 | |||||
150 | /** |
||||
151 | * @param array|string[] ...$relations |
||||
152 | * |
||||
153 | * @return $this |
||||
154 | */ |
||||
155 | public function with($relations) |
||||
156 | { |
||||
157 | $this->with = Arr::flatten(func_get_args()); |
||||
158 | |||||
159 | return $this; |
||||
160 | } |
||||
161 | |||||
162 | /** |
||||
163 | * @throws \Exception |
||||
164 | */ |
||||
165 | public function initialize() |
||||
166 | { |
||||
167 | if ($this->isInitialized()) { |
||||
168 | return; |
||||
169 | } |
||||
170 | |||||
171 | $this->repository = $this->makeRepository(); |
||||
172 | $this->repository->with($this->with); |
||||
173 | |||||
174 | $this->extensions->initialize(); |
||||
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)) { |
||||
0 ignored issues
–
show
|
|||||
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 | * Get the evaluated contents of the object. |
||||
241 | * |
||||
242 | * @return string |
||||
243 | */ |
||||
244 | public function render() |
||||
245 | { |
||||
246 | $view = app('sleeping_owl.template')->view($this->getView(), $this->toArray()); |
||||
0 ignored issues
–
show
The method
view() does not exist on Illuminate\Contracts\Foundation\Application .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
247 | |||||
248 | $blocks = $this->getExtensions()->placableBlocks(); |
||||
249 | |||||
250 | // Flush all view yields before render new Section |
||||
251 | $view->getFactory()->flushSections(); |
||||
252 | |||||
253 | foreach ($blocks as $block => $data) { |
||||
254 | foreach ($data as $html) { |
||||
255 | if (! empty($html)) { |
||||
256 | $view->getFactory()->startSection($block); |
||||
257 | echo $html; |
||||
258 | $view->getFactory()->yieldSection(); |
||||
259 | } else { |
||||
0 ignored issues
–
show
This
else statement is empty and can be removed.
This check looks for the These if (rand(1, 6) > 3) {
print "Check failed";
} else {
//print "Check succeeded";
}
could be turned into if (rand(1, 6) > 3) {
print "Check failed";
}
This is much more concise to read. ![]() |
|||||
260 | //$view->getFactory()->flushSections(); |
||||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
80% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||||
261 | } |
||||
262 | } |
||||
263 | } |
||||
264 | |||||
265 | return $view; |
||||
266 | } |
||||
267 | |||||
268 | /** |
||||
269 | * @param string $name |
||||
270 | * @param array $arguments |
||||
271 | * |
||||
272 | * @return DisplayExtensionInterface |
||||
273 | */ |
||||
274 | public function __call($name, $arguments) |
||||
275 | { |
||||
276 | $method = Str::snake(substr($name, 3)); |
||||
277 | |||||
278 | if (Str::startsWith($name, 'get') && $this->extensions->has($method)) { |
||||
279 | return $this->extensions->get($method); |
||||
280 | } elseif (Str::startsWith($name, 'set') && $this->extensions->has($method)) { |
||||
281 | $extension = $this->extensions->get($method); |
||||
282 | |||||
283 | if (method_exists($extension, 'set')) { |
||||
284 | return call_user_func_array([$extension, 'set'], $arguments); |
||||
285 | } |
||||
286 | } |
||||
287 | |||||
288 | throw new \BadMethodCallException("Call to undefined method [{$name}]"); |
||||
289 | } |
||||
290 | |||||
291 | /** |
||||
292 | * @return ModelConfigurationInterface |
||||
293 | */ |
||||
294 | public function getModelConfiguration() |
||||
295 | { |
||||
296 | return app('sleeping_owl')->getModel($this->modelClass); |
||||
0 ignored issues
–
show
The method
getModel() does not exist on Illuminate\Contracts\Foundation\Application .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
297 | } |
||||
298 | |||||
299 | /** |
||||
300 | * @return \Illuminate\Foundation\Application|mixed |
||||
301 | * @throws \Exception |
||||
302 | */ |
||||
303 | protected function makeRepository() |
||||
304 | { |
||||
305 | $repository = app($this->repositoryClass); |
||||
306 | if (! ($repository instanceof RepositoryInterface)) { |
||||
307 | throw new Exception('Repository class must be instanced of [RepositoryInterface]'); |
||||
308 | } |
||||
309 | |||||
310 | $repository->setClass($this->modelClass); |
||||
311 | |||||
312 | return $repository; |
||||
313 | } |
||||
314 | } |
||||
315 |