1
|
|
|
<?php |
2
|
|
|
namespace App\Modules\V1\Core\Http\Controllers; |
3
|
|
|
|
4
|
|
|
use App\Http\Controllers\Controller; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
class ApiDocumentController extends Controller |
8
|
|
|
{ |
9
|
|
|
public function index() |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
$path = str_replace($_SERVER['DOCUMENT_ROOT'], '',str_replace('\\', '/', __FILE__)); |
12
|
|
|
$baseUrl = str_replace('Http/Controllers/ApiDocumentController.php', '', $path); |
13
|
|
|
$jsonDoc = json_decode(file_get_contents(app_path('Modules/V1/Core/Resources/api.json')), true); |
14
|
|
|
$modules = $jsonDoc['modules']; |
15
|
|
|
$errors = $jsonDoc['errors']; |
16
|
|
|
$conditions = [ |
17
|
|
|
[ |
18
|
|
|
"title" => "email equal [email protected]:", |
19
|
|
|
"content" => "{\n \"email\" =>\"[email protected]\"\n}" |
20
|
|
|
], |
21
|
|
|
[ |
22
|
|
|
"title" => "email equal [email protected] and user is blocked:", |
23
|
|
|
"content" => "{\n \"and\":{\n \"email\" =>\"[email protected]\",\n \"blocked\" =>1\n }\n}" |
24
|
|
|
], |
25
|
|
|
[ |
26
|
|
|
"title" => "email equal [email protected] or user is blocked:", |
27
|
|
|
"content" => "{\n \"or\":{\n \"email\" =>\"[email protected]\",\n \"blocked\" =>1\n {\n}" |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
"title" => "email contain John:", |
31
|
|
|
"content" => "{\n \"email\" =>{\n \"op\" =>\"like\",\n \"val\" =>\"%John%\"\n }\n}" |
32
|
|
|
], |
33
|
|
|
[ |
34
|
|
|
"title" => "user created after 2016-10-25:", |
35
|
|
|
"content" => "{\n \"created_at\" =>{\n \"op\" =>\">\",\n \"val\" =>\"2016-10-25\"\n }\n}" |
36
|
|
|
], |
37
|
|
|
[ |
38
|
|
|
"title" => "user created between 2016-10-20 and 2016-10-25:", |
39
|
|
|
"content" => "{\n \"created_at\" =>{\n \"op\" =>\"between\",\n \"val1\" =>\"2016-10-20\",\n \"val2\" =>\"2016-10-25\"\n {\n}" |
40
|
|
|
], |
41
|
|
|
[ |
42
|
|
|
"title" => "user id in 1,2,3:", |
43
|
|
|
"content" => "{\n \"id\" =>{\n \"op\" =>\"in\",\n \"val\" =>[1, 2, 3]\n}" |
44
|
|
|
], |
45
|
|
|
[ |
46
|
|
|
"title" => "user name is null:", |
47
|
|
|
"content" => "{\n \"name\" =>{\n \"op\" =>\"null\"\n}" |
48
|
|
|
], |
49
|
|
|
[ |
50
|
|
|
"title" => "user name is not null:", |
51
|
|
|
"content" => "{\n \"name\" =>{\n \"op\" =>\"not null\"\n}" |
52
|
|
|
], |
53
|
|
|
[ |
54
|
|
|
"title" => "user has group admin:", |
55
|
|
|
"content" => "{\n \"groups\" =>{\n \"op\" =>\"has\",\n \"val\" =>{\n \t\"name\" =>\"Admin\"\n }\n}" |
56
|
|
|
] |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
return view('core::doc', ['baseUrl' => $baseUrl, 'modules' => $modules, 'errors' => $errors, 'conditions' => $conditions]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testApi(Request $request) |
63
|
|
|
{ |
64
|
|
|
$actoinArray = explode('@', $request->get('action')); |
65
|
|
|
$controller = $actoinArray[0]; |
66
|
|
|
$method = $actoinArray[1]; |
67
|
|
|
$method = $method !== 'index' ? $method : 'all'; |
68
|
|
|
$reflectionClass = new \ReflectionClass($controller); |
69
|
|
|
$classProperties = $reflectionClass->getDefaultProperties(); |
70
|
|
|
|
71
|
|
|
return \Response::json(call_user_func_array("\Core::{$classProperties['model']}", [])->$method(), 200);; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: