1 | <?php |
||||
2 | /** |
||||
3 | * Created by PhpStorm. |
||||
4 | * User: Hamza Alayed |
||||
5 | * Date: 12/29/18 |
||||
6 | * Time: 9:53 AM. |
||||
7 | */ |
||||
8 | |||||
9 | namespace Shamaseen\Repository\Generator\Utility; |
||||
10 | |||||
11 | use App; |
||||
12 | use Config; |
||||
13 | use Exception; |
||||
14 | use Illuminate\Contracts\View\Factory; |
||||
15 | use Illuminate\Http\JsonResponse; |
||||
16 | use Illuminate\Http\RedirectResponse; |
||||
17 | use Illuminate\Http\Resources\Json\JsonResource; |
||||
0 ignored issues
–
show
|
|||||
18 | use Illuminate\Support\Collection; |
||||
19 | use Redirect; |
||||
20 | use View; |
||||
21 | |||||
22 | /** |
||||
23 | * Class BaseController. |
||||
24 | */ |
||||
25 | class Controller extends App\Http\Controllers\Controller |
||||
0 ignored issues
–
show
The type
App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
26 | { |
||||
27 | /** |
||||
28 | * @var ContractInterface |
||||
29 | */ |
||||
30 | protected $interface; |
||||
31 | |||||
32 | protected $limit = 10; |
||||
33 | protected $maxLimit = 100; |
||||
34 | |||||
35 | protected $routeIndex = ''; |
||||
36 | |||||
37 | protected $pageTitle = ''; |
||||
38 | protected $createRoute = ''; |
||||
39 | |||||
40 | protected $viewIndex = ''; |
||||
41 | protected $viewCreate = ''; |
||||
42 | protected $viewEdit = ''; |
||||
43 | protected $viewShow = ''; |
||||
44 | protected $breadcrumbs; |
||||
45 | |||||
46 | protected $menu; |
||||
47 | protected $search; |
||||
48 | protected $selectedMenu = []; |
||||
49 | protected $isAPI = false; |
||||
50 | protected $trash = false; |
||||
51 | protected $params = []; |
||||
52 | /** |
||||
53 | * @var Request |
||||
54 | */ |
||||
55 | protected $request; |
||||
56 | /** |
||||
57 | * @var JsonResource |
||||
58 | */ |
||||
59 | private $resource; |
||||
60 | |||||
61 | /** |
||||
62 | * BaseController constructor. |
||||
63 | * |
||||
64 | * @param ContractInterface $interface |
||||
65 | * @param Request $request |
||||
66 | * @param JsonResource|null $resource |
||||
67 | */ |
||||
68 | public function __construct(ContractInterface $interface, Request $request, JsonResource $resource = null) |
||||
69 | { |
||||
70 | $this->menu = new Collection(); |
||||
71 | $this->breadcrumbs = new Collection(); |
||||
72 | |||||
73 | $language = $request->header('Language', 'en'); |
||||
74 | if (!in_array($language, Config::get('app.locales', []))) { |
||||
75 | $language = 'en'; |
||||
76 | } |
||||
77 | $limit = $request->get('limit', 10); |
||||
78 | |||||
79 | if ($request->get('with-trash', false)) { |
||||
80 | $interface->withTrash(); |
||||
81 | } |
||||
82 | if ($request->get('only-trash', false)) { |
||||
83 | $interface->trash(); |
||||
84 | } |
||||
85 | |||||
86 | $request->offsetUnset('only-trash'); |
||||
87 | $request->offsetUnset('with-trash'); |
||||
88 | $request->offsetUnset('limit'); |
||||
89 | |||||
90 | if ($limit <= $this->maxLimit) { |
||||
91 | $this->limit = $limit; |
||||
92 | } |
||||
93 | |||||
94 | App::setLocale($language); |
||||
95 | switch ($language) { |
||||
96 | case 'ar': |
||||
97 | $dir = 'rtl'; |
||||
98 | $align = 'right'; |
||||
99 | $dirInverse = 'ltr'; |
||||
100 | $alignInverse = 'left'; |
||||
101 | break; |
||||
102 | |||||
103 | case 'en': |
||||
104 | default: |
||||
105 | $dir = 'ltr'; |
||||
106 | $align = 'left'; |
||||
107 | $dirInverse = 'rtl'; |
||||
108 | $alignInverse = 'right'; |
||||
109 | break; |
||||
110 | } |
||||
111 | |||||
112 | View::share('dir', $dir); |
||||
113 | View::share('align', $align); |
||||
114 | View::share('alignInverse', $alignInverse); |
||||
115 | View::share('dirInverse', $dirInverse); |
||||
116 | |||||
117 | $this->interface = $interface; |
||||
118 | $this->isAPI = $request->expectsJson(); |
||||
119 | |||||
120 | if (!$this->isAPI) { |
||||
121 | $this->breadcrumbs = new Collection(); |
||||
122 | $this->search = new Collection(); |
||||
123 | View::share('pageTitle', $this->pageTitle . ' | ' . Config::get('app.name')); |
||||
124 | View::share('breadcrumbs', $this->breadcrumbs); |
||||
125 | View::share('menu', $this->menu); |
||||
126 | View::share('search', $this->search); |
||||
127 | View::share('selectedMenu', $this->selectedMenu); |
||||
128 | } |
||||
129 | $this->resource = $resource; |
||||
130 | if (is_null($resource)) { |
||||
131 | $this->resource = new JsonResource([]); |
||||
132 | } |
||||
133 | |||||
134 | $this->request = $request; |
||||
135 | } |
||||
136 | |||||
137 | /** |
||||
138 | * Display a listing of the resource. |
||||
139 | * |
||||
140 | * |
||||
141 | * @return Factory|JsonResponse|\Illuminate\View\View |
||||
0 ignored issues
–
show
The type
Illuminate\View\View was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
142 | */ |
||||
143 | public function index() |
||||
144 | { |
||||
145 | $data = $this->interface->simplePaginate($this->limit, $this->request->all()); |
||||
146 | |||||
147 | if (!$this->isAPI) { |
||||
148 | View::share('pageTitle', 'List ' . $this->pageTitle . ' | ' . Config::get('app.name')); |
||||
149 | $this->breadcrumbs->put('index', [ |
||||
150 | 'link' => $this->routeIndex, |
||||
151 | 'text' => $this->pageTitle, |
||||
152 | ]); |
||||
153 | |||||
154 | return view($this->viewIndex, $this->params) |
||||
0 ignored issues
–
show
The function
view was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
155 | ->with('entities', $data) |
||||
156 | ->with('createRoute', $this->createRoute) |
||||
157 | ->with('filters', $this->request->all()); |
||||
158 | } |
||||
159 | |||||
160 | $resource = $this->resource::collection($data); |
||||
161 | if ($data->hasMorePages()) { |
||||
162 | $custom = collect([ |
||||
163 | 'code' => JsonResponse::HTTP_PARTIAL_CONTENT, |
||||
164 | 'message' => __('repository-generator.partial_content') |
||||
0 ignored issues
–
show
The function
__ was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
165 | ]); |
||||
166 | $resource = $custom->merge(['data' => $resource]); |
||||
167 | return response()->json($resource, JsonResponse::HTTP_PARTIAL_CONTENT); |
||||
0 ignored issues
–
show
The function
response was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
168 | } |
||||
169 | |||||
170 | if ($data->isEmpty()) { |
||||
171 | $custom = collect([ |
||||
172 | 'code' => JsonResponse::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE, |
||||
173 | 'message' => __('repository-generator.no_content') |
||||
174 | ]); |
||||
175 | $resource = $custom->merge(['data' => $resource]); |
||||
176 | return response()->json($resource, JsonResponse::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE); |
||||
177 | } |
||||
178 | |||||
179 | $custom = collect(['code' => JsonResponse::HTTP_OK, 'message' => __('repository-generator.success')]); |
||||
180 | $resource = $custom->merge(['data' => $resource]); |
||||
181 | return response()->json($resource, JsonResponse::HTTP_OK); |
||||
182 | } |
||||
183 | |||||
184 | /** |
||||
185 | * Show the form for creating a new resource. |
||||
186 | * |
||||
187 | * @return Factory|JsonResponse|\Illuminate\View\View |
||||
188 | */ |
||||
189 | public function create() |
||||
190 | { |
||||
191 | if (!$this->isAPI) { |
||||
192 | View::share('pageTitle', 'Create ' . $this->pageTitle . ' | ' . Config::get('app.name')); |
||||
193 | $this->breadcrumbs->put('create', [ |
||||
194 | 'link' => $this->createRoute, |
||||
195 | 'text' => trans('repository-generator.create'), |
||||
0 ignored issues
–
show
The function
trans was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
196 | ]); |
||||
197 | |||||
198 | return view($this->viewCreate, $this->params); |
||||
0 ignored issues
–
show
The function
view was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
199 | } |
||||
200 | |||||
201 | return response()->json( |
||||
0 ignored issues
–
show
The function
response was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
202 | [ |
||||
203 | 'status' => true, |
||||
204 | 'message' => __('repository-generator.no_content'), |
||||
0 ignored issues
–
show
The function
__ was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
205 | 'data' => [] |
||||
206 | ], |
||||
207 | JsonResponse::HTTP_NO_CONTENT |
||||
208 | ); |
||||
209 | } |
||||
210 | |||||
211 | /** |
||||
212 | * Store a newly created resource in storage. |
||||
213 | * |
||||
214 | * @return RedirectResponse|mixed |
||||
215 | */ |
||||
216 | public function store() |
||||
217 | { |
||||
218 | $entity = $this->interface->create($this->request->except(['_token', '_method'])); |
||||
219 | |||||
220 | return $this->makeResponse($entity, true); |
||||
221 | } |
||||
222 | |||||
223 | /** |
||||
224 | * Display the specified resource. |
||||
225 | * |
||||
226 | * @param int $entityId |
||||
227 | * |
||||
228 | * @return Factory|JsonResponse|RedirectResponse|\Illuminate\View\View |
||||
229 | */ |
||||
230 | public function show(int $entityId) |
||||
231 | { |
||||
232 | $entity = $this->interface->find($entityId); |
||||
233 | if (!$this->isAPI) { |
||||
234 | if (!$entity) { |
||||
235 | return Redirect::to($this->routeIndex)->with('warning', __('repository-generator.not_found')); |
||||
0 ignored issues
–
show
The function
__ was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
236 | } |
||||
237 | View::share('pageTitle', 'View ' . $this->pageTitle . ' | ' . Config::get('app.name')); |
||||
238 | $this->breadcrumbs->put('view', [ |
||||
239 | 'link' => '', |
||||
240 | 'text' => __('repository-generator.show'), |
||||
241 | ]); |
||||
242 | |||||
243 | return view($this->viewShow, $this->params) |
||||
0 ignored issues
–
show
The function
view was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
244 | ->with('entity', $entity); |
||||
245 | } |
||||
246 | |||||
247 | if (!$entity) { |
||||
248 | return response()->json( |
||||
0 ignored issues
–
show
The function
response was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
249 | [ |
||||
250 | 'status' => false, |
||||
251 | 'message' => __('repository-generator.not_found'), |
||||
252 | 'data' => [] |
||||
253 | ], |
||||
254 | JsonResponse::HTTP_NOT_FOUND |
||||
255 | ); |
||||
256 | } |
||||
257 | $resource = new $this->resource($entity); |
||||
258 | return response()->json( |
||||
259 | [ |
||||
260 | 'status' => true, |
||||
261 | 'message' => __('repository-generator.success'), |
||||
262 | 'data' => $resource |
||||
263 | ], |
||||
264 | JsonResponse::HTTP_OK |
||||
265 | ); |
||||
266 | } |
||||
267 | |||||
268 | /** |
||||
269 | * Show the form for editing the specified resource. |
||||
270 | * |
||||
271 | * @param int $entityId |
||||
272 | * |
||||
273 | * @return Factory|JsonResponse|RedirectResponse|\Illuminate\View\View |
||||
274 | */ |
||||
275 | public function edit(int $entityId) |
||||
276 | { |
||||
277 | $entity = $this->interface->find($entityId); |
||||
278 | if (!$this->isAPI) { |
||||
279 | if (!$entity) { |
||||
280 | return Redirect::to($this->routeIndex)->with('warning', __('repository-generator.not_found')); |
||||
0 ignored issues
–
show
The function
__ was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
281 | } |
||||
282 | $this->breadcrumbs->put('edit', [ |
||||
283 | 'link' => '', |
||||
284 | 'text' => __('repository-generator.edit'), |
||||
285 | ]); |
||||
286 | |||||
287 | return view($this->viewEdit, $this->params) |
||||
0 ignored issues
–
show
The function
view was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
288 | ->with('entity', $entity); |
||||
289 | } |
||||
290 | |||||
291 | return response()->json( |
||||
0 ignored issues
–
show
The function
response was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
292 | [ |
||||
293 | 'status' => false, |
||||
294 | 'message' => __('repository-generator.not_found'), |
||||
295 | 'data' => [] |
||||
296 | ], |
||||
297 | JsonResponse::HTTP_NOT_FOUND |
||||
298 | ); |
||||
299 | } |
||||
300 | |||||
301 | /** |
||||
302 | * Update the specified resource in storage. |
||||
303 | * |
||||
304 | * @param int $entityId |
||||
305 | * |
||||
306 | * @return RedirectResponse |
||||
307 | */ |
||||
308 | public function update(int $entityId) |
||||
309 | { |
||||
310 | $entity = $this->interface->update($entityId, $this->request->except(['_token', '_method'])); |
||||
311 | |||||
312 | return $this->makeResponse($entity, true); |
||||
0 ignored issues
–
show
|
|||||
313 | } |
||||
314 | |||||
315 | /** |
||||
316 | * Remove the specified resource from storage. |
||||
317 | * |
||||
318 | * @param int $entityId |
||||
319 | * |
||||
320 | * @return RedirectResponse |
||||
321 | * @throws Exception |
||||
322 | * |
||||
323 | */ |
||||
324 | public function destroy(int $entityId) |
||||
325 | { |
||||
326 | $deleted = $this->interface->delete($entityId); |
||||
327 | |||||
328 | return $this->makeResponse($deleted); |
||||
0 ignored issues
–
show
|
|||||
329 | } |
||||
330 | |||||
331 | /** |
||||
332 | * Restore the specified resource from storage. |
||||
333 | * |
||||
334 | * @param int $entityId |
||||
335 | * |
||||
336 | * @return RedirectResponse |
||||
337 | */ |
||||
338 | public function restore(int $entityId) |
||||
339 | { |
||||
340 | $entity = $this->interface->restore($entityId); |
||||
341 | |||||
342 | return $this->makeResponse($entity); |
||||
0 ignored issues
–
show
|
|||||
343 | } |
||||
344 | |||||
345 | /** |
||||
346 | * Restore the specified resource from storage. |
||||
347 | * |
||||
348 | * @param int $entityId |
||||
349 | * |
||||
350 | * @return RedirectResponse |
||||
351 | */ |
||||
352 | public function forceDelete(int $entityId) |
||||
353 | { |
||||
354 | $entity = $this->interface->forceDelete($entityId); |
||||
355 | |||||
356 | return $this->makeResponse($entity); |
||||
0 ignored issues
–
show
|
|||||
357 | } |
||||
358 | |||||
359 | /** |
||||
360 | * Make response for web or json. |
||||
361 | * |
||||
362 | * @param mixed $entity |
||||
363 | * @param bool $appendEntity |
||||
364 | * |
||||
365 | * @return JsonResponse|RedirectResponse |
||||
366 | */ |
||||
367 | public function makeResponse($entity, bool $appendEntity = false) |
||||
368 | { |
||||
369 | if (!$this->isAPI) { |
||||
370 | if ($entity) { |
||||
371 | return Redirect::to($this->routeIndex)->with('message', __('repository-generator.success')); |
||||
0 ignored issues
–
show
The function
__ was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
372 | } |
||||
373 | |||||
374 | if (null === $entity) { |
||||
375 | return Redirect::to($this->routeIndex)->with('warning', __('repository-generator.not_found')); |
||||
376 | } |
||||
377 | |||||
378 | return Redirect::to($this->routeIndex)->with('error', __('repository-generator.not_modified')); |
||||
379 | } |
||||
380 | |||||
381 | if ($entity) { |
||||
382 | if ($appendEntity) { |
||||
383 | return response()->json( |
||||
0 ignored issues
–
show
The function
response was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
384 | [ |
||||
385 | 'status' => true, |
||||
386 | 'message' => __('repository-generator.success'), |
||||
387 | 'data' => new JsonResource($entity) |
||||
388 | ], |
||||
389 | JsonResponse::HTTP_OK |
||||
390 | ); |
||||
391 | } |
||||
392 | |||||
393 | return response()->json( |
||||
394 | [ |
||||
395 | 'status' => false, |
||||
396 | 'message' => __('repository-generator.no_content'), |
||||
397 | 'data' => [] |
||||
398 | ], |
||||
399 | JsonResponse::HTTP_NO_CONTENT |
||||
400 | ); |
||||
401 | } |
||||
402 | |||||
403 | if (null === $entity) { |
||||
404 | return response()->json( |
||||
405 | [ |
||||
406 | 'status' => false, |
||||
407 | 'message' => __('repository-generator.not_found'), |
||||
408 | 'data' => [] |
||||
409 | ], |
||||
410 | JsonResponse::HTTP_NOT_FOUND |
||||
411 | ); |
||||
412 | } |
||||
413 | |||||
414 | return response()->json( |
||||
415 | [ |
||||
416 | 'status' => false, |
||||
417 | 'message' => __('repository-generator.not_modified'), |
||||
418 | 'data' => [] |
||||
419 | ], |
||||
420 | JsonResponse::HTTP_NOT_MODIFIED |
||||
421 | ); |
||||
422 | } |
||||
423 | } |
||||
424 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: