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