Skip to content

Commit f0ace0e

Browse files
committed
reorganizing the documentation
1 parent c2e2b1c commit f0ace0e

10 files changed

+202
-144
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# HTTP Adapter Documentation
1+
# PHP-HTTP Documentation
22

33
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
44

55

6-
**This is the documentation repository of the HTTP Adapter software components**
6+
**This is the documentation repository of the PHP-HTTP software components**
77

88
Browse the documentation on [Read the Docs](http://php-http.readthedocs.org/).

Diff for: docs/discovery.md

+14-30
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,46 @@
11
# Discovery
22

3-
The discovery service is a set of static classes which allows to find and use installed resources. This is useful in libraries that want to offer zero-configuration services and rely only on the virtual packages, e.g. `php-http/adapter-implementation` or `psr/http-message-implementation`.
3+
The discovery service is a set of static classes which allows to find and use installed resources. This is useful in libraries that want to offer zero-configuration services and rely only on the virtual packages, e.g. `php-http/client-implementation` or `psr/http-message-implementation`.
44

55

66
Currently available discovery services:
77

8-
- HTTP Adapter Discovery
8+
- HTTP client Discovery
99
- PSR-7 Message Factory Discovery
1010
- PSR-7 URI Factory Discovery
1111

1212
The principle is always the same: you call the static `find` method on the discovery service if no explicit implementation was specified. The discovery service will try to locate a suitable implementation. If no implementation is found, an `Http\Discovery\NotFoundException` is thrown.
1313

1414

15-
## HTTP Adapter Discovery
15+
## HTTP Client Discovery
1616

17-
This type of discovery finds installed HTTP Adapters.
18-
19-
Currently available adapters:
20-
21-
- [Guzzle 6](https://github.com/php-http/guzzle6-adapter)
22-
- [Guzzle 5](https://github.com/php-http/guzzle5-adapter)
17+
This type of discovery finds installed HTTP Clients.
2318

2419
``` php
25-
use Http\Adapter\HttpAdapter;
26-
use Http\Discovery\HttpAdapterDiscovery;
20+
use Http\Client\HttpClient;
21+
use Http\Discovery\HttpClientDiscovery;
2722

2823
class MyClass
2924
{
3025
/**
31-
* @var HttpAdapter
26+
* @var HttpClient
3227
*/
33-
protected $httpAdapter;
28+
protected $httpClient;
3429

3530
/**
36-
* @param HttpAdapter|null $httpAdapter to do HTTP requests.
31+
* @param HttpClient|null $httpClient to do HTTP requests.
3732
*/
38-
public function __construct(HttpAdapter $httpAdapter = null)
33+
public function __construct(HttpClient $httpClient = null)
3934
{
40-
$this->httpAdapter = $httpAdapter ?: HttpAdapterDiscovery::find();
35+
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
4136
}
4237
}
4338
```
4439

4540

4641
## PSR-7 Message Factory Discovery
4742

48-
This type of discovery finds installed [PSR-7](http://www.php-fig.org/psr/psr-7/) Message implementations and their factories.
49-
50-
Currently available factories:
51-
52-
- [Guzzle](https://github.com/guzzle/psr7) factory
53-
- [Diactoros](https://github.com/zendframework/zend-diactoros) factory
54-
43+
This type of discovery finds installed [PSR-7](http://www.php-fig.org/psr/psr-7/) Message implementations and their (factories)[message-factory.md].
5544

5645
``` php
5746
use Http\Message\MessageFactory;
@@ -74,16 +63,11 @@ class MyClass
7463
}
7564
```
7665

66+
7767
## PSR-7 URI Factory Discovery
7868

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

81-
Currently available factories:
82-
83-
- [Guzzle](https://github.com/guzzle/psr7) factory
84-
- [Diactoros](https://github.com/zendframework/zend-diactoros) factory
85-
86-
8771
``` php
8872
use Http\Message\UriFactory;
8973
use Http\Discovery\UriFactoryDiscovery;
@@ -111,7 +95,7 @@ class MyClass
11195
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:
11296

11397
``` php
114-
HttpAdapterDiscovery::register('Acme\MyAdapter', true);
98+
HttpClientDiscovery::register('Acme\MyClient', true);
11599
```
116100

117101
- `class`: The class that is instantiated. This class MUST NOT require any constructor arguments.

Diff for: docs/httplug.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Httplug HTTP client abstraction
2+
3+
Httplug is an abstraction for HTTP clients. There are two main use cases:
4+
5+
1. Usage in a project
6+
2. Usage in a reusable package
7+
8+
In both cases, the client provides a `sendRequest` method to send a PSR-7 `RequestInterface` and returns a PSR-7 `ResponseInterface` or throws an exception that implements `Http\Client\Exception`. The client also has a `sendRequests` to send requests in parallel. Clients that do not support sending requests in parallel will just send them sequentially.
9+
10+
See the (tutorial)[tutorial.md] for a concrete example.
11+
12+
## Httplug implementations
13+
14+
Httplug implementations typically are either HTTP clients of their own, or they are adapters wrapping existing clients like Guzzle 6. In the later case, they will depend on the required client implementation, you should only need to require the adapter and not the actual client.
15+
16+
Clients usually also provide a (message factory)[message-factory.md] to allow you to create requests. See https://packagist.org/providers/php-http/client-implementation for the full list of implementations.
17+
18+
Note: Until Httplug 1.0 becomes stable, we will focus on the Guzzle6 adapter.
19+
20+
## Usage in a project
21+
22+
When installing in an application or a non-reusable package, you should require a concrete client implementation. The client will in turn depend on `php-http/httplug`.
23+
24+
A few things should be taken into consideration before choosing an adapter:
25+
26+
- It is possible that some other dependency already has an HTTP Client requirement like Guzzle 6. 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.
27+
- Some clients support parallel requests, others only emulate them. If parallel requests are needed, use one which supports it.
28+
29+
30+
## Installation in a reusable package
31+
32+
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.
33+
34+
In these cases, they should **not rely on a concrete implementation** (like Guzzle 6), but only require any implementation of Httplug. Httplug uses the concept of virtual packages. Instead of depending on only the interfaces, which would be missing an implementation, or depending on one concrete implementation, you should depend on the virtual package `php-http/client-implementation`. There is no package with that name, but all clients and adapters implementing Httplug declare that they provide this virtual package.
35+
36+
With Composer, it is possible:
37+
38+
``` bash
39+
composer require "php-http/client-implementation" "^1.0"
40+
```
41+
42+
This allows the end user to choose the concrete implementation when they are install the package. During development and for running tests, a concrete implementation is needed. Use the `require-dev` section of composer:
43+
44+
45+
``` bash
46+
composer require --dev "php-http/guzzle6-adapter" "^1.0"
47+
```
48+
49+
If injecting the client instance into the code of the reusable package is not convenient, you can use the (Discovery)[discovery.md] system.
50+
51+
Users of your package will have to select a concrete adapter in their project to make your package installable.

Diff for: docs/index.md

+15-68
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,34 @@
11
# HTTP Adapter
22

3-
**This is the documentation for HTTP Adapter and it's software components.**
3+
**This is the documentation for the Httplug web client abstraction and the other PHP-HTTP 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/).
5+
Httplug abstracts from HTTP clients written in PHP that are based on [PSR-7](http://www.php-fig.org/psr/psr-7/).
66
It allows you to write reusable libraries and applications that need a HTTP client without binding to a specific implementation.
77

8-
## History
9-
10-
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. Because of the constantly changing PSR-7 Eric had to rewrite the library over and over again (at least the message handling part, which in most cases affected every adapters).
11-
12-
in 2015 a decision has been made to move the library to it's own organization, so PHP HTTP was born.
8+
If you do not have access to a dependency injection system, the [Discovery](discovery.md) system can be used to automatically locate a suitable client and a PSR-7 message factory.
139

1410

1511
## Getting started
1612

17-
HTTP Adapter is separated into several components:
18-
19-
- Adapter contract
20-
- Client contract
21-
- Adapter client
22-
- Adapter implementations
23-
- Helpers
24-
25-
26-
### Installation
27-
28-
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/):
29-
30-
``` bash
31-
$ composer require php-http/adapter
32-
```
33-
13+
Read our [tutorial](tutorial.md).
3414

35-
#### Installation in a reusable package
3615

37-
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.
16+
## Overview
3817

39-
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:
18+
PHP-HTTP is separated into several packages:
4019

41-
``` json
42-
{
43-
"require": {
44-
"php-http/adapter-implementation": "^1.0"
45-
}
46-
}
47-
```
20+
- [Httplug](httplug.md), the HTTP client abstraction to send PSR-7 requests without binding to a specific implementation;
21+
- [Message Factory](message-factory.md) to create PSR-7 requests without binding to a specific implementation;
22+
- [Discovery](discovery.md) to automatically locate a suitable Httplug implementation and PSR-7 message factory.
23+
- [Utilities](utils.md) for convenient sending of HTTP requests.
4824

49-
This allows the end user to choose a concrete implementation when installs the package. Of course, during development a concrete implementation is needed:
25+
See (package overview)[package-overview.md] for a complete list.
5026

5127

52-
``` json
53-
{
54-
"require-dev": {
55-
"php-http/guzzle6-adapter": "^1.0"
56-
}
57-
}
58-
```
59-
60-
61-
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:
62-
63-
``` json
64-
{
65-
"require": {
66-
"php-http/discovery": "^1.0"
67-
}
68-
}
69-
```
70-
71-
Read more about it in the [Discovery](discovery.md) part.
72-
73-
74-
#### Installation in an end user package
75-
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:
77-
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.
28+
## History
8029

81-
Installing an implementation is easy:
30+
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 a stable release, but it relied on PSR-7 which was not stable either that time. Because of the constantly changing PSR-7, Eric had to rewrite the library over and over again (at least the message handling part, which in most cases affected every adapter as well).
8231

83-
``` bash
84-
$ composer require php-http/*-adapter
85-
```
32+
In 2015, a decision has been made to move the library to it's own organization, so PHP HTTP was born.
8633

87-
_Replace * with any supported adapter name_
34+
See (upgrading)[upgrading.md] for a guide how to migrate your code from the Ivory adapter to Httplug.

Diff for: docs/message-factory.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This package provides interfaces for PSR-7 factories including:
2222

2323
A virtual package ([php-http/message-factory-implementation](https://packagist.org/providers/php-http/message-factory-implementation)) MAY be introduced which MUST be versioned together with this package.
2424

25+
The adapter repositories provide wrapper classes for those factories to implement the `Http\Message\MessageFactory` interface.
2526

2627
### General usage
2728

Diff for: docs/package-overview.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Overview of PHP-HTTP packages
2+
3+
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/).
4+
5+
## httplug
6+
7+
**Interfaces for HTTP Client**
8+
9+
- Namespace: `Http\Client`
10+
- Repository: https://github.com/php-http/httplug
11+
- Documentation: (Httplug)[httplug.md]
12+
13+
## message-factory
14+
15+
**Abstraction to create PSR-7 requests without binding to a specific implementation**
16+
17+
- Namespace: `Http\Message`
18+
- Repository: https://github.com/php-http/message-factory
19+
- Documentation: [Message Factory](message-factory.md)
20+
21+
## *-adapter
22+
23+
**Each adapter is separated into its own package. This allows to make requirements to the underlying HTTP Client libraries.**
24+
25+
- Namespace: `Http\Adapter`
26+
- Repository: https://github.com/php-http/*-adapter
27+
28+
For a full list, see https://github.com/php-http/
29+
30+
## discovery
31+
32+
**Discovery service to find installed resources (httplug implementation, message factory)**
33+
34+
- Namespace: `Http\Discovery`
35+
- Repository: https://github.com/php-http/discovery
36+
- Documentation: (Discovery)[discovery.md]
37+
38+
## utils
39+
40+
**Utility classes for HTTP consumers**
41+
42+
- Namespace: `Http\Utils`
43+
- Repository: https://github.com/php-http/utils
44+
- Documentation: (Utils)[utils.md]

Diff for: docs/tutorial.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Httplug tutorial
2+
3+
This tutorial should give you an idea how to use Httplug in your project. Httplug has two main use cases:
4+
5+
1. Usage in your project;
6+
2. Usage in a reusable package.
7+
8+
This tutorial will start with the first use case and then explain the special considerations to take into account when building a reusable package.
9+
10+
We use (composer)[https://getcomposer.org] for dependency management. Install it if you don't have it yet.
11+
12+
## Setting up the project
13+
14+
``` bash
15+
mkdir httplug-tutorial
16+
cd httplug-tutorial
17+
composer init
18+
# specify your information as you want. say no to defining the dependencies interactively
19+
composer require php-http/guzzle6-adapter
20+
```
21+
22+
The last command will install Guzzle as well as the Guzzle Httplug adapter and the required interface repositories. We are now ready to start coding.
23+
24+
## Writing some simple code
25+
26+
Create a file `demo.php` in the root folder and write the following code:
27+
28+
``` php
29+
<?php
30+
require('vendor/autoload.php');
31+
32+
TODO: create client instance with discovery and do some requests
33+
```
34+
35+
## Handling errors
36+
37+
TODO: explain how to handle exceptions, distinction between network exception and HttpException.
38+
39+
## Doing parallel requests
40+
41+
TODO explain sendRequests and how to work with BatchResult and BatchException
42+
43+
44+
## Writing a reusable package
45+
46+
TODO: explain the virtual package

0 commit comments

Comments
 (0)