Skip to content

Commit dc3345a

Browse files
committed
Removing the uri factory
1 parent 8d07031 commit dc3345a

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

spec/BaseUriPluginSpec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
class BaseUriPluginSpec extends ObjectBehavior
1818
{
19-
function let(UriFactory $uriFactory)
19+
function let(UriInterface $uri)
2020
{
21-
$this->beConstructedWith($uriFactory, 'http://httplug.io/');
21+
$this->beConstructedWith($uri);
2222
}
2323

2424
function it_is_initializable()

src/BaseUriPlugin.php

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,21 @@
55
use Http\Message\UriFactory;
66
use Http\Promise\Promise;
77
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\UriInterface;
89

910
class BaseUriPlugin implements Plugin
1011
{
1112
/**
12-
* @var string
13+
* @var UriInterface
1314
*/
1415
private $baseUri;
1516

1617
/**
17-
* @var UriFactory
18-
*/
19-
private $factory;
20-
21-
/**
22-
* @param UriFactory $factory
23-
* @param string $baseUri
18+
*
19+
* @param UriInterface $baseUri
2420
*/
25-
public function __construct(UriFactory $factory, $baseUri)
21+
public function __construct(UriInterface $baseUri)
2622
{
27-
$this->factory = $factory;
2823
$this->baseUri = $baseUri;
2924
}
3025

@@ -39,21 +34,39 @@ public function __construct(UriFactory $factory, $baseUri)
3934
*/
4035
public function handleRequest(RequestInterface $request, callable $next, callable $first)
4136
{
42-
$uri = $request->getUri();
43-
$uriString = $uri->__toString();
37+
$uriString = $request->getUri()->__toString();
4438
if (!strpos($uriString, '://')) {
45-
if (substr($uriString, 0, 1) !== '/') {
46-
// Append baseUri this url
47-
$request = $request->withUri($this->factory->createUri($this->baseUri.$uriString));
39+
$request = $request->withUri($this->modifyUri($request->getUri()));
40+
}
41+
42+
return $next($request);
43+
}
44+
45+
/**
46+
* @param UriInterface $uri
47+
*
48+
* @return UriInterface
49+
*/
50+
private function modifyUri(UriInterface $uri)
51+
{
52+
$uriString = $uri->__toString();
53+
if (substr($uriString, 0, 1) === '/') {
54+
// Replace/Add path and query to the baseUri
55+
$modifiedUri = $this->baseUri->withPath($uri->getPath());
56+
$modifiedUri = $modifiedUri->withQuery($uri->getQuery());
57+
} else {
58+
// Append baseUri this url
59+
if ("" !== $query = $this->baseUri->getQuery()) {
60+
// Append the uri on the query
61+
$modifiedUri = $this->baseUri->withQuery($query.$uriString);
4862
} else {
49-
// Replace add/replace path and query to the baseUri
50-
$modifiedUri = $this->factory->createUri($this->baseUri);
51-
$modifiedUri = $modifiedUri->withPath($uri->getPath());
52-
$modifiedUri = $modifiedUri->withQuery($uri->getQuery());
53-
$request = $request->withUri($modifiedUri);
63+
// Append the uri on path and query
64+
list($path, $query) = explode('?', $uriString, 2);
65+
$modifiedUri = $this->baseUri->withPath($this->baseUri->getPath().$path);
66+
$modifiedUri = $modifiedUri->withQuery($query);
5467
}
5568
}
5669

57-
return $next($request);
70+
return $modifiedUri;
5871
}
5972
}

0 commit comments

Comments
 (0)