Completed
Push — master ( 12abac...a4ad23 )
by Marcel
05:10
created

LaravelGenerator::getMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
nc 2
cc 2
eloc 4
nop 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Generators;
4
5
use ReflectionClass;
6
use Illuminate\Routing\Route;
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Foundation\Http\FormRequest;
10
11
class LaravelGenerator extends AbstractGenerator
12
{
13
    /**
14
     * @param Route $route
15
     *
16
     * @return mixed
17
     */
18
    public function getUri($route)
19
    {
20
        if (version_compare(app()->version(), '5.4', '<')) {
21
            return $route->getUri();
0 ignored issues
show
Bug introduced by
The method getUri() does not seem to exist on object<Illuminate\Routing\Route>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
22
        }
23
        return $route->uri();
24
    }
25
26
    /**
27
     * @param Route $route
28
     *
29
     * @return mixed
30
     */
31
    public function getMethods($route)
32
    {
33
        if (version_compare(app()->version(), '5.4', '<')) {
34
            return $route->getMethods();
0 ignored issues
show
Bug introduced by
The method getMethods() does not exist on Illuminate\Routing\Route. Did you maybe mean methods()?

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.

Loading history...
35
        }
36
        return $route->methods();
37
    }
38
39
    /**
40
     * @param  \Illuminate\Routing\Route $route
41
     * @param array $bindings
42
     * @param array $headers
43
     * @param bool $withResponse
44
     *
45
     * @return array
46
     */
47
    public function processRoute($route, $bindings = [], $headers = [], $withResponse = true)
48
    {
49
        $content = '';
50
51
        $routeAction = $route->getAction();
52
        $routeGroup = $this->getRouteGroup($routeAction['uses']);
53
        $routeDescription = $this->getRouteDescription($routeAction['uses']);
54
55
        if ($withResponse) {
56
            $response = $this->getRouteResponse($route, $bindings, $headers);
57
            if ($response->headers->get('Content-Type') === 'application/json') {
58
                $content = json_encode(json_decode($response->getContent()), JSON_PRETTY_PRINT);
59
            } else {
60
                $content = $response->getContent();
61
            }
62
        }
63
64
        return $this->getParameters([
65
            'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))),
66
            'resource' => $routeGroup,
67
            'title' => $routeDescription['short'],
68
            'description' => $routeDescription['long'],
69
            'methods' => $this->getMethods($route),
70
            'uri' => $this->getUri($route),
71
            'parameters' => [],
72
            'response' => $content,
73
        ], $routeAction, $bindings);
74
    }
75
76
    /**
77
     * Prepares / Disables route middlewares.
78
     *
79
     * @param  bool $disable
80
     *
81
     * @return  void
82
     */
83
    public function prepareMiddleware($disable = true)
84
    {
85
        App::instance('middleware.disable', true);
0 ignored issues
show
Bug introduced by
The method instance() does not exist on Illuminate\Support\Facades\App. Did you maybe mean clearResolvedInstance()?

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.

Loading history...
86
    }
87
88
    /**
89
     * Call the given URI and return the Response.
90
     *
91
     * @param  string  $method
92
     * @param  string  $uri
93
     * @param  array  $parameters
94
     * @param  array  $cookies
95
     * @param  array  $files
96
     * @param  array  $server
97
     * @param  string  $content
98
     *
99
     * @return \Illuminate\Http\Response
100
     */
101
    public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
102
    {
103
        $server = collect([
104
            'CONTENT_TYPE' => 'application/json',
105
            'Accept' => 'application/json',
106
        ])->merge($server)->toArray();
107
108
        $request = Request::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Support\Facades\Request. Did you maybe mean createFreshMockInstance()?

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.

Loading history...
109
            $uri, $method, $parameters,
110
            $cookies, $files, $this->transformHeadersToServerVars($server), $content
111
        );
112
113
        $kernel = App::make('Illuminate\Contracts\Http\Kernel');
114
        $response = $kernel->handle($request);
115
116
        $kernel->terminate($request, $response);
117
118
        if (file_exists($file = App::bootstrapPath().'/app.php')) {
119
            $app = require $file;
120
            $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
121
        }
122
123
        return $response;
124
    }
125
126
    /**
127
     * @param  string $route
128
     * @param  array $bindings
129
     *
130
     * @return array
131
     */
132
    protected function getRouteRules($route, $bindings)
133
    {
134
        list($class, $method) = explode('@', $route);
135
        $reflection = new ReflectionClass($class);
136
        $reflectionMethod = $reflection->getMethod($method);
137
138
        foreach ($reflectionMethod->getParameters() as $parameter) {
139
            $parameterType = $parameter->getClass();
140
            if (! is_null($parameterType) && class_exists($parameterType->name)) {
141
                $className = $parameterType->name;
142
143
                if (is_subclass_of($className, FormRequest::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Illuminate\Foundation\Http\FormRequest::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
144
                    $parameterReflection = new $className;
145
                    $parameterReflection->setContainer(app());
146
                    // Add route parameter bindings
147
                    $parameterReflection->query->add($bindings);
148
                    $parameterReflection->request->add($bindings);
149
150
                    if (method_exists($parameterReflection, 'validator')) {
151
                        return app()->call([$parameterReflection, 'validator'])
152
                            ->getRules();
153
                    } else {
154
                        return app()->call([$parameterReflection, 'rules']);
155
                    }
156
                }
157
            }
158
        }
159
160
        return [];
161
    }
162
}
163