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