1
1
Discovery
2
2
=========
3
3
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.
5
5
6
6
Currently available discovery services:
7
7
8
- - HTTP Client Discovery
9
- - HTTP Async Client Discovery
10
8
- PSR-17 Factory Discovery
11
9
- 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)
12
15
- 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.
20
16
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.
23
20
24
21
Discovery 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.
27
24
28
25
Auto-installation
29
26
-----------------
@@ -35,10 +32,10 @@ Discovery embeds a composer plugin that can auto-install missing implementations
35
32
when an application does not specify any specific implementation.
36
33
37
34
If 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
39
36
installed, the plugin will auto-install the best matching known implementation.
40
37
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 ``
42
39
to meet a missing dependency on ``php-http/client-implementation ``.
43
40
44
41
The following abstractions are currently supported:
@@ -49,18 +46,65 @@ The following abstractions are currently supported:
49
46
- ``psr/http-factory-implementation ``
50
47
- ``psr/http-message-implementation ``
51
48
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
+
52
59
.. _discovery-strategies :
53
60
54
61
Strategies
55
62
----------
56
63
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
+ ----------------------
58
78
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.
60
81
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:
63
85
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.
64
108
65
109
Installation
66
110
------------
@@ -351,7 +395,7 @@ to the Discovery. Let's take a look::
351
395
// Test...
352
396
}
353
397
}
354
-
398
+
355
399
In the example of a test class above, we have our ``MyCustomService `` which
356
400
relies on an HTTP Client implementation. We do not need to test that the actual
357
401
request our custom service makes is successful in this test class, so it makes
0 commit comments