11Discovery
22=========
33
4- The discovery service allows to find installed resources and install missing ones.
4+ The discovery service allows to find installed resources and auto- install missing ones.
55
66Currently available discovery services:
77
8- - HTTP Client Discovery
9- - HTTP Async Client Discovery
108- PSR-17 Factory Discovery
119- PSR-18 HTTP Client Discovery
10+ - PSR-7 Message Factory Discovery (deprecated in favor of PSR-17)
11+ - PSR-7 URI Factory Discovery (deprecated in favor of PSR-17)
12+ - PSR-7 Stream Factory Discovery (deprecated in favor of PSR-17)
13+ - HTTP Async Client Discovery
14+ - HTTP Client Discovery (deprecated in favor of PSR-18)
1215- Mock Client Discovery (not enabled by default)
13- - PSR-7 Message Factory Discovery (deprecated)
14- - PSR-7 URI Factory Discovery (deprecated)
15- - PSR-7 Stream Factory Discovery (deprecated)
16-
17- The principle is always the same: you call the static ``find `` method on the discovery service if no explicit
18- implementation was specified. The discovery service will try to locate a suitable implementation.
19- If no implementation is found, an ``Http\Discovery\Exception\NotFoundException `` is thrown.
2016
21- .. versionadded :: 0.9
22- The exception ``Http\Discovery\NotFoundException `` has been moved to ``Http\Discovery\Exception\NotFoundException ``.
17+ The principle is always the same: you call the static ``find `` method on the discovery service. The
18+ discovery service will try to locate a suitable implementation. If no implementation is found, an
19+ ``Http\Discovery\Exception\NotFoundException `` is thrown.
2320
2421Discovery is simply a convenience wrapper to statically access clients and factories for when
25- Dependency Injection is not an option. Discovery is useful in libraries that want to offer
26- zero-configuration services relying on the virtual packages.
22+ Dependency Injection is not an option. Discovery is particularly useful in libraries that want to
23+ offer zero-configuration services relying on the virtual packages.
2724
2825Auto-installation
2926-----------------
@@ -35,10 +32,10 @@ Discovery embeds a composer plugin that can auto-install missing implementations
3532when an application does not specify any specific implementation.
3633
3734If a library requires both ``php-http/discovery `` and one of the supported virtual packages
38- (see :doc: `library-developers `), but no implementation for the virtual package is already
35+ (see :doc: `httplug/ library-developers `), but no implementation for the virtual package is already
3936installed, the plugin will auto-install the best matching known implementation.
4037
41- For example, if one is using ``react/event-loop ``, the plugin will select ``php-http/react-adapter ``
38+ For example, if the project requires ``react/event-loop ``, the plugin will select ``php-http/react-adapter ``
4239to meet a missing dependency on ``php-http/client-implementation ``.
4340
4441The following abstractions are currently supported:
@@ -49,18 +46,65 @@ The following abstractions are currently supported:
4946- ``psr/http-factory-implementation ``
5047- ``psr/http-message-implementation ``
5148
49+ .. note ::
50+ Auto-installation is only done for libraries that directly require ``php-http/discovery `` to
51+ avoid unexpected dependency installation.
52+
53+ If you do not want auto-installation to happen, you can chose to not enable the composer
54+ plugin of the discovery component:
55+
56+ ``composer config allow-plugins.php-http/discovery false ``
57+
58+
5259.. _discovery-strategies :
5360
5461Strategies
5562----------
5663
57- The package supports multiple discovery strategies and comes with two out-of-the-box:
64+ The package uses strategies to select an implementation.
65+
66+ The default strategy contains a list of preferences that looks for well-known implementations:
67+ Symfony, Guzzle, Diactoros and Slim Framework.
68+
69+ Once a strategy provided a candidate, the result is cached in memory and reused for further
70+ discovery calls in the same process.
71+
72+ To register a custom strategy, implement the ``Http\Discovery\Strategy\DiscoveryStrategy ``
73+ interface and register your strategy with the ``prependStrategy ``, ``appendStrategy `` or
74+ ``setStrategies `` method of the corresponding discovery type.
75+
76+ Implementation Pinning
77+ ----------------------
5878
59- - A built-in strategy supporting the HTTPlug adapters, clients and factories (including Symfony, Guzzle, Diactoros and Slim Framework)
79+ .. versionadded :: 1.17
80+ Pinning the preferred implementation is available since v1.17.
6081
61- Strategies provide candidates of a type which gets evaluated by the discovery service.
62- When it finds the best candidate, it caches it and stops looking in further strategies.
82+ 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:
6385
86+ .. code-block :: json
87+
88+ "extra" : {
89+ "discovery" : {
90+ "psr/http-factory-implementation" : " GuzzleHttp\\ Psr7\\ HttpFactory"
91+ }
92+ }
93+
94+ You can also pin single interfaces, e.g. for the PSR-17 factories:
95+
96+ .. code-block :: json
97+
98+ "extra" : {
99+ "discovery" : {
100+ "Psr\\ Http\\ Message\\ RequestFactoryInterface" : " Slim\\ Psr7\\ Factory\\ RequestFactory"
101+ }
102+ }
103+
104+ .. note ::
105+ Implementation pinning only works if the composer plugin of discovery is allowed. If you
106+ disabled the plugin, you need to configure your own discovery if you need a specific
107+ implementation selection.
64108
65109Installation
66110------------
@@ -351,7 +395,7 @@ to the Discovery. Let's take a look::
351395 // Test...
352396 }
353397 }
354-
398+
355399In the example of a test class above, we have our ``MyCustomService `` which
356400relies on an HTTP Client implementation. We do not need to test that the actual
357401request our custom service makes is successful in this test class, so it makes
0 commit comments