@@ -22,6 +22,54 @@ Discovery is simply a convenience wrapper to statically access clients and facto
22
22
Dependency Injection is not an option. Discovery is particularly useful in libraries that want to
23
23
offer zero-configuration services relying on the virtual packages.
24
24
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
+
25
73
Auto-installation
26
74
-----------------
27
75
@@ -80,8 +128,13 @@ Implementation Pinning
80
128
Pinning the preferred implementation is available since v1.17.
81
129
82
130
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\P sr7\H ttpFactory
136
+
137
+ This will update your ``composer.json `` file to add the following configuration:
85
138
86
139
.. code-block :: json
87
140
@@ -101,6 +154,13 @@ You can also pin single interfaces, e.g. for the PSR-17 factories:
101
154
}
102
155
}
103
156
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
+
104
164
.. note ::
105
165
Implementation pinning only works if the composer plugin of discovery is allowed. If you
106
166
disabled the plugin, you need to configure your own discovery if you need a specific
0 commit comments