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
+116-1
Original file line number
Diff line number
Diff line change
@@ -10,4 +10,119 @@ 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](#implementation)
16
+
*[Consuming the interfaces through a vendor](#consuming-through-vendor)
17
+
18
+
<aname="implementation"></a>
19
+
## Writing your own implementation
20
+
Implementations should implement all functionality in each interface. Please check [here](https://packagist.org/providers/psr/http-message-implementation) for vendors who have already done this.
21
+
22
+
<aname="consuming-through-vendor"></a>
23
+
## Consuming the interfaces through a vendor
24
+
Imagine you need a way to send HTTP requests from your application. Instead of reinventing the wheel, you would then pull in a [vendor](https://packagist.org/providers/psr/http-message-implementation) that has done the legwork for you (perhaps through composer) and use that.
25
+
26
+
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.
27
+
To decouple your code from guzzle, you can create your own client interface
28
+
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.
29
+
30
+
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).
31
+
32
+
```php
33
+
<?php
34
+
namespace YourApp\Controller;
35
+
36
+
use Psr7\Http\Message\RequestInterface;
37
+
use Psr7\Http\Message\ResponseInterface;
38
+
use GuzzleHttp\Psr7\Client;
39
+
use GuzzleHttp\Psr7\Request;
40
+
41
+
/**
42
+
* Creating a client interface that your services can depend on so you can
43
+
* swap out clients whenever you feel like it, without changing the underlying
44
+
* code
45
+
*/
46
+
interface ClientInterface
47
+
{
48
+
/**
49
+
* This method sends a request and returns a response
50
+
*
51
+
* @param RequestInterface $request
52
+
*
53
+
* @return ResponseInterface
54
+
*/
55
+
public function send(RequestInterface $request);
56
+
57
+
/**
58
+
* @param string $method
59
+
* @param string $url
60
+
*
61
+
* @return RequestInterface
62
+
*/
63
+
public function request($method, $url);
64
+
}
65
+
66
+
/**
67
+
* This is a guzzle client, implementing our interface
68
+
*/
69
+
class GuzzleClient implements ClientInterface
70
+
{
71
+
public function send(RequestInterface $request)
72
+
{
73
+
return $this->guzzle->send($request);
74
+
}
75
+
76
+
public function request($method, $url)
77
+
{
78
+
return new Request($method, $url);
79
+
}
80
+
}
81
+
82
+
/**
83
+
* Or another client, that you swapped guzzle out with
84
+
* as a simple example.
85
+
*/
86
+
class OtherHttpClient implements ClientInterface
87
+
{
88
+
public function send(RequestInterface $request)
89
+
{
90
+
$this->otherClient->send($request);
91
+
}
92
+
93
+
public function request($method, $url)
94
+
{
95
+
return new OtherHttpVendorRequest($method, $url);
96
+
}
97
+
}
98
+
99
+
/**
100
+
* Your posts service then receives a client object, on which he can create
101
+
* requests (RequestInterface) and receive responses (ResponseInterface)
102
+
*/
103
+
class PostsService
104
+
{
105
+
/**
106
+
* Inject the client, but keep the service agnostic as to which client it is using.
107
+
*
108
+
* @param ClientInterface $client
109
+
*/
110
+
public function __construct(ClientInterface $client)
111
+
{
112
+
$this->client = $client;
113
+
}
114
+
115
+
/**
116
+
* @return array
117
+
*/
118
+
public function all()
119
+
{
120
+
// $request is now an instance of RequestInterface
0 commit comments