11Discovery
22=========
33
4- The discovery service is a set of static classes which allows to find and use installed resources.
5- This is useful in libraries that want to offer zero-configuration services and rely only on the virtual packages.
4+ The discovery service allows to find and use installed resources.
5+ Under the hood it uses `Puli `_ for the discovery logic. All of our packages provide Puli resources.
6+ Discovery is simply a convenience wrapper to statically access clients and factories for when
7+ Dependency Injection is not an option. Discovery is useful in libraries that want to offer
8+ zero-configuration services relying on the virtual packages. If you have Dependency Injection available,
9+ using Puli directly is more elegant (see for example the Symfony HttplugBundle).
610
711Currently available discovery services:
812
913- HTTP Client Discovery
14+ - HTTP Async Client Discovery
1015- PSR-7 Message Factory Discovery
1116- PSR-7 URI Factory Discovery
1217- PSR-7 Stream Factory Discovery
@@ -15,14 +20,34 @@ The principle is always the same: you call the static ``find`` method on the dis
1520implementation was specified. The discovery service will try to locate a suitable implementation.
1621If no implementation is found, an ``Http\Discovery\NotFoundException `` is thrown.
1722
18-
1923Installation
2024------------
2125
26+ There are two kinds of installation:
27+
28+ - In a reusable library
29+ - In an application
30+
31+ In both cases you have to install the discovery package itself:
32+
2233.. code-block :: bash
2334
2435 $ composer require php-http/discovery
2536
37+ As mentioned above, discovery relies on Puli. In order to use discovery, you need to also set up Puli.
38+ The easiest way is installing the composer-plugin which automatically configures all the composer packages to act as
39+ Puli modules:
40+
41+ .. code-block :: bash
42+
43+ $ composer require puli/composer-plugin
44+
45+ This is only necessary in an application. However, you might also want to install the composer-plugin as a development
46+ dependency in reusable libraries as well (for example to use discovery in tests).
47+
48+ Read more about setting up Puli in their `official documentation `_.
49+
50+
2651HTTP Client Discovery
2752---------------------
2853
@@ -123,53 +148,19 @@ This type of discovery finds a URI factory for a PSR-7_ URI implementation::
123148Integrating your own implementation with the discovery mechanism
124149----------------------------------------------------------------
125150
126- The ``php-http/discovery `` has built-in support for some implementations.
127- To use a different implementation or override the default when several implementations are available in your code base,
128- you can register a class explicitly with the corresponding discovery service. For example::
129-
130- HttpClientDiscovery::register('Acme\MyClient', true);
151+ When you are working on an HTTP Client or Message Factory, you can easily make your class discoverable:
152+ you have to configure it as a Puli resource (`binding `_ in Puli terminology).
131153
132- - ``class ``: The class that is instantiated. This class MUST NOT require any constructor arguments.
133- - ``condition ``: The condition that is evaluated to boolean to decide whether the class can be instantiated.
134-
135- The following types are allowed:
136- - string: Checked for class existence
137- - callable: Called and evaluated to boolean
138- - boolean: Can be true or false
139- - any other: considered false
140-
141- The condition can also be omitted. In that case the class is used as the condition (to check for existence).
142-
143- Classes registered manually are put on top of the list.
144-
145- Writing your own discovery
146- ^^^^^^^^^^^^^^^^^^^^^^^^^^
147-
148- Each discovery service is based on the ``ClassDiscovery `` and has to specify a ``cache `` property and a ``class `` property
149- to specify classes for the corresponding service. Since they are static, this properties need to be re-declared
150- in each discovery class. If ``ClassDiscovery `` would declare them,
151- they would be shared between the discovery classes which would make no sense.
152-
153- Here is an example discovery::
154-
155- use Http\Discovery\ClassDiscovery;
156-
157- class MyDiscovery extends ClassDiscovery
158- {
159- // IMPORTANT: not declared in the parent to avoid overwritting
160- protected static $cache;
161-
162- // IMPORTANT: not declared in the parent
163- protected static $classes = [];
164- }
154+ A binding must have a type, called `binding-type `_. All of our interfaces are registered as binding types.
165155
166- A start value can be defined for the `` classes `` property in the following structure::
156+ For example: a client `` Http\Client\MyClient `` should be bind to `` Http\Client\HttpClient ``
167157
168- [
169- [
170- 'class' => 'MyClass',
171- 'condition' => 'MyCondition',
172- ],
173- ]
158+ Puli uses a ``puli.json `` file for configuration (placed in the package root).
159+ Use the CLI tool for configuring bindings. It is necessary, because each binding must have a unique identifier.
160+ Read more in Puli's documentation (`Providing Resources `_).
174161
175- The condition is as described above for ``register ``.
162+ .. _`Puli` : http://puli.io
163+ .. _official documentation : http://docs.puli.io/en/latest
164+ .. _`binding` : http://docs.puli.io/en/latest/glossary.html#glossary-binding
165+ .. _`binding-type` : http://docs.puli.io/en/latest/glossary.html#glossary-binding-type
166+ .. _Providing Resources : http://docs.puli.io/en/latest/discovery/providing-resources.html
0 commit comments