8000 Fixes #16 Symfony Request created without any URI · symfony/symfony@a59c572 · GitHub
[go: up one dir, main page]

Skip to content

Commit a59c572

Browse files
rouginfabpot
authored andcommitted
Fixes #16 Symfony Request created without any URI
1 parent d16c63c commit a59c572

File tree

3 files changed

+170
-1
lines changed

3 files changed

+170
-1
lines changed

Factory/HttpFoundationFactory.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Http\Message\ServerRequestInterface;
1515
use Psr\Http\Message\ResponseInterface;
1616
use Psr\Http\Message\UploadedFileInterface;
17+
use Psr\Http\Message\UriInterface;
1718
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
1819
use Symfony\Component\HttpFoundation\Cookie;
1920
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -32,6 +33,18 @@ class HttpFoundationFactory implements HttpFoundationFactoryInterface
3233
*/
3334
public function createRequest(ServerRequestInterface $psrRequest)
3435
{
36+
$server = array();
37+
$uri = $psrRequest->getUri();
38+
39+
if ($uri instanceof UriInterface) {
40+
$server['SERVER_NAME'] = $uri->getHost();
41+
$server['SERVER_PORT'] = $uri->getPort();
42+
$server['REQUEST_URI'] = $uri->getPath();
43+
$server['QUERY_STRING'] = $uri->getQuery();
44+
}
45+
46+
$server = array_replace($server, $psrRequest->getServerParams());
47+
3548
$parsedBody = $psrRequest->getParsedBody();
3649
$parsedBody = is_array($parsedBody) ? $parsedBody : array();
3750

@@ -41,7 +54,7 @@ public function createRequest(ServerRequestInterface $psrRequest)
4154
$psrRequest->getAttributes(),
4255
$psrRequest->getCookieParams(),
4356
$this->getFiles($psrRequest->getUploadedFiles()),
44-
$psrRequest->getServerParams(),
57+
$server,
4558
$psrRequest->getBody()->__toString()
4659
);
4760
$request->headers->replace($psrRequest->getHeaders());

Tests/Factory/HttpFoundationFactoryTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\ServerRequest;
1818
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Stream;
1919
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\UploadedFile;
20+
use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Uri;
2021

2122
/**
2223
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -120,6 +121,26 @@ public function testCreateRequestWithObjectParsedBody()
120121
$this->assertCount(0, $this->factory->createRequest($serverRequest)->request);
121122
}
122123

124+
public function testCreateRequestWithUri()
125+
{
126+
$serverRequest = new ServerRequest(
127+
'1.1',
128+
array(),
129+
new Stream(),
130+
'/',
131+
'GET',
132+
new Uri('http://les-tilleuls.coop/about/kevin'),
133+
array(),
134+
array(),
135+
array(),
136+
array(),
137+
null,
138+
array()
139+
);
140+
141+
$this->assertEquals('/about/kevin', $this->factory->createRequest($serverRequest)->getPathInfo());
142+
}
143+
123144
public function testCreateUploadedFile()
124145
{
125146
$uploadedFile = $this->createUploadedFile('An uploaded file.', UPLOAD_ERR_OK, 'myfile.txt', 'text/plain');

Tests/Fixtures/Uri.php

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PsrHttpMessage\Tests\Fixtures;
13+
14+
use Psr\Http\Message\UriInterface;
15+
16+
/**
17+
* @author Rougin Royce Gutib <rougingutib@gmail.com>
18+
*/
19+
class Uri implements UriInterface
20+
{
21+
private $scheme = '';
22+
private $userInfo = '';
23+
private $host = '';
24+
private $port;
25+
private $path = '';
26+
private $query = '';
27+
private $fragment = '';
28+
private $uriString;
29+
30+
public function __construct($uri = '')
31+
{
32+
$parts = parse_url($uri);
33+
34+
$this->scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
35+
$this->userInfo = isset($parts['user']) ? $parts['user'] : '';
36+
$this->host = isset($parts['host']) ? $parts['host'] : '';
37+
$this->port = isset($parts['port']) ? $parts['port'] : null;
38+
$this->path = isset($parts['path']) ? $parts['path'] : '';
39+
$this->query = isset($parts['query']) ? $parts['query'] : '';
40+
$this->fragment = isset($parts['fragment']) ? $parts['fragment'] : '';
41+
$this->uriString = $uri;
42+
}
43+
44+
public function getScheme()
45+
{
46+
return $this->scheme;
47+
}
48+
49+
public function getAuthority()
50+
{
51+
if (empty($this->host)) {
52+
return '';
53+
}
54+
55+
$authority = $this->host;
56+
57+
if (!empty($this->userInfo)) {
58+
$authority = $this->userInfo.'@'.$authority;
59+
}
60+
61+
$authority .= ':'.$this->port;
62+
63+
return $authority;
64+
}
65+
66+
public function getUserInfo()
67+
{
68+
return $this->userInfo;
69+
}
70+
71+
public function getHost()
72+
{
73+
return $this->host;
74+
}
75+
76+
public function getPort()
77+
{
78+
return $this->port;
79+
}
80+
81+
public function getPath()
82+
{
83+
return $this->path;
84+
}
85+
86+
public function getQuery()
87+
{
88+
return $this->query;
89+
}
90+
91+
public function getFragment()
92+
{
93+
return $this->fragment;
94+
}
95+
96+
public function withScheme($scheme)
97+
{
98+
throw new \BadMethodCallException('Not implemented.');
99+
}
100+
101+
public function withUserInfo($user, $password = null)
102+
{
103+
throw new \BadMethodCallException('Not implemented.');
104+
}
105+
106+
public function withHost($host)
107+
{
108+
throw new \BadMethodCallException('Not implemented.');
109+
}
110+
111+
public function withPort($port)
112+
{
113+
throw new \BadMethodCallException('Not implemented.');
114+
}
115+
116+
public function withPath($path)
117+
{
118+
throw new \BadMethodCallException('Not implemented.');
119+
}
120+
121+
public function withQuery($query)
122+
{
123+
throw new \BadMethodCallException('Not implemented.');
124+
}
125+
126+
public function withFragment($fragment)
127+
{
128+
throw new \BadMethodCallException('Not implemented.');
129+
}
130+
131+
public function __toString()
132+
{
133+
return $this->uriString;
134+
}
135+
}

0 commit comments

Comments
 (0)
0