Skip to content

Commit 0f2b962

Browse files
Merge pull request #161 from php-http/factories-doc-improvements
improve factories documentation
2 parents 8abb042 + 3eca342 commit 0f2b962

File tree

6 files changed

+123
-41
lines changed

6 files changed

+123
-41
lines changed

clients/includes/further-reading.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
* Use :doc:`plugins </plugins/introduction>` to customize the way HTTP requests are sent and
22
responses processed by following redirects, adding Authentication or Cookie
33
headers and more.
4-
* Learn how you can decouple your code from any PSR-7 implementation by using a
5-
:ref:`message factory <message-factory>`.
4+
* Learn how you can decouple your code from any PSR-7 implementation by using
5+
the :ref:`HTTP factories <message-factory>`.

clients/includes/install-message-factory.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ to install one as well (for example `Guzzle PSR-7`_):
66
$ composer require guzzlehttp/psr7
77

88
In order to provide full interoperability, message implementations are
9-
accessed through :doc:`factories </message/message-factory>`. Message factories for
9+
accessed through :ref:`factories <message-factory>`. Message factories for
1010
`Diactoros`_, `Guzzle PSR-7`_ and `Slim Framework`_ are available in the
1111
:doc:`message </message>` component:
1212

httplug/library-developers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Messages
6363
--------
6464

6565
When you construct HTTP message objects in your library, you should not depend on a concrete PSR-7 message
66-
implementation. Instead, use the :ref:`PHP-HTTP message factory <message-factory>`.
66+
implementation. Instead, use the :doc:`HTTP factories <../message/message-factory>`.
6767

6868
Discovery
6969
---------

message.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@ This package contains various PSR-7 tools which might be useful in an HTTP workf
1212
* Various Stream encoding tools
1313
* Message decorators
1414
* Message factory implementations for Guzzle PSR-7, Diactoros and Slim Framework.
15+
* Stream and URI factory implementations for Guzzle PSR-7 and Diactoros
1516
* Cookie implementation
16-
17-
.. _message-factory:
18-

message/message-factory.rst

Lines changed: 117 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,140 @@
11
.. _message-factory:
2+
.. _stream-factory:
23

3-
Message Factory
4-
===============
4+
HTTP Factories
5+
==============
56

6-
**Factory interfaces for PSR-7 HTTP Message.**
7+
**Factory interfaces for PSR-7 HTTP objects.**
78

89
Rationale
910
---------
1011

11-
While it should be possible to use every PSR-7 aware HTTP client with any RequestInterface implementation,
12-
creating the request objects will still tie the code to a specific implementation.
13-
If each reusable library is tied to a specific message implementation,
14-
an application could end up installing several message implementations.
15-
The factories abstract away from this.
12+
While it should be possible to use every PSR-7 aware HTTP client with any
13+
request, URI and stream implementation, instantiating objects explicitly would
14+
still tie the code to a specific implementation. If each reusable library is
15+
tied to a specific message implementation, an application could end up
16+
installing several message implementations. The factories move instantiation
17+
out of the library code, further decoupling libraries from implementation.
1618

1719
The FIG was pretty straightforward by NOT putting any construction logic into PSR-7.
1820
The ``MessageFactory`` aims to provide an easy way to construct messages.
1921

20-
Usage
21-
-----
22-
23-
.. _stream-factory:
22+
Factories
23+
---------
2424

2525
The `php-http/message-factory` package defines interfaces for PSR-7 factories including:
2626

27-
- ``MessageFactory``
28-
- ``ServerRequestFactory`` - WIP (PRs welcome)
27+
- ``RequestFactory``
28+
- ``ResponseFactory``
29+
- ``MessageFactory`` (combination of request and response factories)
2930
- ``StreamFactory``
30-
- ``UploadedFileFactory`` - WIP (PRs welcome)
3131
- ``UriFactory``
3232

33-
Implementation for the interfaces above for `Diactoros`_ and `Guzzle PSR-7`_ and `Slim Framework`_ can be found in ``php-http/message``.
34-
35-
.. code:: php
33+
Implementations of the interfaces above for `Diactoros`_, `Guzzle PSR-7`_ and the `Slim Framework`_ can be found in ``php-http/message``.
3634

37-
// Create a PSR-7 request
38-
$factory = new Http\Message\MessageFactory\DiactorosMessageFactory();
39-
$request = $factory->createRequest('GET', 'http://example.com');
40-
41-
// Create a PSR-7 stream
42-
$factory = new Http\Message\StreamFactory\DiactorosStreamFactory();
43-
$stream = $factory->createStream('stream content');
44-
45-
You could also use :doc:`/discovery` to find an installed factory automatically.
46-
47-
.. code:: php
48-
49-
// Create a PSR-7 request
50-
$factory = MessageFactoryDiscovery::find();
51-
$request = $factory->createRequest('GET', 'http://example.com');
35+
Usage
36+
-----
5237

38+
Instantiate the factories in your bootstrap code or use discovery for them.
39+
Inject the factories into the rest of your code to limit the implementation
40+
choice to the bootstrapping code::
41+
42+
// ApiClient.php
43+
44+
use Http\Message\RequestFactory;
45+
use Http\Message\StreamFactory;
46+
use Http\Message\UriFactory;
47+
48+
class ApiClient
49+
{
50+
/**
51+
* @var RequestFactory
52+
*/
53+
private $requestFactory;
54+
55+
/**
56+
* @var StreamFactory
57+
*/
58+
private $streamFactory;
59+
60+
/**
61+
* @var UriFactory
62+
*/
63+
private $uriFactory;
64+
65+
public function __construct(
66+
RequestFactory $requestFactory,
67+
StreamFactory $streamFactory,
68+
UriFactory $uriFactory
69+
) {
70+
$this->requestFactory = $requestFactory;
71+
$this->streamFactory = $streamFactory;
72+
$this->uriFactory = $uriFactory;
73+
}
74+
75+
public function doStuff()
76+
{
77+
$request = $this->requestFactory->createRequest('GET', 'http://httplug.io');
78+
$stream = $this->streamFactory->createStream('stream content');
79+
$uri = $this->UriFactory->createUri('http://httplug.io');
80+
...
81+
}
82+
}
83+
84+
The bootstrapping code could look like this::
85+
86+
// bootstrap.php
87+
use Http\Message\MessageFactory\DiactorosMessageFactory;
88+
use Http\Message\StreamFactory\DiactorosStreamFactory;
89+
use Http\Message\UriFactory\DiactorosUriFactory;
90+
91+
$apiClient = new ApiClient(
92+
new DiactorosMessageFactory(),
93+
new DiactorosStreamFactory(),
94+
new DiactorosUriFactory()
95+
);
96+
97+
You could also use :doc:`/discovery` to make the factory arguments optional and
98+
automatically find an available factory in the client::
99+
100+
// ApiClient.php
101+
102+
use Http\Discovery\MessageFactoryDiscovery;
103+
use Http\Discovery\StreamFactoryDiscovery;
104+
use Http\Discovery\UriFactoryDiscovery;
105+
use Http\Message\RequestFactory;
106+
use Http\Message\StreamFactory;
107+
use Http\Message\UriFactory;
108+
109+
class ApiClient
110+
{
111+
public function __construct(
112+
RequestFactory $requestFactory = null,
113+
StreamFactory $streamFactory = null,
114+
UriFactory $uriFactory = null
115+
) {
116+
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(),
117+
$this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find();
118+
$this->uriFactory = $uriFactory ?: UriFactoryDiscovery::find();;
119+
}
120+
121+
...
122+
}
123+
124+
.. hint::
125+
126+
If you create requests only and no responses, use ``RequestFactory`` in the
127+
type hint, instead of the ``MessageFactory``. And vice versa if you create
128+
responses only.
129+
130+
Server Side Factories
131+
---------------------
132+
133+
It would make sense to also provide factories for the server side constructs
134+
``ServerRequestInterface`` and ``UploadedFileInterface``. We did not get around
135+
to do that yet. Contributions are welcome if you want to define the
136+
``ServerRequestFactory`` and ``UploadedFileFactory``.
53137

54138
.. _Diactoros: https://github.com/zendframework/zend-diactoros
55139
.. _Guzzle PSR-7: https://github.com/guzzle/psr7
56140
.. _Slim Framework: https://github.com/slimphp/Slim
57-

spelling_word_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ sublicense
3030
sync
3131
toolbar
3232
username
33+
versa
3334
whitelist
3435
wiki
3536
Wikipedia

0 commit comments

Comments
 (0)