1: <?php
2:
3: namespace PHPFastCGI\FastCGIDaemon\Http;
4:
5: use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest;
6: use Zend\Diactoros\ServerRequest;
7: use Zend\Diactoros\ServerRequestFactory;
8:
9: 10: 11:
12: class Request implements RequestInterface
13: {
14: 15: 16:
17: private $params;
18:
19: 20: 21:
22: private $stdin;
23:
24: 25: 26: 27: 28: 29:
30: public function __construct(array $params, $stdin)
31: {
32: $this->params = [];
33:
34: foreach ($params as $name => $value) {
35: $this->params[strtoupper($name)] = $value;
36: }
37:
38: $this->stdin = $stdin;
39:
40: rewind($this->stdin);
41: }
42:
43: 44: 45:
46: public function getParams()
47: {
48: return $this->params;
49: }
50:
51: 52: 53:
54: public function getQuery()
55: {
56: $query = null;
57:
58: if (isset($this->params['QUERY_STRING'])) {
59: parse_str($this->params['QUERY_STRING'], $query);
60: }
61:
62: return $query ?: [];
63: }
64:
65: 66: 67:
68: public function getPost()
69: {
70: $post = null;
71:
72: if (isset($this->params['REQUEST_METHOD']) && isset($this->params['CONTENT_TYPE'])) {
73: $requestMethod = $this->params['REQUEST_METHOD'];
74: $contentType = $this->params['CONTENT_TYPE'];
75:
76: if (strcasecmp($requestMethod, 'POST') === 0 && strcasecmp($contentType, 'application/x-www-form-urlencoded') === 0) {
77: $postData = stream_get_contents($this->stdin);
78: rewind($this->stdin);
79:
80: parse_str($postData, $post);
81: }
82: }
83:
84: return $post ?: [];
85: }
86:
87: 88: 89:
90: public function getCookies()
91: {
92: $cookies = [];
93:
94: if (isset($this->params['HTTP_COOKIE'])) {
95: $cookiePairs = explode(';', $this->params['HTTP_COOKIE']);
96:
97: foreach ($cookiePairs as $cookiePair) {
98: list($name, $value) = explode('=', trim($cookiePair));
99: $cookies[$name] = $value;
100: }
101: }
102:
103: return $cookies;
104: }
105:
106: 107: 108:
109: public function getStdin()
110: {
111: return $this->stdin;
112: }
113:
114: 115: 116:
117: public function getServerRequest()
118: {
119: $query = $this->getQuery();
120: $post = $this->getPost();
121: $cookies = $this->getCookies();
122:
123: $server = ServerRequestFactory::normalizeServer($this->params);
124: $headers = ServerRequestFactory::marshalHeaders($server);
125: $uri = ServerRequestFactory::marshalUriFromServer($server, $headers);
126: $method = ServerRequestFactory::get('REQUEST_METHOD', $server, 'GET');
127:
128: $request = new ServerRequest($server, [], $uri, $method, $this->stdin, $headers);
129:
130: return $request
131: ->withCookieParams($cookies)
132: ->withQueryParams($query)
133: ->withParsedBody($post);
134: }
135:
136: 137: 138:
139: public function getHttpFoundationRequest()
140: {
141: $query = $this->getQuery();
142: $post = $this->getPost();
143: $cookies = $this->getCookies();
144:
145: return new HttpFoundationRequest($query, $post, [], $cookies, [], $this->params, $this->stdin);
146: }
147: }
148: