Skip to content

Commit e73b895

Browse files
committed
Initial commit
0 parents  commit e73b895

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
charset = utf-8
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true
8+
indent_style = space
9+
indent_size = 4
10+
11+
[*.yml*]
12+
indent_style = space
13+
indent_size = 2

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2014-2015 Eric GELOEN <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# HTTP Adapter Documentation
2+
3+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
4+
5+
6+
**This is the documentation repository of the HTTP Adapter software components**

docs/discovery.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
```

docs/index.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# HTTP Adapter
2+
3+
**This is the documentation for HTTP Adapter and it's software components.**
4+
5+
6+
## History
7+
8+
This project has been started by [Eric Geloen](https://github.com/egeloen) as [Ivory Http Adapter](https://github.com/egeloen/ivory-http-adapter). It never made it to be really stable, but it relied on PSR-7 which was not stable either that time.
9+
10+
11+
## Getting started
12+
13+
HTTP Adapter is separated into several components:
14+
15+
- Adapter contract
16+
- Client contract
17+
- Adapter client
18+
- Adapter implementations
19+
- Helpers
20+
21+
22+
### Installation
23+
24+
There are many strategies how adapters and other components can be installed. However they are the same in one thing: they can be installed via [Composer](http://getcomposer.org/):
25+
26+
``` bash
27+
$ composer require php-http/adapter
28+
```
29+
30+
31+
#### Installation in a reusable package
32+
33+
In many cases packages are designed to be reused from the very beginning. For example API clients are usually used in other packages/applications, not on their own.
34+
35+
In these cases it is always a good idea not to rely on a concrete implementation (like Guzzle), but only require some implementation of an HTTP Adapter. With Composer, it is possible:
36+
37+
``` json
38+
{
39+
"require": {
40+
"php-http/adapter-implementation": "^1.0"
41+
}
42+
}
43+
```
44+
45+
This allows the end user to choose a concrete implementation when installs the package. Of course, during development a concrete implementation is needed:
46+
47+
48+
``` json
49+
{
50+
"require-dev": {
51+
"php-http/guzzle6-adapter": "^1.0"
52+
}
53+
}
54+
```
55+
56+
57+
Another good practice if the package works out-of-the-box, no or only minimal configuration is needed. While not requiring a concrete implementation is great, it also means that the end user would have to always inject the installed adapter (which is the right, but not a convenient solution). To solve this, there is a discovery components which finds and resolves other installed components:
58+
59+
``` json
60+
{
61+
"require": {
62+
"php-http/discovery": "^1.0"
63+
}
64+
}
65+
```
66+
67+
Read more about it in the [Discovery](discovery.md) part.
68+
69+
70+
#### Installation in an end user package
71+
72+
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:
73+
74+
- It is possible that some other package already has an HTTP Client requirement. It doesn't worth to install more than one HTTP Clients.
75+
- Some adapters support paralell requests, some only emulate them. If paralell requests are needed, use one which supports it.
76+
77+
Installing an implementation is easy:
78+
79+
``` bash
80+
$ composer require php-http/*-adapter
81+
```
82+
83+
_Replace * with any supported adapter name_

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
site_name: HTTP Adapter Documentation

0 commit comments

Comments
 (0)