Completed
Pull Request — master (#134)
by mark
10:17 queued 08:18
created

LaravelGenerator::getRouteRules()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.439
cc 6
eloc 19
nc 5
nop 2
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
        if ($withResponse) {
38
            $response = $this->getRouteResponse($route, $bindings, $headers);
39
            if ($response->headers->get('Content-Type') === 'application/json') {
40
                $content = json_encode(json_decode($response->getContent()), JSON_PRETTY_PRINT);
41
            } else {
42
                $content = $response->getContent();
43
            }
44
        }
45
46
        return $this->getParameters([
47
            'id' => md5($route->getUri().':'.implode($route->getMethods())),
48
            'resource' => $routeGroup,
49
            'title' => $routeDescription['short'],
50
            'description' => $routeDescription['long'],
51
            'methods' => $route->getMethods(),
52
            'uri' => $route->getUri(),
53
            'parameters' => [],
54
            'response' => $content,
55
        ], $routeAction, $bindings);
56
    }
57
58
    /**
59
     * Prepares / Disables route middlewares.
60
     *
61
     * @param  bool $disable
62
     *
63
     * @return  void
64
     */
65
    public function prepareMiddleware($disable = true)
66
    {
67
        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...
68
    }
69
70
    /**
71
     * Call the given URI and return the Response.
72
     *
73
     * @param  string  $method
74
     * @param  string  $uri
75
     * @param  array  $parameters
76
     * @param  array  $cookies
77
     * @param  array  $files
78
     * @param  array  $server
79
     * @param  string  $content
80
     *
81
     * @return \Illuminate\Http\Response
82
     */
83
    public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
84
    {
85
        $server = collect([
86
            'CONTENT_TYPE' => 'application/json',
87
            'Accept' => 'application/json',
88
        ])->merge($server)->toArray();
89
90
        $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...
91
            $uri, $method, $parameters,
92
            $cookies, $files, $this->transformHeadersToServerVars($server), $content
93
        );
94
95
        $kernel = App::make('Illuminate\Contracts\Http\Kernel');
96
        $response = $kernel->handle($request);
97
98
        $kernel->terminate($request, $response);
99
100
        if (file_exists($file = App::bootstrapPath().'/app.php')) {
101
            $app = require $file;
102
            $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
103
        }
104
105
        return $response;
106
    }
107
108
    /**
109
     * @param  string $route
110
     * @param  array $bindings
111
     *
112
     * @return array
113
     */
114
    protected function getRouteRules($route, $bindings)
115
    {
116
        list($class, $method) = explode('@', $route);
117
        $reflection = new ReflectionClass($class);
118
        $reflectionMethod = $reflection->getMethod($method);
119
120
        foreach ($reflectionMethod->getParameters() as $parameter) {
121
            $parameterType = $parameter->getClass();
122
            if (! is_null($parameterType) && class_exists($parameterType->name)) {
123
                $className = $parameterType->name;
124
125
                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 \Mpociot\ApiDoc\Generators\FormRequest::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
126
                    $parameterReflection = new $className;
127
                    $parameterReflection->setContainer(app());
128
                    // Add route parameter bindings
129
                    $parameterReflection->query->add($bindings);
130
                    $parameterReflection->request->add($bindings);
131
132
                    if (method_exists($parameterReflection, 'validator')) {
133
                        return app()->call([$parameterReflection, 'validator'])
134
                            ->getRules();
135
                    } else {
136
                        return app()->call([$parameterReflection, 'rules']);
137
                    }
138
                }
139
            }
140
        }
141
142
        return [];
143
    }
144
}
145