|
| 1 | +# Discovery |
| 2 | + |
| 3 | +The discovery service is a set of static classes which allows to find and use installed resources. This is especially useful when used with some virtual packages providing an implementation (`php-http/adapter-implementation`, `psr/http-message-implementation`). |
| 4 | + |
| 5 | + |
| 6 | +Currently available discovery services: |
| 7 | + |
| 8 | +- HTTP Adapter Discovery |
| 9 | +- PSR Message Discovery |
| 10 | +- PSR URI Discovery |
| 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 | + 'name' => [ |
| 37 | + 'class' => 'MyClass', |
| 38 | + 'condition' => 'MyCondition', |
| 39 | + ], |
| 40 | +] |
| 41 | +``` |
| 42 | + |
| 43 | +- `name`: A unique name for the resource, can be overwritten using `register` |
| 44 | +- `class`: The class that is instantiated. There MUST NOT be any constructor arguments. |
| 45 | +- `condition`: The condition that is evaluated to boolean to decide whether the resource is available. The following types are allowed: |
| 46 | + - string: Checked for class existence |
| 47 | + - callable: Called and evaluated to boolean |
| 48 | + - boolean: Evaluated as is |
| 49 | + - any other: evaluated to false |
| 50 | + |
| 51 | +By declaring a start value for `classes` only string conditions are allowed, however the `register` method allows any type of arguments: |
| 52 | + |
| 53 | +``` php |
| 54 | +MyDiscovery::register('name', 'MyClass', true); |
| 55 | +``` |
| 56 | + |
| 57 | +The condition can also be omitted. In that case the class is used as the condition (to check for existence). |
| 58 | + |
| 59 | +The last thing to do is to find the first available resource: |
| 60 | + |
| 61 | +```php |
| 62 | +$myClass = MyDiscovery::find(); |
| 63 | +``` |
| 64 | + |
| 65 | +If no classes can be found, an `Http\Discovery\NotFoundException` is thrown. |
| 66 | + |
| 67 | + |
| 68 | +## HTTP Adapter Discovery |
| 69 | + |
| 70 | +This type of discovery finds installed HTTP Adapters. |
| 71 | + |
| 72 | +It is useful to provide zero-configuration for classes relying on an adapter: |
| 73 | + |
| 74 | +``` php |
| 75 | +use Http\Adapter\HttpAdapter; |
| 76 | +use Http\Discovery\HttpAdapterDiscovery; |
| 77 | + |
| 78 | +class MyClass |
| 79 | +{ |
| 80 | + /** |
| 81 | + * @var HttpAdapter |
| 82 | + */ |
| 83 | + protected $httpAdapter; |
| 84 | + |
| 85 | + /** |
| 86 | + * @param HttpAdapter $httpAdapter |
| 87 | + */ |
| 88 | + public function __construct(HttpAdapter $httpAdapter) |
| 89 | + { |
| 90 | + $this->httpAdapter = $httpAdapter ?: HttpAdapterDiscovery::find(); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
0 commit comments