Skip to content

Commit 8094668

Browse files
committed
Merge pull request #4 from dbu/discovery
restructure discovery documentation - Fix #1, Fix #2
2 parents 83fd630 + 5e5f584 commit 8094668

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

docs/discovery.md

+58-59
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,10 @@ The discovery service is a set of static classes which allows to find and use in
66
Currently available discovery services:
77

88
- HTTP Adapter Discovery
9-
- PSR-7 Message Discovery
10-
- PSR-7 URI Discovery
9+
- PSR-7 Message Factory Discovery
10+
- PSR-7 URI Factory Discovery
1111

12-
13-
## General
14-
15-
Discoveries in general are really similar. In fact, the code behind them is exactly the same.
16-
17-
Here is an example dummy discovery:
18-
19-
``` php
20-
use Http\Discovery\ClassDiscovery;
21-
22-
class MyDiscovery extends ClassDiscovery
23-
{
24-
// IMPORTANT: not declared in the parent to avoid overwritting
25-
protected static $cache;
26-
27-
// IMPORTANT: not declared in the parent
28-
protected static $classes = [];
29-
}
30-
```
31-
32-
A start value can be defined for the `classes` property in the following structure:
33-
34-
``` php
35-
[
36-
[
37-
'class' => 'MyClass',
38-
'condition' => 'MyCondition',
39-
],
40-
]
41-
```
42-
43-
- `class`: The class that is instantiated. There MUST NOT be any constructor arguments.
44-
- `condition`: The condition that is evaluated to boolean to decide whether the resource is available. The following types are allowed:
45-
- string: Checked for class existence
46-
- callable: Called and evaluated to boolean
47-
- boolean: Evaluated as is
48-
- any other: evaluated to false
49-
50-
By declaring a start value for `classes` only string conditions are allowed, however the `register` method allows any type of arguments:
51-
52-
``` php
53-
MyDiscovery::register('MyClass', true);
54-
```
55-
56-
Classes registered manually are put on top of the list.
57-
58-
The condition can also be omitted. In that case the class is used as the condition (to check for existence).
59-
60-
The last thing to do is to find the first available resource:
61-
62-
```php
63-
$myClass = MyDiscovery::find();
64-
```
65-
66-
If no classes can be found, an `Http\Discovery\NotFoundException` is thrown.
12+
The principle is always the same: you call a static find method if no explicit implementation was specified. Find will try to locate the service that is enabled. If no service is enabled, an `Http\Discovery\NotFoundException` is thrown.
6713

6814

6915
## HTTP Adapter Discovery
@@ -94,7 +40,7 @@ class MyClass
9440
```
9541

9642

97-
## Message Factory Discovery
43+
## PSR-7 Message Factory Discovery
9844

9945
This type of discovery finds installed [PSR-7](http://www.php-fig.org/psr/psr-7/) Message implementations and their factories.
10046

@@ -125,7 +71,7 @@ class MyClass
12571
}
12672
```
12773

128-
## URI Factory Discovery
74+
## PSR-7 URI Factory Discovery
12975

13076
This type of discovery finds installed [PSR-7](http://www.php-fig.org/psr/psr-7/) URI implementations and their factories.
13177

@@ -155,3 +101,56 @@ class MyClass
155101
}
156102
}
157103
```
104+
105+
106+
## Integrating your own implementation with the discovery mechanism
107+
108+
The `php-http/discovery` has built-in support for some implementations. To use a different implementation or override the default when several implementations are available in your codebase, you can register a class explicitly with the corresponding discovery service. For example:
109+
110+
``` php
111+
HttpAdapterDiscovery::register('Acme\MyAdapter', true);
112+
```
113+
114+
- `class`: The class that is instantiated. This class MUST NOT require any constructor arguments.
115+
- `condition`: The condition that is evaluated to boolean to decide whether the class can be instantiated. The following types are allowed:
116+
- string: Checked for class existence
117+
- callable: Called and evaluated to boolean
118+
- boolean: Can be true or false
119+
- any other: considered false
120+
121+
The condition can also be omitted. In that case the class is used as the condition (to check for existence).
122+
123+
Classes registered manually are put on top of the list.
124+
125+
126+
### Writing your own discovery
127+
128+
Each discovery service is based on the `ClassDiscovery` and has to specify a `cache` field and a `class` field to specify classes for the corresponding service. The fields need to be redeclared in each discovery class. If `ClassDiscovery` would declare them, they would be shared between the discovery classes which would make no sense.
129+
130+
Here is an example discovery:
131+
132+
``` php
133+
use Http\Discovery\ClassDiscovery;
134+
135+
class MyDiscovery extends ClassDiscovery
136+
{
137+
// IMPORTANT: not declared in the parent to avoid overwritting
138+
protected static $cache;
139+
140+
// IMPORTANT: not declared in the parent
141+
protected static $classes = [];
142+
}
143+
```
144+
145+
A start value can be defined for the `classes` property in the following structure:
146+
147+
``` php
148+
[
149+
[
150+
'class' => 'MyClass',
151+
'condition' => 'MyCondition',
152+
],
153+
]
154+
```
155+
156+
The condition is as described above for `register`.

docs/index.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
**This is the documentation for HTTP Adapter and it's software components.**
44

5+
The HTTP Adapter abstracts from PHP HTTP clients that are based on [PSR-7](http://www.php-fig.org/psr/psr-7/).
6+
It allows you to write reusable libraries and applications that need a HTTP client without binding to a specific implementation.
57

68
## History
79

@@ -71,10 +73,10 @@ Read more about it in the [Discovery](discovery.md) part.
7173

7274
#### Installation in an end user package
7375

74-
When installing in an application or a non-reusable package, using a virtual package doesn't really make sense. However there are a few things which should be taken into consideration before choosing an adapter:
76+
When installing in an application or a non-reusable package, requiring the virtual package doesn't really make sense. However there are a few things which should be taken into consideration before choosing an adapter:
7577

76-
- It is possible that some other package already has an HTTP Client requirement. It doesn't worth to install more than one HTTP Clients, so always check your other requirements and choose an adapter based on that.
77-
- Some adapters support paralell requests, some only emulate them. If paralell requests are needed, use one which supports it.
78+
- It is possible that some other package already has an HTTP Client requirement. It can be confusing to have more than one HTTP Client installed, so always check your other requirements and choose an adapter based on that.
79+
- Some adapters support parallel requests, some only emulate them. If parallel requests are needed, use one which supports it.
7880

7981
Installing an implementation is easy:
8082

0 commit comments

Comments
 (0)