You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/discovery.md
+58-59
Original file line number
Diff line number
Diff line change
@@ -6,64 +6,10 @@ The discovery service is a set of static classes which allows to find and use in
6
6
Currently available discovery services:
7
7
8
8
- 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
11
11
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.
67
13
68
14
69
15
## HTTP Adapter Discovery
@@ -94,7 +40,7 @@ class MyClass
94
40
```
95
41
96
42
97
-
## Message Factory Discovery
43
+
## PSR-7 Message Factory Discovery
98
44
99
45
This type of discovery finds installed [PSR-7](http://www.php-fig.org/psr/psr-7/) Message implementations and their factories.
100
46
@@ -125,7 +71,7 @@ class MyClass
125
71
}
126
72
```
127
73
128
-
## URI Factory Discovery
74
+
## PSR-7 URI Factory Discovery
129
75
130
76
This type of discovery finds installed [PSR-7](http://www.php-fig.org/psr/psr-7/) URI implementations and their factories.
131
77
@@ -155,3 +101,56 @@ class MyClass
155
101
}
156
102
}
157
103
```
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:
-`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`.
Copy file name to clipboardExpand all lines: docs/index.md
+5-3
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
**This is the documentation for HTTP Adapter and it's software components.**
4
4
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.
5
7
6
8
## History
7
9
@@ -71,10 +73,10 @@ Read more about it in the [Discovery](discovery.md) part.
71
73
72
74
#### Installation in an end user package
73
75
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:
75
77
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.
0 commit comments