1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Route; |
6
|
|
|
use Illuminate\Support\Facades\App; |
7
|
|
|
use Illuminate\Support\Facades\Request; |
8
|
|
|
|
9
|
|
|
class LaravelGenerator extends AbstractGenerator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param Route $route |
13
|
|
|
* |
14
|
|
|
* @return mixed |
15
|
|
|
*/ |
16
|
|
|
protected function getUri($route) |
17
|
|
|
{ |
18
|
|
|
return $route->getUri(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param \Illuminate\Routing\Route $route |
23
|
|
|
* @param array $bindings |
24
|
|
|
* @param array $headers |
25
|
|
|
* @param bool $withResponse |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function processRoute($route, $bindings = [], $headers = [], $withResponse = true) |
30
|
|
|
{ |
31
|
|
|
$content = ''; |
32
|
|
|
|
33
|
|
|
$routeAction = $route->getAction(); |
34
|
|
|
$routeGroup = $this->getRouteGroup($routeAction['uses']); |
35
|
|
|
$routeDescription = $this->getRouteDescription($routeAction['uses']); |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
if ($withResponse) { |
39
|
|
|
$response = $this->getRouteResponse($route, $bindings, $headers); |
40
|
|
|
if ($response->headers->get('Content-Type') === 'application/json') { |
41
|
|
|
$content = json_encode(json_decode($response->getContent()), JSON_PRETTY_PRINT); |
42
|
|
|
} else { |
43
|
|
|
$content = $response->getContent(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->getParameters([ |
48
|
|
|
'id' => md5($route->getUri().':'.implode($route->getMethods())), |
49
|
|
|
'resource' => $routeGroup, |
50
|
|
|
'title' => $routeDescription['short'], |
51
|
|
|
'description' => $routeDescription['long'], |
52
|
|
|
'methods' => $route->getMethods(), |
53
|
|
|
'uri' => $route->getUri(), |
54
|
|
|
'parameters' => [], |
55
|
|
|
'response' => $content, |
56
|
|
|
], $routeAction, $bindings); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Prepares / Disables route middlewares. |
61
|
|
|
* |
62
|
|
|
* @param bool $disable |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function prepareMiddleware($disable = true) |
67
|
|
|
{ |
68
|
|
|
App::instance('middleware.disable', true); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Call the given URI and return the Response. |
73
|
|
|
* |
74
|
|
|
* @param string $method |
75
|
|
|
* @param string $uri |
76
|
|
|
* @param array $parameters |
77
|
|
|
* @param array $cookies |
78
|
|
|
* @param array $files |
79
|
|
|
* @param array $server |
80
|
|
|
* @param string $content |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Http\Response |
83
|
|
|
*/ |
84
|
|
|
public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null) |
85
|
|
|
{ |
86
|
|
|
$server = collect([ |
87
|
|
|
'CONTENT_TYPE' => 'application/json', |
88
|
|
|
'Accept' => 'application/json', |
89
|
|
|
])->merge($server)->toArray(); |
90
|
|
|
|
91
|
|
|
$request = Request::create( |
|
|
|
|
92
|
|
|
$uri, $method, $parameters, |
93
|
|
|
$cookies, $files, $this->transformHeadersToServerVars($server), $content |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$kernel = App::make('Illuminate\Contracts\Http\Kernel'); |
97
|
|
|
$response = $kernel->handle($request); |
98
|
|
|
|
99
|
|
|
$kernel->terminate($request, $response); |
100
|
|
|
|
101
|
|
|
if (file_exists($file = App::bootstrapPath().'/app.php')) { |
102
|
|
|
$app = require $file; |
103
|
|
|
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $response; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $route |
111
|
|
|
* @param array $bindings |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
protected function getRouteRules($route, $bindings) |
116
|
|
|
{ |
117
|
|
|
list($class, $method) = explode('@', $route); |
118
|
|
|
$reflection = new ReflectionClass($class); |
119
|
|
|
$reflectionMethod = $reflection->getMethod($method); |
120
|
|
|
|
121
|
|
|
foreach ($reflectionMethod->getParameters() as $parameter) { |
122
|
|
|
$parameterType = $parameter->getClass(); |
123
|
|
|
if (! is_null($parameterType) && class_exists($parameterType->name)) { |
124
|
|
|
$className = $parameterType->name; |
125
|
|
|
|
126
|
|
|
if (is_subclass_of($className, FormRequest::class)) { |
|
|
|
|
127
|
|
|
$parameterReflection = new $className; |
128
|
|
|
$parameterReflection->setContainer(app()); |
129
|
|
|
// Add route parameter bindings |
130
|
|
|
$parameterReflection->query->add($bindings); |
131
|
|
|
$parameterReflection->request->add($bindings); |
132
|
|
|
|
133
|
|
|
if (method_exists($parameterReflection, 'validator')) { |
134
|
|
|
return app()->call([$parameterReflection, 'validator']) |
135
|
|
|
->getRules(); |
136
|
|
|
} else { |
137
|
|
|
return app()->call([$parameterReflection, 'rules']); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return []; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.