Completed
Pull Request — master (#39)
by Tobias
03:31
created

Request::getServerRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace PHPFastCGI\FastCGIDaemon\Http;
4
5
use Nyholm\Psr7Server\ServerRequestCreatorInterface;
6
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest;
7
8
/**
9
 * The default implementation of the RequestInterface.
10
 */
11
class Request implements RequestInterface
12
{
13
    /**
14
     * @var ServerRequestCreatorInterface|null
15
     */
16
    private static $serverRequestCreator = null;
17
18
    /**
19
     * @var array
20
     */
21
    private $params;
22
23
    /**
24
     * @var resource
25
     */
26
    private $stdin;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param array    $params The FastCGI server params as an associative array
32
     * @param resource $stdin  The FastCGI stdin data as a stream resource
33
     */
34
    public function __construct(array $params, $stdin)
35
    {
36
        $this->params = [];
37
38
        foreach ($params as $name => $value) {
39
            $this->params[strtoupper($name)] = $value;
40
        }
41
42
        $this->stdin  = $stdin;
43
44
        rewind($this->stdin);
45
    }
46
47
    public static function setServerRequestCreator(ServerRequestCreatorInterface $serverRequestCreator): void
48
    {
49
        self::$serverRequestCreator = $serverRequestCreator;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getParams()
56
    {
57
        return $this->params;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getQuery()
64
    {
65
        $query = null;
66
67
        if (isset($this->params['QUERY_STRING'])) {
68
            parse_str($this->params['QUERY_STRING'], $query);
69
        }
70
71
        return $query ?: [];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getPost()
78
    {
79
        $post = null;
80
81
        if (isset($this->params['REQUEST_METHOD']) && isset($this->params['CONTENT_TYPE'])) {
82
            $requestMethod = $this->params['REQUEST_METHOD'];
83
            $contentType   = $this->params['CONTENT_TYPE'];
84
85
            if (strcasecmp($requestMethod, 'POST') === 0 && stripos($contentType, 'application/x-www-form-urlencoded') === 0) {
86
                $postData = stream_get_contents($this->stdin);
87
                rewind($this->stdin);
88
89
                parse_str($postData, $post);
90
            }
91
        }
92
93
        return $post ?: [];
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getCookies()
100
    {
101
        $cookies = [];
102
103
        if (isset($this->params['HTTP_COOKIE'])) {
104
            $cookiePairs = explode(';', $this->params['HTTP_COOKIE']);
105
106
            foreach ($cookiePairs as $cookiePair) {
107
                list($name, $value) = explode('=', trim($cookiePair));
108
                $cookies[$name] = $value;
109
            }
110
        }
111
112
        return $cookies;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getStdin()
119
    {
120
        return $this->stdin;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getServerRequest()
127
    {
128
        if (null === self::$serverRequestCreator) {
129
            throw new \RuntimeException('You need to add an object of \Nyholm\Psr7Server\ServerRequestCreatorInterface to \PHPFastCGI\FastCGIDaemon\Http\Request::setServerRequestCreator to use PSR-7 requests. Please install and read more at https://github.com/nyholm/psr7-server');
130
        }
131
132
        $server  = $this->params;
133
        $query   = $this->getQuery();
134
        $post    = $this->getPost();
135
        $cookies = $this->getCookies();
136
137
        return self::$serverRequestCreator->fromArrays(
138
            $server,
139
            self::$serverRequestCreator->getHeadersFromServer($server),
140
            $cookies,
141
            $query,
142
            $post,
143
            [] /*$this->uploadedFiles*/,
144
            $this->stdin
145
        );
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getHttpFoundationRequest()
152
    {
153
        if (!class_exists(HttpFoundationRequest::class)) {
154
            throw new \RuntimeException('You need to install symfony/http-foundation:^4.0 to use HttpFoundation requests.');
155
        }
156
157
        $query   = $this->getQuery();
158
        $post    = $this->getPost();
159
        $cookies = $this->getCookies();
160
161
        return new HttpFoundationRequest($query, $post, [], $cookies, [], $this->params, $this->stdin);
162
    }
163
}
164