Skip to content

Commit fc63080

Browse files
authored
Merge pull request #306 from php-http/discovery
documentation from nicolas grekas for discovery
2 parents a8c0978 + ea28aad commit fc63080

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

discovery.rst

+62-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,54 @@ Discovery is simply a convenience wrapper to statically access clients and facto
2222
Dependency Injection is not an option. Discovery is particularly useful in libraries that want to
2323
offer zero-configuration services relying on the virtual packages.
2424

25+
Using discovery in a shared library
26+
-------------------------------------
27+
28+
The goal of the PSR standards is that libraries do not depend on specific
29+
implementations but only on the standard. The library should only require the
30+
PSR standards.
31+
32+
To run tests, you might still need an implementation. We recommend to
33+
explicitly require that, but only for dev. To build a library that needs to
34+
send HTTP requests, you could do:
35+
36+
.. code-block:: bash
37+
$ composer require --dev symfony/http-client
38+
$ composer require --dev nyholm/psr7
39+
40+
Then, you can disable the Composer plugin provided by``php-http/discovery``
41+
because you just installed the dev dependencies you need for testing:
42+
43+
.. code-block:: bash
44+
$ composer config allow-plugins.php-http/discovery false
45+
46+
Finally, you need to require ``php-http/discovery`` and the generic implementations
47+
that your library is going to need:
48+
49+
.. code-block:: bash
50+
$ composer require php-http/discovery:^1.17
51+
$ composer require psr/http-client-implementation:*
52+
$ composer require psr/http-factory-implementation:*
53+
54+
Now, you're ready to make an HTTP request::
55+
56+
use Http\Discovery\Psr18Client;
57+
58+
$client = new Psr18Client();
59+
60+
$request = $client->createRequest('GET', 'https://example.com');
61+
$response = $client->sendRequest($request);
62+
63+
.. versionadded:: 1.17
64+
The ``Psr18Client`` is available since v1.17.
65+
66+
Internally, this code will use whatever PSR-7, PSR-17 and PSR-18 implementations
67+
your users have installed.
68+
69+
It is best practice to allow the users of your library to optionally specify the
70+
``ClientInterface`` instance and only fallback to discovery when no explicit
71+
client has been specified.
72+
2573
Auto-installation
2674
-----------------
2775

@@ -80,8 +128,13 @@ Implementation Pinning
80128
Pinning the preferred implementation is available since v1.17.
81129

82130
In case there are several implementations available, the application can pin which implementation
83-
to prefer. You can specify the implementation for one of the standards in the ``extra.discovery``
84-
section of the application ``composer.json`` file:
131+
to prefer. You can specify the implementation for one of the standards:
132+
133+
134+
.. code-block:: bash
135+
$ composer config extra.discovery.psr/http-factory-implementation GuzzleHttp\Psr7\HttpFactory
136+
137+
This will update your ``composer.json`` file to add the following configuration:
85138

86139
.. code-block:: json
87140
@@ -101,6 +154,13 @@ You can also pin single interfaces, e.g. for the PSR-17 factories:
101154
}
102155
}
103156
157+
Don't forget to run composer install to apply the changes, and ensure that
158+
the composer plugin is enabled:
159+
160+
.. code-block:: bash
161+
$ composer config allow-plugins.php-http/discovery true
162+
$ composer install
163+
104164
.. note::
105165
Implementation pinning only works if the composer plugin of discovery is allowed. If you
106166
disabled the plugin, you need to configure your own discovery if you need a specific

0 commit comments

Comments
 (0)