@@ -11,68 +11,68 @@ |
||
11 | 11 | |
12 | 12 | class CheckPermissions |
13 | 13 | { |
14 | - protected $route; |
|
15 | - protected $auth; |
|
16 | - protected $authMiddleware; |
|
17 | - protected $userService; |
|
18 | - protected $arr; |
|
14 | + protected $route; |
|
15 | + protected $auth; |
|
16 | + protected $authMiddleware; |
|
17 | + protected $userService; |
|
18 | + protected $arr; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Init new object. |
|
22 | - * |
|
23 | - * @param Route $route |
|
24 | - * @param Auth $auth |
|
25 | - * @param AuthMiddleware $authMiddleware |
|
26 | - * @param UserService $userService |
|
27 | - * @param Arr $arr |
|
28 | - * |
|
29 | - * @return void |
|
30 | - */ |
|
31 | - public function __construct(Route $route, Auth $auth, AuthMiddleware $authMiddleware, UserService $userService, Arr $arr) |
|
32 | - { |
|
33 | - $this->route = $route; |
|
34 | - $this->auth = $auth; |
|
35 | - $this->authMiddleware = $authMiddleware; |
|
36 | - $this->userService = $userService; |
|
37 | - $this->arr = $arr; |
|
38 | - } |
|
20 | + /** |
|
21 | + * Init new object. |
|
22 | + * |
|
23 | + * @param Route $route |
|
24 | + * @param Auth $auth |
|
25 | + * @param AuthMiddleware $authMiddleware |
|
26 | + * @param UserService $userService |
|
27 | + * @param Arr $arr |
|
28 | + * |
|
29 | + * @return void |
|
30 | + */ |
|
31 | + public function __construct(Route $route, Auth $auth, AuthMiddleware $authMiddleware, UserService $userService, Arr $arr) |
|
32 | + { |
|
33 | + $this->route = $route; |
|
34 | + $this->auth = $auth; |
|
35 | + $this->authMiddleware = $authMiddleware; |
|
36 | + $this->userService = $userService; |
|
37 | + $this->arr = $arr; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Handle an incoming request. |
|
42 | - * |
|
43 | - * @param \Illuminate\Http\Request $request |
|
44 | - * @param \Closure $next |
|
45 | - * @return mixed |
|
46 | - */ |
|
47 | - public function handle($request, Closure $next) |
|
48 | - { |
|
49 | - $routeActions = explode('@', $this->route->currentRouteAction()); |
|
50 | - $reflectionClass = new \ReflectionClass($routeActions[0]); |
|
51 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
52 | - $skipPermissionCheck = $this->arr->get($classProperties, 'skipPermissionCheck', []); |
|
53 | - $skipLoginCheck = $this->arr->get($classProperties, 'skipLoginCheck', []); |
|
54 | - $modelName = explode('\\', $routeActions[0]); |
|
55 | - $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
56 | - $permission = $routeActions[1]; |
|
40 | + /** |
|
41 | + * Handle an incoming request. |
|
42 | + * |
|
43 | + * @param \Illuminate\Http\Request $request |
|
44 | + * @param \Closure $next |
|
45 | + * @return mixed |
|
46 | + */ |
|
47 | + public function handle($request, Closure $next) |
|
48 | + { |
|
49 | + $routeActions = explode('@', $this->route->currentRouteAction()); |
|
50 | + $reflectionClass = new \ReflectionClass($routeActions[0]); |
|
51 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
52 | + $skipPermissionCheck = $this->arr->get($classProperties, 'skipPermissionCheck', []); |
|
53 | + $skipLoginCheck = $this->arr->get($classProperties, 'skipLoginCheck', []); |
|
54 | + $modelName = explode('\\', $routeActions[0]); |
|
55 | + $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
56 | + $permission = $routeActions[1]; |
|
57 | 57 | |
58 | - $this->auth->shouldUse('api'); |
|
59 | - if (! in_array($permission, $skipLoginCheck)) { |
|
60 | - $this->authMiddleware->handle($request, function ($request) use ($modelName, $skipPermissionCheck, $permission) { |
|
61 | - $user = $this->auth->user(); |
|
62 | - $isPasswordClient = $user->token()->client->password_client; |
|
58 | + $this->auth->shouldUse('api'); |
|
59 | + if (! in_array($permission, $skipLoginCheck)) { |
|
60 | + $this->authMiddleware->handle($request, function ($request) use ($modelName, $skipPermissionCheck, $permission) { |
|
61 | + $user = $this->auth->user(); |
|
62 | + $isPasswordClient = $user->token()->client->password_client; |
|
63 | 63 | |
64 | - if ($user->blocked) { |
|
65 | - \Errors::userIsBlocked(); |
|
66 | - } |
|
64 | + if ($user->blocked) { |
|
65 | + \Errors::userIsBlocked(); |
|
66 | + } |
|
67 | 67 | |
68 | - if ($isPasswordClient && (in_array($permission, $skipPermissionCheck) || $this->userService->can($permission, $modelName))) { |
|
69 | - } elseif (! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
70 | - } else { |
|
71 | - \Errors::noPermissions(); |
|
72 | - } |
|
73 | - }); |
|
74 | - } |
|
68 | + if ($isPasswordClient && (in_array($permission, $skipPermissionCheck) || $this->userService->can($permission, $modelName))) { |
|
69 | + } elseif (! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
70 | + } else { |
|
71 | + \Errors::noPermissions(); |
|
72 | + } |
|
73 | + }); |
|
74 | + } |
|
75 | 75 | |
76 | - return $next($request); |
|
77 | - } |
|
76 | + return $next($request); |
|
77 | + } |
|
78 | 78 | } |
@@ -2,15 +2,15 @@ |
||
2 | 2 | |
3 | 3 | return [ |
4 | 4 | |
5 | - /** |
|
6 | - * Here goes your error messages. |
|
7 | - */ |
|
8 | - 'connectionError' => 'Connection error', |
|
9 | - 'redisNotRunning' => 'Your redis notification server is\'t running', |
|
10 | - 'dbQueryError' => 'Please check the given inputes', |
|
11 | - 'cannotCreateSetting' => 'Can\'t create setting', |
|
12 | - 'cannotUpdateSettingKey' => 'Can\'t update setting key', |
|
13 | - 'notFound' => 'The requested :replace not found', |
|
14 | - 'cannotUploadImage' => 'Can\'t upload the given image' |
|
5 | + /** |
|
6 | + * Here goes your error messages. |
|
7 | + */ |
|
8 | + 'connectionError' => 'Connection error', |
|
9 | + 'redisNotRunning' => 'Your redis notification server is\'t running', |
|
10 | + 'dbQueryError' => 'Please check the given inputes', |
|
11 | + 'cannotCreateSetting' => 'Can\'t create setting', |
|
12 | + 'cannotUpdateSettingKey' => 'Can\'t update setting key', |
|
13 | + 'notFound' => 'The requested :replace not found', |
|
14 | + 'cannotUploadImage' => 'Can\'t upload the given image' |
|
15 | 15 | |
16 | 16 | ]; |
@@ -2,15 +2,15 @@ |
||
2 | 2 | |
3 | 3 | return [ |
4 | 4 | |
5 | - /** |
|
6 | - * Here goes your error messages. |
|
7 | - */ |
|
8 | - 'connectionError' => 'خطأ في الاتصال', |
|
9 | - 'redisNotRunning' => 'سيرفير الاشعارات لايعمل', |
|
10 | - 'dbQueryError' => 'خطا في البيانات', |
|
11 | - 'cannotCreateSetting' => 'لا يمكن اضافة اعدادات', |
|
12 | - 'cannotUpdateSettingKey' => 'لا يمكن تعديل اعدادات', |
|
13 | - 'notFound' => 'ال :replace المطلوب غير موجود', |
|
14 | - 'cannotUploadImage' => 'لا يمكن رفع هذه الصورة' |
|
5 | + /** |
|
6 | + * Here goes your error messages. |
|
7 | + */ |
|
8 | + 'connectionError' => 'خطأ في الاتصال', |
|
9 | + 'redisNotRunning' => 'سيرفير الاشعارات لايعمل', |
|
10 | + 'dbQueryError' => 'خطا في البيانات', |
|
11 | + 'cannotCreateSetting' => 'لا يمكن اضافة اعدادات', |
|
12 | + 'cannotUpdateSettingKey' => 'لا يمكن تعديل اعدادات', |
|
13 | + 'notFound' => 'ال :replace المطلوب غير موجود', |
|
14 | + 'cannotUploadImage' => 'لا يمكن رفع هذه الصورة' |
|
15 | 15 | |
16 | 16 | ]; |
@@ -9,21 +9,21 @@ |
||
9 | 9 | class Setting extends Model |
10 | 10 | { |
11 | 11 | |
12 | - use SoftDeletes; |
|
13 | - protected $table = 'settings'; |
|
14 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
15 | - protected $hidden = ['deleted_at']; |
|
16 | - protected $guarded = ['id', 'key']; |
|
17 | - public $fillable = ['name', 'value']; |
|
12 | + use SoftDeletes; |
|
13 | + protected $table = 'settings'; |
|
14 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
15 | + protected $hidden = ['deleted_at']; |
|
16 | + protected $guarded = ['id', 'key']; |
|
17 | + public $fillable = ['name', 'value']; |
|
18 | 18 | |
19 | - public function newCollection(array $models = []) |
|
20 | - { |
|
21 | - return parent::newCollection($models)->keyBy('key'); |
|
22 | - } |
|
19 | + public function newCollection(array $models = []) |
|
20 | + { |
|
21 | + return parent::newCollection($models)->keyBy('key'); |
|
22 | + } |
|
23 | 23 | |
24 | - public static function boot() |
|
25 | - { |
|
26 | - parent::boot(); |
|
27 | - Setting::observe(SettingsObserver::class); |
|
28 | - } |
|
24 | + public static function boot() |
|
25 | + { |
|
26 | + parent::boot(); |
|
27 | + Setting::observe(SettingsObserver::class); |
|
28 | + } |
|
29 | 29 | } |
@@ -9,109 +9,109 @@ |
||
9 | 9 | |
10 | 10 | class BaseApiController extends Controller |
11 | 11 | { |
12 | - /** |
|
13 | - * Array of eager loaded relations. |
|
14 | - * |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $relations; |
|
12 | + /** |
|
13 | + * Array of eager loaded relations. |
|
14 | + * |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $relations; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @var object |
|
21 | - */ |
|
22 | - protected $service; |
|
19 | + /** |
|
20 | + * @var object |
|
21 | + */ |
|
22 | + protected $service; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Path of the model resource. |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $modelResource; |
|
24 | + /** |
|
25 | + * Path of the model resource. |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $modelResource; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Path of the sotre form request. |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $storeFormRequest; |
|
31 | + /** |
|
32 | + * Path of the sotre form request. |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $storeFormRequest; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Init new object. |
|
40 | - * |
|
41 | - * @param mixed $service |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function __construct($service) |
|
45 | - { |
|
46 | - $this->service = new CachingDecorator($service, \App::make('Illuminate\Contracts\Cache\Repository')); |
|
47 | - } |
|
38 | + /** |
|
39 | + * Init new object. |
|
40 | + * |
|
41 | + * @param mixed $service |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function __construct($service) |
|
45 | + { |
|
46 | + $this->service = new CachingDecorator($service, \App::make('Illuminate\Contracts\Cache\Repository')); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Fetch all records with relations from storage. |
|
51 | - * |
|
52 | - * @param Request $request |
|
53 | - * @return \Illuminate\Http\Response |
|
54 | - */ |
|
55 | - public function index(Request $request) |
|
56 | - { |
|
57 | - return $this->modelResource::collection($this->service->list($request->relations, $request->query(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'), $request->query('trashed'))); |
|
58 | - } |
|
49 | + /** |
|
50 | + * Fetch all records with relations from storage. |
|
51 | + * |
|
52 | + * @param Request $request |
|
53 | + * @return \Illuminate\Http\Response |
|
54 | + */ |
|
55 | + public function index(Request $request) |
|
56 | + { |
|
57 | + return $this->modelResource::collection($this->service->list($request->relations, $request->query(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'), $request->query('trashed'))); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Fetch the single object with relations from storage. |
|
62 | - * |
|
63 | - * @param Request $request |
|
64 | - * @param integer $id Id of the requested model. |
|
65 | - * @return \Illuminate\Http\Response |
|
66 | - */ |
|
67 | - public function show(Request $request, $id) |
|
68 | - { |
|
69 | - return new $this->modelResource($this->service->find($id, $request->relations)); |
|
70 | - } |
|
60 | + /** |
|
61 | + * Fetch the single object with relations from storage. |
|
62 | + * |
|
63 | + * @param Request $request |
|
64 | + * @param integer $id Id of the requested model. |
|
65 | + * @return \Illuminate\Http\Response |
|
66 | + */ |
|
67 | + public function show(Request $request, $id) |
|
68 | + { |
|
69 | + return new $this->modelResource($this->service->find($id, $request->relations)); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Insert the given model to storage. |
|
74 | - * |
|
75 | - * @return \Illuminate\Http\Response |
|
76 | - */ |
|
77 | - public function store() |
|
78 | - { |
|
79 | - $data = \App::make($this->storeFormRequest)->validated(); |
|
80 | - return new $this->modelResource($this->service->save($data)); |
|
81 | - } |
|
72 | + /** |
|
73 | + * Insert the given model to storage. |
|
74 | + * |
|
75 | + * @return \Illuminate\Http\Response |
|
76 | + */ |
|
77 | + public function store() |
|
78 | + { |
|
79 | + $data = \App::make($this->storeFormRequest)->validated(); |
|
80 | + return new $this->modelResource($this->service->save($data)); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Update the given model to storage. |
|
85 | - * |
|
86 | - * @param integer $id |
|
87 | - * @return \Illuminate\Http\Response |
|
88 | - */ |
|
89 | - public function update($id) |
|
90 | - { |
|
91 | - $data = \App::make($this->storeFormRequest)->validated(); |
|
92 | - $data['id'] = $id; |
|
93 | - return new $this->modelResource($this->service->save($data)); |
|
94 | - } |
|
83 | + /** |
|
84 | + * Update the given model to storage. |
|
85 | + * |
|
86 | + * @param integer $id |
|
87 | + * @return \Illuminate\Http\Response |
|
88 | + */ |
|
89 | + public function update($id) |
|
90 | + { |
|
91 | + $data = \App::make($this->storeFormRequest)->validated(); |
|
92 | + $data['id'] = $id; |
|
93 | + return new $this->modelResource($this->service->save($data)); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Delete by the given id from storage. |
|
98 | - * |
|
99 | - * @param integer $id Id of the deleted model. |
|
100 | - * @return \Illuminate\Http\Response |
|
101 | - */ |
|
102 | - public function destroy($id) |
|
103 | - { |
|
104 | - return new GeneralResource($this->service->delete($id)); |
|
105 | - } |
|
96 | + /** |
|
97 | + * Delete by the given id from storage. |
|
98 | + * |
|
99 | + * @param integer $id Id of the deleted model. |
|
100 | + * @return \Illuminate\Http\Response |
|
101 | + */ |
|
102 | + public function destroy($id) |
|
103 | + { |
|
104 | + return new GeneralResource($this->service->delete($id)); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Restore the deleted model. |
|
109 | - * |
|
110 | - * @param integer $id Id of the restored model. |
|
111 | - * @return \Illuminate\Http\Response |
|
112 | - */ |
|
113 | - public function restore($id) |
|
114 | - { |
|
115 | - return new GeneralResource($this->service->restore($id)); |
|
116 | - } |
|
107 | + /** |
|
108 | + * Restore the deleted model. |
|
109 | + * |
|
110 | + * @param integer $id Id of the restored model. |
|
111 | + * @return \Illuminate\Http\Response |
|
112 | + */ |
|
113 | + public function restore($id) |
|
114 | + { |
|
115 | + return new GeneralResource($this->service->restore($id)); |
|
116 | + } |
|
117 | 117 | } |
@@ -5,30 +5,30 @@ |
||
5 | 5 | |
6 | 6 | class Settings extends Migration |
7 | 7 | { |
8 | - /** |
|
9 | - * Run the migrations. |
|
10 | - * |
|
11 | - * @return void |
|
12 | - */ |
|
13 | - public function up() |
|
14 | - { |
|
15 | - Schema::create('settings', function (Blueprint $table) { |
|
16 | - increments('id'); |
|
17 | - $table->string('name', 100); |
|
18 | - $table->string('key', 100)->unique(); |
|
19 | - $table->text('value')->nullable(); |
|
20 | - $table->softDeletes(); |
|
21 | - $table->timestamps(); |
|
22 | - }); |
|
23 | - } |
|
8 | + /** |
|
9 | + * Run the migrations. |
|
10 | + * |
|
11 | + * @return void |
|
12 | + */ |
|
13 | + public function up() |
|
14 | + { |
|
15 | + Schema::create('settings', function (Blueprint $table) { |
|
16 | + increments('id'); |
|
17 | + $table->string('name', 100); |
|
18 | + $table->string('key', 100)->unique(); |
|
19 | + $table->text('value')->nullable(); |
|
20 | + $table->softDeletes(); |
|
21 | + $table->timestamps(); |
|
22 | + }); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * Reverse the migrations. |
|
27 | - * |
|
28 | - * @return void |
|
29 | - */ |
|
30 | - public function down() |
|
31 | - { |
|
32 | - Schema::dropIfExists('settings'); |
|
33 | - } |
|
25 | + /** |
|
26 | + * Reverse the migrations. |
|
27 | + * |
|
28 | + * @return void |
|
29 | + */ |
|
30 | + public function down() |
|
31 | + { |
|
32 | + Schema::dropIfExists('settings'); |
|
33 | + } |
|
34 | 34 | } |
@@ -4,70 +4,70 @@ |
||
4 | 4 | |
5 | 5 | class Media |
6 | 6 | { |
7 | - /** |
|
8 | - * Upload the given image. |
|
9 | - * |
|
10 | - * @param object $image |
|
11 | - * @param string $dir |
|
12 | - * @return string |
|
13 | - */ |
|
14 | - public function uploadImage($image, $dir) |
|
15 | - { |
|
16 | - $image = \Image::make($image); |
|
17 | - return $this->saveImage($image, $dir); |
|
18 | - } |
|
7 | + /** |
|
8 | + * Upload the given image. |
|
9 | + * |
|
10 | + * @param object $image |
|
11 | + * @param string $dir |
|
12 | + * @return string |
|
13 | + */ |
|
14 | + public function uploadImage($image, $dir) |
|
15 | + { |
|
16 | + $image = \Image::make($image); |
|
17 | + return $this->saveImage($image, $dir); |
|
18 | + } |
|
19 | 19 | |
20 | - /** |
|
21 | - * Upload the given image. |
|
22 | - * |
|
23 | - * @param object $image |
|
24 | - * @param string $dir |
|
25 | - * @return string |
|
26 | - */ |
|
27 | - public function uploadImageBas64($image, $dir) |
|
28 | - { |
|
29 | - if (! strlen($image)) { |
|
30 | - return null; |
|
31 | - } |
|
20 | + /** |
|
21 | + * Upload the given image. |
|
22 | + * |
|
23 | + * @param object $image |
|
24 | + * @param string $dir |
|
25 | + * @return string |
|
26 | + */ |
|
27 | + public function uploadImageBas64($image, $dir) |
|
28 | + { |
|
29 | + if (! strlen($image)) { |
|
30 | + return null; |
|
31 | + } |
|
32 | 32 | |
33 | - if(filter_var($image, FILTER_VALIDATE_URL)) { |
|
34 | - return $image; |
|
35 | - } |
|
33 | + if(filter_var($image, FILTER_VALIDATE_URL)) { |
|
34 | + return $image; |
|
35 | + } |
|
36 | 36 | |
37 | - try { |
|
38 | - $base = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image)); |
|
39 | - $image = \Image::make($base); |
|
40 | - } catch (\Exception $e) { |
|
41 | - \Errors::cannotUploadImage(); |
|
42 | - } |
|
37 | + try { |
|
38 | + $base = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image)); |
|
39 | + $image = \Image::make($base); |
|
40 | + } catch (\Exception $e) { |
|
41 | + \Errors::cannotUploadImage(); |
|
42 | + } |
|
43 | 43 | |
44 | - return $this->saveImage($image, $dir); |
|
45 | - } |
|
44 | + return $this->saveImage($image, $dir); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Delete the given image. |
|
49 | - * |
|
50 | - * @param object $path |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function deleteImage($path) |
|
54 | - { |
|
55 | - \Storage::delete($path); |
|
56 | - } |
|
47 | + /** |
|
48 | + * Delete the given image. |
|
49 | + * |
|
50 | + * @param object $path |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function deleteImage($path) |
|
54 | + { |
|
55 | + \Storage::delete($path); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Save the given image. |
|
60 | - * |
|
61 | - * @param object $image |
|
62 | - * @param string $dir |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - protected function saveImage($image, $dir) |
|
66 | - { |
|
67 | - $imageName = 'image'.uniqid().time().'.jpg'; |
|
68 | - $path = 'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$imageName; |
|
69 | - \Storage::put($path, $image->stream()); |
|
58 | + /** |
|
59 | + * Save the given image. |
|
60 | + * |
|
61 | + * @param object $image |
|
62 | + * @param string $dir |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + protected function saveImage($image, $dir) |
|
66 | + { |
|
67 | + $imageName = 'image'.uniqid().time().'.jpg'; |
|
68 | + $path = 'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$imageName; |
|
69 | + \Storage::put($path, $image->stream()); |
|
70 | 70 | |
71 | - return $path; |
|
72 | - } |
|
71 | + return $path; |
|
72 | + } |
|
73 | 73 | } |
@@ -4,38 +4,38 @@ |
||
4 | 4 | |
5 | 5 | class CoreErrors |
6 | 6 | { |
7 | - public function connectionError() |
|
8 | - { |
|
9 | - abort(500, trans('core::errors.connectionError')); |
|
10 | - } |
|
11 | - |
|
12 | - public function redisNotRunning() |
|
13 | - { |
|
14 | - abort(500, trans('core::errors.redisNotRunning')); |
|
15 | - } |
|
16 | - |
|
17 | - public function dbQueryError() |
|
18 | - { |
|
19 | - abort(500, trans('core::errors.dbQueryError')); |
|
20 | - } |
|
21 | - |
|
22 | - public function cannotCreateSetting() |
|
23 | - { |
|
24 | - abort(400, trans('core::errors.cannotCreateSetting')); |
|
25 | - } |
|
26 | - |
|
27 | - public function cannotUpdateSettingKey() |
|
28 | - { |
|
29 | - abort(400, trans('core::errors.cannotUpdateSettingKey')); |
|
30 | - } |
|
31 | - |
|
32 | - public function notFound($text) |
|
33 | - { |
|
34 | - abort(404, trans('core::errors.notFound', ['replace' => $text])); |
|
35 | - } |
|
36 | - |
|
37 | - public function cannotUploadImage() |
|
38 | - { |
|
39 | - abort(400, trans('core::errors.cannotUploadImage')); |
|
40 | - } |
|
7 | + public function connectionError() |
|
8 | + { |
|
9 | + abort(500, trans('core::errors.connectionError')); |
|
10 | + } |
|
11 | + |
|
12 | + public function redisNotRunning() |
|
13 | + { |
|
14 | + abort(500, trans('core::errors.redisNotRunning')); |
|
15 | + } |
|
16 | + |
|
17 | + public function dbQueryError() |
|
18 | + { |
|
19 | + abort(500, trans('core::errors.dbQueryError')); |
|
20 | + } |
|
21 | + |
|
22 | + public function cannotCreateSetting() |
|
23 | + { |
|
24 | + abort(400, trans('core::errors.cannotCreateSetting')); |
|
25 | + } |
|
26 | + |
|
27 | + public function cannotUpdateSettingKey() |
|
28 | + { |
|
29 | + abort(400, trans('core::errors.cannotUpdateSettingKey')); |
|
30 | + } |
|
31 | + |
|
32 | + public function notFound($text) |
|
33 | + { |
|
34 | + abort(404, trans('core::errors.notFound', ['replace' => $text])); |
|
35 | + } |
|
36 | + |
|
37 | + public function cannotUploadImage() |
|
38 | + { |
|
39 | + abort(400, trans('core::errors.cannotUploadImage')); |
|
40 | + } |
|
41 | 41 | } |
@@ -8,307 +8,307 @@ |
||
8 | 8 | |
9 | 9 | class GenerateDocCommand extends Command |
10 | 10 | { |
11 | - /** |
|
12 | - * The name and signature of the console command. |
|
13 | - * |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $signature = 'doc:generate'; |
|
17 | - |
|
18 | - /** |
|
19 | - * The console command description. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - protected $description = 'Generate api documentation'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var ReprotService |
|
27 | - */ |
|
28 | - protected $reportService; |
|
29 | - |
|
30 | - /** |
|
31 | - * Init new object. |
|
32 | - * |
|
33 | - * @return void |
|
34 | - */ |
|
35 | - public function __construct(ReportService $reportService) |
|
36 | - { |
|
37 | - $this->reportService = $reportService; |
|
38 | - parent::__construct(); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * Execute the console command. |
|
43 | - * |
|
44 | - * @return mixed |
|
45 | - */ |
|
46 | - public function handle() |
|
47 | - { |
|
48 | - $docData = []; |
|
49 | - $docData['models'] = []; |
|
50 | - $routes = $this->getRoutes(); |
|
51 | - foreach ($routes as $route) { |
|
52 | - if ($route) { |
|
53 | - $actoinArray = explode('@', $route['action']); |
|
54 | - if (Arr::get($actoinArray, 1, false)) { |
|
55 | - $prefix = $route['prefix']; |
|
56 | - $module = \Str::camel(str_replace('/', '_', str_replace('api', '', $prefix))); |
|
57 | - if ($prefix === 'telescope') { |
|
58 | - continue; |
|
59 | - } |
|
60 | - |
|
61 | - $controller = $actoinArray[0]; |
|
62 | - $method = $actoinArray[1]; |
|
63 | - $route['name'] = $method; |
|
64 | - $reflectionClass = new \ReflectionClass($controller); |
|
65 | - $reflectionMethod = $reflectionClass->getMethod($method); |
|
66 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
67 | - $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
68 | - $modelName = explode('\\', $controller); |
|
69 | - $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
70 | - |
|
71 | - $this->processDocBlock($route, $reflectionMethod); |
|
72 | - $this->getHeaders($route, $method, $skipLoginCheck); |
|
73 | - $this->getPostData($route, $reflectionMethod, explode('\\', $reflectionClass->getName())[2], $modelName); |
|
74 | - |
|
75 | - $route['response'] = $this->getResponseObject($modelName, $route['name'], $route['returnDocBlock']); |
|
76 | - $docData['modules'][$module][] = $route; |
|
77 | - |
|
78 | - $this->getModels($modelName, $docData, $reflectionClass); |
|
79 | - } |
|
80 | - } |
|
81 | - } |
|
11 | + /** |
|
12 | + * The name and signature of the console command. |
|
13 | + * |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $signature = 'doc:generate'; |
|
17 | + |
|
18 | + /** |
|
19 | + * The console command description. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + protected $description = 'Generate api documentation'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var ReprotService |
|
27 | + */ |
|
28 | + protected $reportService; |
|
29 | + |
|
30 | + /** |
|
31 | + * Init new object. |
|
32 | + * |
|
33 | + * @return void |
|
34 | + */ |
|
35 | + public function __construct(ReportService $reportService) |
|
36 | + { |
|
37 | + $this->reportService = $reportService; |
|
38 | + parent::__construct(); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * Execute the console command. |
|
43 | + * |
|
44 | + * @return mixed |
|
45 | + */ |
|
46 | + public function handle() |
|
47 | + { |
|
48 | + $docData = []; |
|
49 | + $docData['models'] = []; |
|
50 | + $routes = $this->getRoutes(); |
|
51 | + foreach ($routes as $route) { |
|
52 | + if ($route) { |
|
53 | + $actoinArray = explode('@', $route['action']); |
|
54 | + if (Arr::get($actoinArray, 1, false)) { |
|
55 | + $prefix = $route['prefix']; |
|
56 | + $module = \Str::camel(str_replace('/', '_', str_replace('api', '', $prefix))); |
|
57 | + if ($prefix === 'telescope') { |
|
58 | + continue; |
|
59 | + } |
|
60 | + |
|
61 | + $controller = $actoinArray[0]; |
|
62 | + $method = $actoinArray[1]; |
|
63 | + $route['name'] = $method; |
|
64 | + $reflectionClass = new \ReflectionClass($controller); |
|
65 | + $reflectionMethod = $reflectionClass->getMethod($method); |
|
66 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
67 | + $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
68 | + $modelName = explode('\\', $controller); |
|
69 | + $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
70 | + |
|
71 | + $this->processDocBlock($route, $reflectionMethod); |
|
72 | + $this->getHeaders($route, $method, $skipLoginCheck); |
|
73 | + $this->getPostData($route, $reflectionMethod, explode('\\', $reflectionClass->getName())[2], $modelName); |
|
74 | + |
|
75 | + $route['response'] = $this->getResponseObject($modelName, $route['name'], $route['returnDocBlock']); |
|
76 | + $docData['modules'][$module][] = $route; |
|
77 | + |
|
78 | + $this->getModels($modelName, $docData, $reflectionClass); |
|
79 | + } |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | - $docData['errors'] = $this->getErrors(); |
|
84 | - $docData['reports'] = $this->reportService->all(); |
|
85 | - \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Get list of all registered routes. |
|
90 | - * |
|
91 | - * @return collection |
|
92 | - */ |
|
93 | - protected function getRoutes() |
|
94 | - { |
|
95 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
96 | - if (strpos($route->uri(), 'api/') !== false) { |
|
97 | - return [ |
|
98 | - 'method' => $route->methods()[0], |
|
99 | - 'uri' => $route->uri(), |
|
100 | - 'action' => $route->getActionName(), |
|
101 | - 'prefix' => $route->getPrefix() |
|
102 | - ]; |
|
103 | - } |
|
104 | - return false; |
|
105 | - })->all(); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Generate headers for the given route. |
|
110 | - * |
|
111 | - * @param array &$route |
|
112 | - * @param string $method |
|
113 | - * @param array $skipLoginCheck |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
117 | - { |
|
118 | - $route['headers'] = [ |
|
119 | - 'Accept' => 'application/json', |
|
120 | - 'Content-Type' => 'application/json', |
|
121 | - 'Accept-Language' => 'The language of the returned data: ar, en or all.', |
|
122 | - 'time-zone' => 'Your locale time zone', |
|
123 | - ]; |
|
124 | - |
|
125 | - |
|
126 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
127 | - $route['headers']['Authorization'] = 'Bearer {token}'; |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Generate description and params for the given route |
|
133 | - * based on the docblock. |
|
134 | - * |
|
135 | - * @param array &$route |
|
136 | - * @param \ReflectionMethod $reflectionMethod |
|
137 | - * @return void |
|
138 | - */ |
|
139 | - protected function processDocBlock(&$route, $reflectionMethod) |
|
140 | - { |
|
141 | - $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
142 | - $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
143 | - $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
144 | - $params = $docblock->getTagsByName('param'); |
|
145 | - $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
146 | - |
|
147 | - foreach ($params as $param) { |
|
148 | - $name = $param->getVariableName(); |
|
149 | - if ($name == 'perPage') { |
|
150 | - $route['parametars'][$name] = 'perPage? number of records per page default 15'; |
|
151 | - } elseif ($name == 'sortBy') { |
|
152 | - $route['parametars'][$name] = 'sortBy? sort column default created_at'; |
|
153 | - } elseif ($name == 'desc') { |
|
154 | - $route['parametars'][$name] = 'desc? sort descending or ascending default is descending'; |
|
155 | - } elseif ($name !== 'request') { |
|
156 | - $route['parametars'][$name] = $param->getDescription()->render(); |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - if ($route['name'] === 'index') { |
|
161 | - $route['parametars']['perPage'] = 'perPage? number of records per page default 15'; |
|
162 | - $route['parametars']['sortBy'] = 'sortBy? sort column default created_at'; |
|
163 | - $route['parametars']['desc'] = 'desc? sort descending or ascending default is descending'; |
|
164 | - $route['parametars']['trashed'] = 'trashed? retreive trashed or not default not'; |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Generate post body for the given route. |
|
170 | - * |
|
171 | - * @param array &$route |
|
172 | - * @param \ReflectionMethod $reflectionMethod |
|
173 | - * @param string $module |
|
174 | - * @param string $modelName |
|
175 | - * @return void |
|
176 | - */ |
|
177 | - protected function getPostData(&$route, $reflectionMethod, $module, $modelName) |
|
178 | - { |
|
179 | - $parameters = $reflectionMethod->getParameters(); |
|
180 | - $className = optional(optional(\Arr::get($parameters, 0))->getType())->getName(); |
|
181 | - if ($className) { |
|
182 | - $reflectionClass = new \ReflectionClass($className); |
|
183 | - } elseif (in_array($reflectionMethod->getName(), ['store', 'update'])) { |
|
184 | - $className = 'App\\Modules\\' . ucfirst($module) . '\\Http\\Requests\\Store' . ucfirst($modelName); |
|
185 | - $reflectionClass = new \ReflectionClass($className); |
|
186 | - } |
|
187 | - |
|
188 | - if (isset($reflectionClass) && $reflectionClass->hasMethod('rules')) { |
|
189 | - $reflectionMethod = $reflectionClass->getMethod('rules'); |
|
190 | - $route['body'] = $reflectionMethod->invoke(new $className); |
|
191 | - |
|
192 | - foreach ($route['body'] as &$rule) { |
|
193 | - if (strpos($rule, 'unique')) { |
|
194 | - $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
195 | - } elseif (strpos($rule, 'exists')) { |
|
196 | - $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Generate application errors. |
|
204 | - * |
|
205 | - * @return array |
|
206 | - */ |
|
207 | - protected function getErrors() |
|
208 | - { |
|
209 | - $errors = []; |
|
210 | - foreach (\Module::all() as $module) { |
|
211 | - $nameSpace = 'App\\Modules\\' . $module['basename']; |
|
212 | - $class = $nameSpace . '\\Errors\\' . $module['basename'] . 'Errors'; |
|
213 | - $reflectionClass = new \ReflectionClass($class); |
|
214 | - foreach ($reflectionClass->getMethods() as $method) { |
|
215 | - $methodName = $method->name; |
|
216 | - $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
217 | - $body = $this->getMethodBody($reflectionMethod); |
|
218 | - |
|
219 | - preg_match('/abort\(([^#]+)\,/iU', $body, $match); |
|
220 | - |
|
221 | - if (count($match)) { |
|
222 | - $errors[$match[1]][] = $methodName; |
|
223 | - } |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - return $errors; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Get the given method body code. |
|
232 | - * |
|
233 | - * @param object $reflectionMethod |
|
234 | - * @return string |
|
235 | - */ |
|
236 | - protected function getMethodBody($reflectionMethod) |
|
237 | - { |
|
238 | - $filename = $reflectionMethod->getFileName(); |
|
239 | - $start_line = $reflectionMethod->getStartLine() - 1; |
|
240 | - $end_line = $reflectionMethod->getEndLine(); |
|
241 | - $length = $end_line - $start_line; |
|
242 | - $source = file($filename); |
|
243 | - $body = implode("", array_slice($source, $start_line, $length)); |
|
244 | - $body = trim(preg_replace('/\s+/', '', $body)); |
|
245 | - |
|
246 | - return $body; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Get example object of all availble models. |
|
251 | - * |
|
252 | - * @param string $modelName |
|
253 | - * @param array $docData |
|
254 | - * @return string |
|
255 | - */ |
|
256 | - protected function getModels($modelName, &$docData, $reflectionClass) |
|
257 | - { |
|
258 | - if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
259 | - $repo = call_user_func_array("\Core::{$modelName}", []); |
|
260 | - if (! $repo) { |
|
261 | - return; |
|
262 | - } |
|
83 | + $docData['errors'] = $this->getErrors(); |
|
84 | + $docData['reports'] = $this->reportService->all(); |
|
85 | + \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Get list of all registered routes. |
|
90 | + * |
|
91 | + * @return collection |
|
92 | + */ |
|
93 | + protected function getRoutes() |
|
94 | + { |
|
95 | + return collect(\Route::getRoutes())->map(function ($route) { |
|
96 | + if (strpos($route->uri(), 'api/') !== false) { |
|
97 | + return [ |
|
98 | + 'method' => $route->methods()[0], |
|
99 | + 'uri' => $route->uri(), |
|
100 | + 'action' => $route->getActionName(), |
|
101 | + 'prefix' => $route->getPrefix() |
|
102 | + ]; |
|
103 | + } |
|
104 | + return false; |
|
105 | + })->all(); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Generate headers for the given route. |
|
110 | + * |
|
111 | + * @param array &$route |
|
112 | + * @param string $method |
|
113 | + * @param array $skipLoginCheck |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
117 | + { |
|
118 | + $route['headers'] = [ |
|
119 | + 'Accept' => 'application/json', |
|
120 | + 'Content-Type' => 'application/json', |
|
121 | + 'Accept-Language' => 'The language of the returned data: ar, en or all.', |
|
122 | + 'time-zone' => 'Your locale time zone', |
|
123 | + ]; |
|
124 | + |
|
125 | + |
|
126 | + if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
127 | + $route['headers']['Authorization'] = 'Bearer {token}'; |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Generate description and params for the given route |
|
133 | + * based on the docblock. |
|
134 | + * |
|
135 | + * @param array &$route |
|
136 | + * @param \ReflectionMethod $reflectionMethod |
|
137 | + * @return void |
|
138 | + */ |
|
139 | + protected function processDocBlock(&$route, $reflectionMethod) |
|
140 | + { |
|
141 | + $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
142 | + $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
143 | + $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
144 | + $params = $docblock->getTagsByName('param'); |
|
145 | + $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
146 | + |
|
147 | + foreach ($params as $param) { |
|
148 | + $name = $param->getVariableName(); |
|
149 | + if ($name == 'perPage') { |
|
150 | + $route['parametars'][$name] = 'perPage? number of records per page default 15'; |
|
151 | + } elseif ($name == 'sortBy') { |
|
152 | + $route['parametars'][$name] = 'sortBy? sort column default created_at'; |
|
153 | + } elseif ($name == 'desc') { |
|
154 | + $route['parametars'][$name] = 'desc? sort descending or ascending default is descending'; |
|
155 | + } elseif ($name !== 'request') { |
|
156 | + $route['parametars'][$name] = $param->getDescription()->render(); |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + if ($route['name'] === 'index') { |
|
161 | + $route['parametars']['perPage'] = 'perPage? number of records per page default 15'; |
|
162 | + $route['parametars']['sortBy'] = 'sortBy? sort column default created_at'; |
|
163 | + $route['parametars']['desc'] = 'desc? sort descending or ascending default is descending'; |
|
164 | + $route['parametars']['trashed'] = 'trashed? retreive trashed or not default not'; |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Generate post body for the given route. |
|
170 | + * |
|
171 | + * @param array &$route |
|
172 | + * @param \ReflectionMethod $reflectionMethod |
|
173 | + * @param string $module |
|
174 | + * @param string $modelName |
|
175 | + * @return void |
|
176 | + */ |
|
177 | + protected function getPostData(&$route, $reflectionMethod, $module, $modelName) |
|
178 | + { |
|
179 | + $parameters = $reflectionMethod->getParameters(); |
|
180 | + $className = optional(optional(\Arr::get($parameters, 0))->getType())->getName(); |
|
181 | + if ($className) { |
|
182 | + $reflectionClass = new \ReflectionClass($className); |
|
183 | + } elseif (in_array($reflectionMethod->getName(), ['store', 'update'])) { |
|
184 | + $className = 'App\\Modules\\' . ucfirst($module) . '\\Http\\Requests\\Store' . ucfirst($modelName); |
|
185 | + $reflectionClass = new \ReflectionClass($className); |
|
186 | + } |
|
187 | + |
|
188 | + if (isset($reflectionClass) && $reflectionClass->hasMethod('rules')) { |
|
189 | + $reflectionMethod = $reflectionClass->getMethod('rules'); |
|
190 | + $route['body'] = $reflectionMethod->invoke(new $className); |
|
191 | + |
|
192 | + foreach ($route['body'] as &$rule) { |
|
193 | + if (strpos($rule, 'unique')) { |
|
194 | + $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
195 | + } elseif (strpos($rule, 'exists')) { |
|
196 | + $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Generate application errors. |
|
204 | + * |
|
205 | + * @return array |
|
206 | + */ |
|
207 | + protected function getErrors() |
|
208 | + { |
|
209 | + $errors = []; |
|
210 | + foreach (\Module::all() as $module) { |
|
211 | + $nameSpace = 'App\\Modules\\' . $module['basename']; |
|
212 | + $class = $nameSpace . '\\Errors\\' . $module['basename'] . 'Errors'; |
|
213 | + $reflectionClass = new \ReflectionClass($class); |
|
214 | + foreach ($reflectionClass->getMethods() as $method) { |
|
215 | + $methodName = $method->name; |
|
216 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
217 | + $body = $this->getMethodBody($reflectionMethod); |
|
218 | + |
|
219 | + preg_match('/abort\(([^#]+)\,/iU', $body, $match); |
|
220 | + |
|
221 | + if (count($match)) { |
|
222 | + $errors[$match[1]][] = $methodName; |
|
223 | + } |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + return $errors; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Get the given method body code. |
|
232 | + * |
|
233 | + * @param object $reflectionMethod |
|
234 | + * @return string |
|
235 | + */ |
|
236 | + protected function getMethodBody($reflectionMethod) |
|
237 | + { |
|
238 | + $filename = $reflectionMethod->getFileName(); |
|
239 | + $start_line = $reflectionMethod->getStartLine() - 1; |
|
240 | + $end_line = $reflectionMethod->getEndLine(); |
|
241 | + $length = $end_line - $start_line; |
|
242 | + $source = file($filename); |
|
243 | + $body = implode("", array_slice($source, $start_line, $length)); |
|
244 | + $body = trim(preg_replace('/\s+/', '', $body)); |
|
245 | + |
|
246 | + return $body; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Get example object of all availble models. |
|
251 | + * |
|
252 | + * @param string $modelName |
|
253 | + * @param array $docData |
|
254 | + * @return string |
|
255 | + */ |
|
256 | + protected function getModels($modelName, &$docData, $reflectionClass) |
|
257 | + { |
|
258 | + if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
259 | + $repo = call_user_func_array("\Core::{$modelName}", []); |
|
260 | + if (! $repo) { |
|
261 | + return; |
|
262 | + } |
|
263 | 263 | |
264 | - $modelClass = get_class($repo->model); |
|
265 | - $model = factory($modelClass)->make(); |
|
266 | - |
|
267 | - $property = $reflectionClass->getProperty('modelResource'); |
|
268 | - $property->setAccessible(true); |
|
269 | - $modelResource = $property->getValue(\App::make($reflectionClass->getName())); |
|
270 | - |
|
271 | - $relations = []; |
|
272 | - $relationMethods = ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany', 'morphToMany', 'morphTo']; |
|
273 | - $reflector = new \ReflectionClass($model); |
|
274 | - foreach ($reflector->getMethods() as $reflectionMethod) { |
|
275 | - $body = $this->getMethodBody($reflectionMethod); |
|
276 | - foreach ($relationMethods as $relationMethod) { |
|
277 | - $relation = $reflectionMethod->getName(); |
|
278 | - if (strpos($body, '$this->' . $relationMethod) && $relation !== 'morphedByMany') { |
|
279 | - $relations[] = $relation; |
|
280 | - break; |
|
281 | - } |
|
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - $modelResource = new $modelResource($model->load($relations)); |
|
286 | - $modelArr = $modelResource->toArray([]); |
|
287 | - |
|
288 | - foreach ($modelArr as $key => $attr) { |
|
289 | - if (is_object($attr) && property_exists($attr, 'resource') && $attr->resource instanceof \Illuminate\Http\Resources\MissingValue) { |
|
290 | - unset($modelArr[$key]); |
|
291 | - } |
|
292 | - } |
|
293 | - |
|
294 | - $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Get the route response object type. |
|
300 | - * |
|
301 | - * @param string $modelName |
|
302 | - * @param string $method |
|
303 | - * @param string $returnDocBlock |
|
304 | - * @return array |
|
305 | - */ |
|
306 | - protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
307 | - { |
|
308 | - $relations = config('core.relations'); |
|
309 | - $relations = Arr::has($relations, $modelName) ? Arr::has($relations[$modelName], $method) ? $relations[$modelName] : false : false; |
|
310 | - $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
311 | - |
|
312 | - return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
313 | - } |
|
264 | + $modelClass = get_class($repo->model); |
|
265 | + $model = factory($modelClass)->make(); |
|
266 | + |
|
267 | + $property = $reflectionClass->getProperty('modelResource'); |
|
268 | + $property->setAccessible(true); |
|
269 | + $modelResource = $property->getValue(\App::make($reflectionClass->getName())); |
|
270 | + |
|
271 | + $relations = []; |
|
272 | + $relationMethods = ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany', 'morphToMany', 'morphTo']; |
|
273 | + $reflector = new \ReflectionClass($model); |
|
274 | + foreach ($reflector->getMethods() as $reflectionMethod) { |
|
275 | + $body = $this->getMethodBody($reflectionMethod); |
|
276 | + foreach ($relationMethods as $relationMethod) { |
|
277 | + $relation = $reflectionMethod->getName(); |
|
278 | + if (strpos($body, '$this->' . $relationMethod) && $relation !== 'morphedByMany') { |
|
279 | + $relations[] = $relation; |
|
280 | + break; |
|
281 | + } |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + $modelResource = new $modelResource($model->load($relations)); |
|
286 | + $modelArr = $modelResource->toArray([]); |
|
287 | + |
|
288 | + foreach ($modelArr as $key => $attr) { |
|
289 | + if (is_object($attr) && property_exists($attr, 'resource') && $attr->resource instanceof \Illuminate\Http\Resources\MissingValue) { |
|
290 | + unset($modelArr[$key]); |
|
291 | + } |
|
292 | + } |
|
293 | + |
|
294 | + $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Get the route response object type. |
|
300 | + * |
|
301 | + * @param string $modelName |
|
302 | + * @param string $method |
|
303 | + * @param string $returnDocBlock |
|
304 | + * @return array |
|
305 | + */ |
|
306 | + protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
307 | + { |
|
308 | + $relations = config('core.relations'); |
|
309 | + $relations = Arr::has($relations, $modelName) ? Arr::has($relations[$modelName], $method) ? $relations[$modelName] : false : false; |
|
310 | + $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
311 | + |
|
312 | + return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
313 | + } |
|
314 | 314 | } |