You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+112-1
Original file line number
Diff line number
Diff line change
@@ -10,4 +10,115 @@ interface that describes a HTTP message. See the specification for more details.
10
10
Usage
11
11
-----
12
12
13
-
We'll certainly need some stuff in here.
13
+
There are two ways of using the [PSR-7](http://www.php-fig.org/psr/psr-7) standard. You can write your own implementation or consume a library/package that implements the standard.
14
+
15
+
*[Writing your own implementation](https://packagist.org/providers/psr/http-message-implementation)
16
+
*[Consuming the interfaces through a vendor](#consuming-through-vendor)
17
+
18
+
<aname="consuming-through-vendor"></a>
19
+
## Consuming the interfaces through a vendor
20
+
Imagine you need a way to send HTTP requests from your application. Instead of reinventing the wheel, you would then pull in a vendor that has done the legwork for you (perhaps through composer) and use that.
21
+
22
+
As an example, the [Guzzle](https://github.com/guzzle/guzzle) library already supports [PSR-7](http://www.php-fig.org/psr/psr-7). It is then possible to send a request and get back a response.
23
+
To decouple your code from guzzle, you can create your own client interface
24
+
and have that type hinted in your services. Your services know to send a RequestInterface to the client and that they get back a ResponseInterface object.
25
+
26
+
You have an application with an api that allows you to fetch posts. You create a RequestInterface implementation object (like Guzzle's Request object) and send it to the client. The client then responds with an implementation of a ResponseInterface object (like Guzzle's Response object).
27
+
28
+
```php
29
+
<?php
30
+
namespace YourApp\Controller;
31
+
32
+
use Psr7\Http\Message\RequestInterface;
33
+
use Psr7\Http\Message\ResponseInterface;
34
+
use GuzzleHttp\Psr7\Client;
35
+
use GuzzleHttp\Psr7\Request;
36
+
37
+
/**
38
+
* Creating a client interface that your services can depend on so you can
39
+
* swap out clients whenever you feel like it, without changing the underlying
40
+
* code
41
+
*/
42
+
interface ClientInterface
43
+
{
44
+
/**
45
+
* This method sends a request and returns a response
46
+
*
47
+
* @param RequestInterface $request
48
+
*
49
+
* @return ResponseInterface
50
+
*/
51
+
public function send(RequestInterface $request);
52
+
53
+
/**
54
+
* @param string $method
55
+
* @param string $url
56
+
*
57
+
* @return RequestInterface
58
+
*/
59
+
public function request($method, $url);
60
+
}
61
+
62
+
/**
63
+
* This is a guzzle client, implementing our interface
64
+
*/
65
+
class GuzzleClient implements ClientInterface
66
+
{
67
+
public function send(RequestInterface $request)
68
+
{
69
+
return $this->guzzle->send($request);
70
+
}
71
+
72
+
public function request($method, $url)
73
+
{
74
+
return new Request($method, $url);
75
+
}
76
+
}
77
+
78
+
/**
79
+
* Or another client, that you swapped guzzle out with
80
+
* as a simple example.
81
+
*/
82
+
class OtherHttpClient implements ClientInterface
83
+
{
84
+
public function send(RequestInterface $request)
85
+
{
86
+
$this->otherClient->send($request);
87
+
}
88
+
89
+
public function request($method, $url)
90
+
{
91
+
return new OtherHttpVendorRequest($method, $url);
92
+
}
93
+
}
94
+
95
+
/**
96
+
* Your posts service then receives a client object, on which he can create
97
+
* requests (RequestInterface) and receive responses (ResponseInterface)
98
+
*/
99
+
class PostsService
100
+
{
101
+
/**
102
+
* Inject the client, but keep the service agnostic as to which client it is using.
103
+
*
104
+
* @param ClientInterface $client
105
+
*/
106
+
public function __construct(ClientInterface $client)
107
+
{
108
+
$this->client = $client;
109
+
}
110
+
111
+
/**
112
+
* @return array
113
+
*/
114
+
public function all()
115
+
{
116
+
// $request is now an instance of RequestInterface
0 commit comments