Skip to content

Commit 2018063

Browse files
Nyholmdbu
authored andcommitted
Updated docs for the bundle, added missing factories
1 parent 7c2dbb9 commit 2018063

File tree

4 files changed

+192
-91
lines changed

4 files changed

+192
-91
lines changed

assets/img/debug-toolbar.png

6.99 KB
Loading

integrations/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ HTTPlug provides the following framework integrations:
66
.. toctree::
77

88
symfony-bundle
9+
symfony-full-configuration

integrations/symfony-bundle.rst

+115-91
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,64 @@
11
Symfony Bundle
22
==============
33

4-
The usage documentation is split into two parts. First we explain how to configure the bundle in an application. The second part is for developing reusable Symfony bundles that depend on an HTTP client defined by the Httplug interface.
5-
6-
For information how to write applications with the services provided by this bundle, have a look at the [Httplug documentation](http://docs.php-http.org).
7-
8-
9-
Use in Applications
10-
-------------------
11-
12-
Custom services
13-
```````````````
14-
15-
+----------------------------------+---------------------------------------------------------------------+
16-
| Service id | Description |
17-
+==================================+=====================================================================+
18-
| httplug.message_factory | Service* that provides the `Http\Message\MessageFactory` |
19-
+----------------------------------+---------------------------------------------------------------------+
20-
| httplug.uri_factory | Service* that provides the `Http\Message\UriFactory` |
21-
+----------------------------------+---------------------------------------------------------------------+
22-
| httplug.stream_factory | Service* that provides the `Http\Message\StreamFactory` |
23-
+----------------------------------+---------------------------------------------------------------------+
24-
| httplug.client.[name] | | This is your Httpclient that you have configured. |
25-
| | | With the configuration below the name would be `acme_client`. |
26-
+----------------------------------+---------------------------------------------------------------------+
27-
| httplug.client | This is the first client configured or a client named `default`. |
28-
+----------------------------------+---------------------------------------------------------------------+
29-
| | httplug.plugin.content_length | | These are plugins that are enabled by default. |
30-
| | httplug.plugin.decoder | | These services are not public and may only be used when configure |
31-
| | httplug.plugin.error | | HttpClients or other services. |
32-
| | httplug.plugin.logger | |
33-
| | httplug.plugin.redirect | |
34-
| | httplug.plugin.retry | |
35-
| | httplug.plugin.stopwatch | |
36-
+----------------------------------+---------------------------------------------------------------------+
37-
| | httplug.plugin.cache | | These are plugins that are disabled by default. |
38-
| | httplug.plugin.cookie | | They need to be configured before they can be used. |
39-
| | httplug.plugin.history | | These services are not public and may only be used when configure |
40-
| | | HttpClients or other services. |
41-
+----------------------------------+---------------------------------------------------------------------+
42-
43-
\* *These services are always an alias to another service. You can specify your own service or leave the default, which is the same name with `.default` appended. The default services in turn use the service discovery mechanism to provide the best available implementation. You can specify a class for each of the default services to use instead of discovery, as long as those classes can be instantiated without arguments.*
44-
45-
46-
If you need a more custom setup, define the services in your application configuration and specify your service in the `main_alias` section. For example, to add authentication headers, you could define a service that decorates the service `httplug.client.default` with a plugin that injects the authentication headers into the request and configure `httplug.main_alias.client` to the name of your service.
4+
This bundle integrate HTTPlug with the Symfony framework. The bundle helps to register services for all your clients and makes sure all the configuration is in one place. The bundle also feature a toolbar plugin with information about your requests.
5+
6+
7+
Installation
8+
````````````
9+
10+
Install the Httplug bundle with composer and enable it in your AppKernel.php.
11+
12+
.. code-block:: bash
13+
14+
$ composer require php-http/httplug-bundle
15+
16+
.. code-block:: php
17+
18+
public function registerBundles()
19+
{
20+
$bundles = array(
21+
// ...
22+
new Http\HttplugBundle\HttplugBundle(),
23+
);
24+
}
25+
26+
You will find all available configuration at the :doc:`full configuration </integrations/symfony-full-configuration>` page.
27+
28+
Usage
29+
`````
4730

4831
.. code-block:: yaml
4932
5033
httplug:
34+
plugins:
35+
logger: ~
5136
clients:
52-
acme_client: # This is the name of the client
53-
factory: 'httplug.factory.guzzle6'
54-
55-
main_alias:
56-
client: httplug.client.default
57-
message_factory: httplug.message_factory.default
58-
uri_factory: httplug.uri_factory.default
59-
stream_factory: httplug.stream_factory.default
60-
classes:
61-
# uses discovery if not specified
62-
client: ~
63-
message_factory: ~
64-
uri_factory: ~
65-
stream_factory: ~
37+
acme:
38+
factory: 'httplug.factory.guzzle6'
39+
plugins: ['httplug.plugin.logger']
40+
config:
41+
verify: false
42+
timeout: 2
43+
44+
.. code-block:: php
6645
46+
$request = $this->container->get('httplug.message_factory')->createRequest('GET', 'http://example.com');
47+
$response = $this->container->get('httplug.client.acme')->sendRequest($request);
6748
68-
Configuration without auto discovery
69-
````````````````````````````````````
7049
71-
By default we use Puli to auto discover factories. If you do not want to use auto discovery you could use the following configuration (Guzzle):
50+
Web Debug Toolbar
51+
`````````````````
52+
.. image:: /assets/img/debug-toolbar.png
53+
:align: right
54+
:width: 120px
55+
56+
When using a client configured with HttplugBundle, you will get debug information in the web debug toolbar. It will tell you how many request were made and how many of those that were successful or not. It will also show you detailed information about each request.
57+
58+
Discovery of Factory Classes
59+
````````````````````````````
60+
61+
If you want the bundle to automatically find usable factory classes, install and enable ``puli/symfony-bundle``. If you do not want use auto discovery, you need to specify all the factory classes for you client. The following example show how you configure factory classes using Guzzle:
7262

7363
.. code-block:: yaml
7464
@@ -80,10 +70,11 @@ By default we use Puli to auto discover factories. If you do not want to use aut
8070
stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory
8171
8272
83-
Configure your client
84-
`````````````````````
8573
86-
You can configure your clients with some good default options. The clients are later registered as services.
74+
Configure Clients
75+
`````````````````
76+
77+
You can configure your clients with default options. These default values will be specific to you client you are using. The clients are later registered as services.
8778

8879
.. code-block:: yaml
8980
@@ -100,77 +91,110 @@ You can configure your clients with some good default options. The clients are l
10091
headers:
10192
Content-Type: 'application/json'
10293
acme:
103-
factory: 'httplug.factory.guzzle6'
94+
factory: 'httplug.factory.curl'
10495
config:
105-
base_uri: 'http://google.se/'
96+
78: 4 #CURLOPT_CONNECTTIMEOUT
10697
10798
.. code-block:: php
10899
109100
$httpClient = $this->container->get('httplug.client.my_guzzle5');
110-
$httpClient = $this->container->get('httplug.client.acme');
101+
$httpClient = $this->container->get('httplug.client.curl');
102+
103+
// will be the same as ``httplug.client.my_guzzle5``
104+
$httpClient = $this->container->get('httplug.client');
111105
106+
The bundle has client factory services that you can use to build your client. If you need a very custom made client you could create your own factory service implementing ``Http\HttplugBudle\ClientFactory\ClientFactory``. The build in services are:
107+
108+
* ``httplug.factory.curl``
109+
* ``httplug.factory.guzzle5``
110+
* ``httplug.factory.guzzle6``
111+
* ``httplug.factory.react``
112+
* ``httplug.factory.socket``
112113

113114
Plugins
114115
```````
115116

116-
You can configure the clients with plugins.
117+
You can configure the clients with plugins. You can choose to use a built in plugin in the ``php-http/plugins`` package or provide a plugin of your own. The order of the specified plugin does matter.
117118

118119
.. code-block:: yaml
119120
120121
// services.yml
121122
acme_plugin:
122-
class: Acme\Plugin\MyCustonPlugin
123-
arguments: ["%api_key%"]
123+
class: Acme\Plugin\MyCustomPlugin
124+
arguments: ["%some_parameter%"]
124125
125126
.. code-block:: yaml
126127
127128
// config.yml
128-
httpug:
129+
httplug:
129130
plugins:
130131
cache:
131132
cache_pool: 'my_cache_pool'
132133
clients:
133134
acme:
134135
factory: 'httplug.factory.guzzle6'
135-
plugins: ['acme_plugin', 'httplug.plugin.cache', ''httplug.plugin.retry']
136-
config:
137-
base_uri: 'http://google.se/'
136+
plugins: ['acme_plugin', 'httplug.plugin.cache', 'httplug.plugin.retry']
138137
139138
140139
Authentication
141140
``````````````
142141

142+
You can configure a client with authentication. Valid authentication types are ``basic``, ``bearer``, ``service`` and ``wsse``. See more examples at the :doc:`full configuration </integrations/symfony-full-configuration>`.
143+
143144
.. code-block:: yaml
144145
145146
// config.yml
146-
httpug:
147+
httplug:
147148
plugins:
148149
authentication:
149-
my_basic:
150-
type: 'basic'
151-
username: 'my_username'
152-
password: 'p4ssw0rd'
153150
my_wsse:
154151
type: 'wsse'
155152
username: 'my_username'
156153
password: 'p4ssw0rd'
157-
my_brearer:
158-
type: 'bearer'
159-
token: 'authentication_token_hash'
160-
my_service:
161-
type: 'service'
162-
service: 'my_authentication_service'
163154
164155
clients:
165156
acme:
166157
factory: 'httplug.factory.guzzle6'
167158
plugins: ['httplug.plugin.authentication.my_wsse']
168159
169160
170-
171-
Use for Reusable Bundles
172-
------------------------
173-
174-
Rather than code against specific HTTP clients, you want to use the Httplug `Client` interface. To avoid building your own infrastructure to define services for the client, simply `require: php-http/httplug-bundle` in your bundles `composer.json`. You SHOULD provide configuration for each of your services that needs an HTTP client to specify the service to use, defaulting to `httplug.client`. This way, the default case needs no additional configuration for your users.
175-
176-
The only steps they need is `require` one of the adapter implementations in their projects `composer.json` and instantiating the HttplugBundle in their kernel.
161+
List of Services
162+
````````````````
163+
164+
+----------------------------------+-------------------------------------------------------------------------+
165+
| Service id | Description |
166+
+==================================+=========================================================================+
167+
| httplug.message_factory | Service* that provides the `Http\Message\MessageFactory` |
168+
+----------------------------------+-------------------------------------------------------------------------+
169+
| httplug.uri_factory | Service* that provides the `Http\Message\UriFactory` |
170+
+----------------------------------+-------------------------------------------------------------------------+
171+
| httplug.stream_factory | Service* that provides the `Http\Message\StreamFactory` |
172+
+----------------------------------+-------------------------------------------------------------------------+
173+
| httplug.client.[name] | There is one service per named client. |
174+
+----------------------------------+-------------------------------------------------------------------------+
175+
| httplug.client | | If there is a client named "default", this service is an alias to |
176+
| | | that client, otherwise it is an alias to the first client configured. |
177+
+----------------------------------+-------------------------------------------------------------------------+
178+
| | httplug.plugin.content_length | | These are plugins that are enabled by default. |
179+
| | httplug.plugin.decoder | | These services are private and should only be used to configure |
180+
| | httplug.plugin.error | | clients or other services. |
181+
| | httplug.plugin.logger | |
182+
| | httplug.plugin.redirect | |
183+
| | httplug.plugin.retry | |
184+
| | httplug.plugin.stopwatch | |
185+
+----------------------------------+-------------------------------------------------------------------------+
186+
| | httplug.plugin.cache | | These are plugins that are disabled by default and only get |
187+
| | httplug.plugin.cookie | | activated when configured. |
188+
| | httplug.plugin.history | | These services are private and should only be used to configure |
189+
| | | clients or other services. |
190+
+----------------------------------+-------------------------------------------------------------------------+
191+
192+
\* *These services are always an alias to another service. You can specify your own service or leave the default, which is the same name with `.default` appended.*
193+
194+
195+
Useage for Reusable Bundles
196+
``````````````````````````
197+
198+
Rather than code against specific HTTP clients, you want to use the Httplug ``Client`` interface. To avoid building your own infrastructure to define services for the client, simply ``require: php-http/httplug-bundle`` in your bundles ``composer.json``. You SHOULD provide a configuration option to specify the which HTTP client service to use for each of your services. This option should default to ``httplug.client``. This way, the default case needs no additional configuration for your users, but they have the option of using specific clients with each of your services.
199+
200+
The only steps they need is ``require`` one of the adapter implementations in their projects ``composer.json`` and instantiating the HttplugBundle in their kernel.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Full configuration
2+
==================
3+
4+
This page shows an example of all configuration values provided by the bundle.
5+
6+
.. code-block:: yaml
7+
8+
// config.yml
9+
httplug:
10+
main_alias:
11+
client: httplug.client.default
12+
message_factory: httplug.message_factory.default
13+
uri_factory: httplug.uri_factory.default
14+
stream_factory: httplug.stream_factory.default
15+
classes:
16+
# uses discovery if not specified
17+
client: ~
18+
message_factory: ~
19+
uri_factory: ~
20+
stream_factory: ~
21+
22+
plugins:
23+
authentication:
24+
my_basic:
25+
type: 'basic'
26+
username: 'my_username'
27+
password: 'p4ssw0rd'
28+
my_wsse:
29+
type: 'wsse'
30+
username: 'my_username'
31+
password: 'p4ssw0rd'
32+
my_bearer:
33+
type: 'bearer'
34+
token: 'authentication_token_hash'
35+
my_service:
36+
type: 'service'
37+
service: 'my_authentication_service'
38+
cache:
39+
enabled: true
40+
cache_pool: 'my_cache_pool'
41+
stream_factory: 'httplug.stream_factory'
42+
config:
43+
default_ttl: 3600
44+
respect_cache_headers: true
45+
cookie:
46+
enabled: true
47+
cookie_jar: my_cookie_jar
48+
decoder:
49+
enabled: true
50+
use_content_encoding: true
51+
history:
52+
enabled: true
53+
journal: my_journal
54+
logger:
55+
enabled: true
56+
logger: 'logger'
57+
formatter: null
58+
redirect:
59+
enabled: true
60+
preserve_header: true
61+
use_default_for_multiple: true
62+
retry:
63+
enabled: true
64+
retry: 1
65+
stopwatch:
66+
enabled: true
67+
stopwatch: 'debug.stopwatch'
68+
clients:
69+
acme:
70+
factory: 'httplug.factory.guzzle6'
71+
plugins: ['httplug.plugin.authentication.my_wsse', 'httplug.plugin.cache', 'httplug.plugin.retry']
72+
config:
73+
verify: false
74+
timeout: 2
75+
# more options to the guzzle 6 constructor
76+

0 commit comments

Comments
 (0)