|
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; |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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 $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 ((bool)$request->get('with-trash', false)) { |
|
80
|
|
|
$interface->withTrash(); |
|
81
|
|
|
} |
|
82
|
|
|
if ((bool)$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
|
|
|
/** |
|
139
|
|
|
* Display a listing of the resource. |
|
140
|
|
|
* |
|
141
|
|
|
* |
|
142
|
|
|
* @return Factory|JsonResponse|\Illuminate\View\View |
|
|
|
|
|
|
143
|
|
|
*/ |
|
144
|
|
|
public function index() |
|
145
|
|
|
{ |
|
146
|
|
|
$data = $this->interface->simplePaginate($this->limit, $this->request->all()); |
|
147
|
|
|
|
|
148
|
|
|
if (!$this->isAPI) { |
|
149
|
|
|
View::share('pageTitle', 'List ' . $this->pageTitle . ' | ' . Config::get('app.name')); |
|
150
|
|
|
$this->breadcrumbs->put('index', [ |
|
151
|
|
|
'link' => $this->routeIndex, |
|
152
|
|
|
'text' => $this->pageTitle, |
|
153
|
|
|
]); |
|
154
|
|
|
|
|
155
|
|
|
return view($this->viewIndex, $this->params) |
|
|
|
|
|
|
156
|
|
|
->with('entities', $data) |
|
157
|
|
|
->with('createRoute', $this->createRoute) |
|
158
|
|
|
->with('filters', $this->request->all()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$resource = $this->resource::collection($data); |
|
162
|
|
|
if ($data->hasMorePages()) { |
|
163
|
|
|
$custom = collect(['code' => JsonResponse::HTTP_PARTIAL_CONTENT, 'message' => __('repository-generator.partial_content')]); |
|
|
|
|
|
|
164
|
|
|
$resource = $custom->merge(['data' => $resource]); |
|
165
|
|
|
return response()->json($resource, JsonResponse::HTTP_PARTIAL_CONTENT); |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if ($data->isEmpty()) { |
|
169
|
|
|
$custom = collect(['code' => JsonResponse::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE, 'message' => __('repository-generator.no_content')]); |
|
170
|
|
|
$resource = $custom->merge(['data' => $resource]); |
|
171
|
|
|
return response()->json($resource, JsonResponse::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$custom = collect(['code' => JsonResponse::HTTP_OK, 'message' => __('repository-generator.success')]); |
|
175
|
|
|
$resource = $custom->merge(['data' => $resource]); |
|
176
|
|
|
return response()->json($resource, JsonResponse::HTTP_OK); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Show the form for creating a new resource. |
|
181
|
|
|
* |
|
182
|
|
|
* @return Factory|JsonResponse|\Illuminate\View\View |
|
183
|
|
|
*/ |
|
184
|
|
|
public function create() |
|
185
|
|
|
{ |
|
186
|
|
|
if (!$this->isAPI) { |
|
187
|
|
|
View::share('pageTitle', 'Create ' . $this->pageTitle . ' | ' . Config::get('app.name')); |
|
188
|
|
|
$this->breadcrumbs->put('create', [ |
|
189
|
|
|
'link' => $this->createRoute, |
|
190
|
|
|
'text' => trans('repository-generator.create'), |
|
|
|
|
|
|
191
|
|
|
]); |
|
192
|
|
|
|
|
193
|
|
|
return view($this->viewCreate, $this->params); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return response()->json( |
|
|
|
|
|
|
197
|
|
|
[ |
|
198
|
|
|
'status' => true, |
|
199
|
|
|
'message' => __('repository-generator.no_content'), |
|
|
|
|
|
|
200
|
|
|
'data' => [] |
|
201
|
|
|
], |
|
202
|
|
|
JsonResponse::HTTP_NO_CONTENT |
|
203
|
|
|
); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Store a newly created resource in storage. |
|
208
|
|
|
* |
|
209
|
|
|
* @return RedirectResponse|mixed |
|
210
|
|
|
*/ |
|
211
|
|
|
public function store() |
|
212
|
|
|
{ |
|
213
|
|
|
$entity = $this->interface->create($this->request->except(['_token', '_method'])); |
|
214
|
|
|
|
|
215
|
|
|
return $this->makeResponse($entity, true); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Display the specified resource. |
|
220
|
|
|
* |
|
221
|
|
|
* @param int $entityId |
|
222
|
|
|
* |
|
223
|
|
|
* @return Factory|JsonResponse|RedirectResponse|\Illuminate\View\View |
|
224
|
|
|
*/ |
|
225
|
|
|
public function show($entityId) |
|
226
|
|
|
{ |
|
227
|
|
|
$entity = $this->interface->find($entityId); |
|
228
|
|
|
if (!$this->isAPI) { |
|
229
|
|
|
if (!$entity) { |
|
230
|
|
|
return Redirect::to($this->routeIndex)->with('warning', __('repository-generator.not_found')); |
|
|
|
|
|
|
231
|
|
|
} |
|
232
|
|
|
View::share('pageTitle', 'View ' . $this->pageTitle . ' | ' . Config::get('app.name')); |
|
233
|
|
|
$this->breadcrumbs->put('view', [ |
|
234
|
|
|
'link' => '', |
|
235
|
|
|
'text' => __('repository-generator.show'), |
|
236
|
|
|
]); |
|
237
|
|
|
|
|
238
|
|
|
return view($this->viewShow, $this->params) |
|
|
|
|
|
|
239
|
|
|
->with('entity', $entity); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
if (!$entity) { |
|
243
|
|
|
return response()->json( |
|
|
|
|
|
|
244
|
|
|
[ |
|
245
|
|
|
'status' => false, |
|
246
|
|
|
'message' => __('repository-generator.not_found'), |
|
247
|
|
|
'data' => [] |
|
248
|
|
|
], |
|
249
|
|
|
JsonResponse::HTTP_NOT_FOUND |
|
250
|
|
|
); |
|
251
|
|
|
} |
|
252
|
|
|
$resource = new $this->resource($entity); |
|
253
|
|
|
return response()->json( |
|
254
|
|
|
[ |
|
255
|
|
|
'status' => true, |
|
256
|
|
|
'message' => __('repository-generator.success'), |
|
257
|
|
|
'data' => $resource |
|
258
|
|
|
], |
|
259
|
|
|
JsonResponse::HTTP_OK |
|
260
|
|
|
); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Show the form for editing the specified resource. |
|
265
|
|
|
* |
|
266
|
|
|
* @param int $entityId |
|
267
|
|
|
* |
|
268
|
|
|
* @return Factory|JsonResponse|RedirectResponse|\Illuminate\View\View |
|
269
|
|
|
*/ |
|
270
|
|
|
public function edit($entityId) |
|
271
|
|
|
{ |
|
272
|
|
|
$entity = $this->interface->find($entityId); |
|
273
|
|
|
if (!$this->isAPI) { |
|
274
|
|
|
if (!$entity) { |
|
275
|
|
|
return Redirect::to($this->routeIndex)->with('warning', __('repository-generator.not_found')); |
|
|
|
|
|
|
276
|
|
|
} |
|
277
|
|
|
$this->breadcrumbs->put('edit', [ |
|
278
|
|
|
'link' => '', |
|
279
|
|
|
'text' => __('repository-generator.edit'), |
|
280
|
|
|
]); |
|
281
|
|
|
|
|
282
|
|
|
return view($this->viewEdit, $this->params) |
|
|
|
|
|
|
283
|
|
|
->with('entity', $entity); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return response()->json( |
|
|
|
|
|
|
287
|
|
|
[ |
|
288
|
|
|
'status' => false, |
|
289
|
|
|
'message' => __('repository-generator.not_found'), |
|
290
|
|
|
'data' => [] |
|
291
|
|
|
], |
|
292
|
|
|
JsonResponse::HTTP_NOT_FOUND |
|
293
|
|
|
); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Update the specified resource in storage. |
|
298
|
|
|
* |
|
299
|
|
|
* @param int $entityId |
|
300
|
|
|
* |
|
301
|
|
|
* @return RedirectResponse |
|
302
|
|
|
*/ |
|
303
|
|
|
public function update($entityId) |
|
304
|
|
|
{ |
|
305
|
|
|
$entity = $this->interface->update($entityId, $this->request->except(['_token', '_method'])); |
|
306
|
|
|
|
|
307
|
|
|
return $this->makeResponse($entity, true); |
|
|
|
|
|
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Remove the specified resource from storage. |
|
312
|
|
|
* |
|
313
|
|
|
* @param int $entityId |
|
314
|
|
|
* |
|
315
|
|
|
* @return RedirectResponse |
|
316
|
|
|
* @throws Exception |
|
317
|
|
|
* |
|
318
|
|
|
*/ |
|
319
|
|
|
public function destroy($entityId) |
|
320
|
|
|
{ |
|
321
|
|
|
$deleted = $this->interface->delete($entityId); |
|
322
|
|
|
|
|
323
|
|
|
return $this->makeResponse($deleted); |
|
|
|
|
|
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Restore the specified resource from storage. |
|
328
|
|
|
* |
|
329
|
|
|
* @param int $entityId |
|
330
|
|
|
* |
|
331
|
|
|
* @return RedirectResponse |
|
332
|
|
|
*/ |
|
333
|
|
|
public function restore($entityId) |
|
334
|
|
|
{ |
|
335
|
|
|
$entity = $this->interface->restore($entityId); |
|
336
|
|
|
|
|
337
|
|
|
return $this->makeResponse($entity); |
|
|
|
|
|
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Restore the specified resource from storage. |
|
342
|
|
|
* |
|
343
|
|
|
* @param int $entityId |
|
344
|
|
|
* |
|
345
|
|
|
* @return RedirectResponse |
|
346
|
|
|
*/ |
|
347
|
|
|
public function forceDelete($entityId) |
|
348
|
|
|
{ |
|
349
|
|
|
$entity = $this->interface->forceDelete($entityId); |
|
350
|
|
|
|
|
351
|
|
|
return $this->makeResponse($entity); |
|
|
|
|
|
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* Make response for web or json. |
|
356
|
|
|
* |
|
357
|
|
|
* @param mixed $entity |
|
358
|
|
|
* @param bool $appendEntity |
|
359
|
|
|
* |
|
360
|
|
|
* @return JsonResponse|RedirectResponse |
|
361
|
|
|
*/ |
|
362
|
|
|
public function makeResponse($entity, $appendEntity = false) |
|
363
|
|
|
{ |
|
364
|
|
|
if (!$this->isAPI) { |
|
365
|
|
|
if ($entity) { |
|
366
|
|
|
return Redirect::to($this->routeIndex)->with('message', __('repository-generator.success')); |
|
|
|
|
|
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
if (null === $entity) { |
|
370
|
|
|
return Redirect::to($this->routeIndex)->with('warning', __('repository-generator.not_found')); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
return Redirect::to($this->routeIndex)->with('error', __('repository-generator.not_modified')); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
if ($entity) { |
|
377
|
|
|
if ($appendEntity) { |
|
378
|
|
|
return response()->json( |
|
|
|
|
|
|
379
|
|
|
[ |
|
380
|
|
|
'status' => true, |
|
381
|
|
|
'message' => __('repository-generator.success'), |
|
382
|
|
|
'data' => new JsonResource($entity) |
|
383
|
|
|
], |
|
384
|
|
|
JsonResponse::HTTP_OK |
|
385
|
|
|
); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
return response()->json( |
|
389
|
|
|
[ |
|
390
|
|
|
'status' => false, |
|
391
|
|
|
'message' => __('repository-generator.no_content'), |
|
392
|
|
|
'data' => [] |
|
393
|
|
|
], |
|
394
|
|
|
JsonResponse::HTTP_NO_CONTENT |
|
395
|
|
|
); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
if (null === $entity) { |
|
399
|
|
|
return response()->json( |
|
400
|
|
|
[ |
|
401
|
|
|
'status' => false, |
|
402
|
|
|
'message' => __('repository-generator.not_found'), |
|
403
|
|
|
'data' => [] |
|
404
|
|
|
], |
|
405
|
|
|
JsonResponse::HTTP_NOT_FOUND |
|
406
|
|
|
); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
return response()->json( |
|
410
|
|
|
[ |
|
411
|
|
|
'status' => false, |
|
412
|
|
|
'message' => __('repository-generator.not_modified'), |
|
413
|
|
|
'data' => [] |
|
414
|
|
|
], |
|
415
|
|
|
JsonResponse::HTTP_NOT_MODIFIED |
|
416
|
|
|
); |
|
417
|
|
|
} |
|
418
|
|
|
} |
|
419
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: