Maurits van der Schee 5 years ago
parent
commit
7e72405e0f
3 changed files with 6 additions and 188 deletions
  1. 3
    0
      api.php
  2. 0
    188
      src/Tqdev/PhpCrudApi/Request.old
  3. 3
    0
      src/Tqdev/PhpCrudApi/RequestFactory.php

+ 3
- 0
api.php View File

@@ -7243,6 +7243,9 @@ class RequestFactory
7243 7243
         $psr17Factory = new Psr17Factory();
7244 7244
         $creator = new ServerRequestCreator($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
7245 7245
         $serverRequest = $creator->fromGlobals();
7246
+        if (isset($_SERVER['PATH_INFO'])) {
7247
+            $serverRequest = $serverRequest->withUri($psr17Factory->createUri($_SERVER['PATH_INFO'] . '?' . $_SERVER['QUERY_STRING']));
7248
+        }
7246 7249
         $body = file_get_contents('php://input');
7247 7250
         if ($body) {
7248 7251
             $serverRequest = $serverRequest->withParsedBody(self::parseBody($body));

+ 0
- 188
src/Tqdev/PhpCrudApi/Request.old View File

@@ -1,188 +0,0 @@
1
-<?php
2
-namespace Tqdev\PhpCrudApi;
3
-
4
-class Request
5
-{
6
-    private $method;
7
-    private $path;
8
-    private $pathSegments;
9
-    private $params;
10
-    private $body;
11
-    private $headers;
12
-    private $highPerformance;
13
-
14
-    public function __construct(string $method = null, string $path = null, string $query = null, array $headers = null, string $body = null, bool $highPerformance = true)
15
-    {
16
-        $this->parseMethod($method);
17
-        $this->parsePath($path);
18
-        $this->parseParams($query);
19
-        $this->parseHeaders($headers);
20
-        $this->parseBody($body);
21
-        $this->highPerformance = $highPerformance;
22
-    }
23
-
24
-    private function parseMethod(string $method = null)
25
-    {
26
-        if (!$method) {
27
-            if (isset($_SERVER['REQUEST_METHOD'])) {
28
-                $method = $_SERVER['REQUEST_METHOD'];
29
-            } else {
30
-                $method = 'GET';
31
-            }
32
-        }
33
-        $this->method = $method;
34
-    }
35
-
36
-    private function parsePath(string $path = null)
37
-    {
38
-        if (!$path) {
39
-            if (isset($_SERVER['PATH_INFO'])) {
40
-                $path = $_SERVER['PATH_INFO'];
41
-            } else {
42
-                $path = '/';
43
-            }
44
-        }
45
-        $this->path = $path;
46
-        $this->pathSegments = explode('/', $path);
47
-    }
48
-
49
-    private function parseParams(string $query = null)
50
-    {
51
-        if (!$query) {
52
-            if (isset($_SERVER['QUERY_STRING'])) {
53
-                $query = $_SERVER['QUERY_STRING'];
54
-            } else {
55
-                $query = '';
56
-            }
57
-        }
58
-        $query = str_replace('][]=', ']=', str_replace('=', '[]=', $query));
59
-        parse_str($query, $this->params);
60
-    }
61
-
62
-    private function parseHeaders(array $headers = null)
63
-    {
64
-        if (!$headers) {
65
-            $headers = array();
66
-            if (!$this->highPerformance) {
67
-                foreach ($_SERVER as $name => $value) {
68
-                    if (substr($name, 0, 5) == 'HTTP_') {
69
-                        $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
70
-                        $headers[$key] = $value;
71
-                    }
72
-                }
73
-            }
74
-        }
75
-        $this->headers = $headers;
76
-    }
77
-
78
-    private function decodeBody(string $body) /*: ?object*/
79
-    {
80
-        $first = substr($body, 0, 1);
81
-        if ($first == '[' || $first == '{') {
82
-            $object = json_decode($body);
83
-            $causeCode = json_last_error();
84
-            if ($causeCode !== JSON_ERROR_NONE) {
85
-                $object = null;
86
-            }
87
-        } else {
88
-            parse_str($body, $input);
89
-            foreach ($input as $key => $value) {
90
-                if (substr($key, -9) == '__is_null') {
91
-                    $input[substr($key, 0, -9)] = null;
92
-                    unset($input[$key]);
93
-                }
94
-            }
95
-            $object = (object) $input;
96
-        }
97
-        return $object;
98
-    }
99
-
100
-    private function parseBody(string $body = null) /*: void*/
101
-    {
102
-        if (!$body) {
103
-            $body = file_get_contents('php://input');
104
-        }
105
-        $this->body = $this->decodeBody($body);
106
-    }
107
-
108
-    public function getMethod(): string
109
-    {
110
-        return $this->method;
111
-    }
112
-
113
-    public function getPath(): string
114
-    {
115
-        return $this->path;
116
-    }
117
-
118
-    public function getPathSegment(int $part): string
119
-    {
120
-        if ($part < 0 || $part >= count($this->pathSegments)) {
121
-            return '';
122
-        }
123
-        return $this->pathSegments[$part];
124
-    }
125
-
126
-    public function getParams(): array
127
-    {
128
-        return $this->params;
129
-    }
130
-
131
-    public function setParams(array $params) /*: void*/
132
-    {
133
-        $this->params = $params;
134
-    }
135
-
136
-    public function getBody() /*: ?array*/
137
-    {
138
-        return $this->body;
139
-    }
140
-
141
-    public function setBody($body) /*: void*/
142
-    {
143
-        $this->body = $body;
144
-    }
145
-
146
-    public function addHeader(string $key, string $value)
147
-    {
148
-        $this->headers[$key] = $value;
149
-    }
150
-
151
-    public function getHeader(string $key): string
152
-    {
153
-        if (isset($this->headers[$key])) {
154
-            return $this->headers[$key];
155
-        }
156
-        if ($this->highPerformance) {
157
-            $serverKey = 'HTTP_' . strtoupper(str_replace('-', '_', $key));
158
-            if (isset($_SERVER[$serverKey])) {
159
-                return $_SERVER[$serverKey];
160
-            }
161
-        }
162
-        return '';
163
-    }
164
-
165
-    public function getHeaders(): array
166
-    {
167
-        return $this->headers;
168
-    }
169
-
170
-    public static function fromString(string $request): Request
171
-    {
172
-        $parts = explode("\n\n", trim($request), 2);
173
-        $head = $parts[0];
174
-        $body = isset($parts[1]) ? $parts[1] : null;
175
-        $lines = explode("\n", $head);
176
-        $line = explode(' ', trim(array_shift($lines)), 2);
177
-        $method = $line[0];
178
-        $url = isset($line[1]) ? $line[1] : '';
179
-        $path = parse_url($url, PHP_URL_PATH);
180
-        $query = parse_url($url, PHP_URL_QUERY);
181
-        $headers = array();
182
-        foreach ($lines as $line) {
183
-            list($key, $value) = explode(':', $line, 2);
184
-            $headers[$key] = trim($value);
185
-        }
186
-        return new Request($method, $path, $query, $headers, $body);
187
-    }
188
-}

+ 3
- 0
src/Tqdev/PhpCrudApi/RequestFactory.php View File

@@ -34,6 +34,9 @@ class RequestFactory
34 34
         $psr17Factory = new Psr17Factory();
35 35
         $creator = new ServerRequestCreator($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
36 36
         $serverRequest = $creator->fromGlobals();
37
+        if (isset($_SERVER['PATH_INFO'])) {
38
+            $serverRequest = $serverRequest->withUri($psr17Factory->createUri($_SERVER['PATH_INFO'] . '?' . $_SERVER['QUERY_STRING']));
39
+        }
37 40
         $body = file_get_contents('php://input');
38 41
         if ($body) {
39 42
             $serverRequest = $serverRequest->withParsedBody(self::parseBody($body));

Loading…
Cancel
Save