Skip to content

Commit eacd0af

Browse files
committed
Merge pull request #94 from Nyholm/symfony_bundle_2
Updated docs for the bundle
2 parents 7c2dbb9 + 5845636 commit eacd0af

File tree

4 files changed

+186
-88
lines changed

4 files changed

+186
-88
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

+109-88
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
@@ -109,68 +100,98 @@ You can configure your clients with some good default options. The clients are l
109100
$httpClient = $this->container->get('httplug.client.my_guzzle5');
110101
$httpClient = $this->container->get('httplug.client.acme');
111102
103+
// will be the same as ``httplug.client.my_guzzle5``
104+
$httpClient = $this->container->get('httplug.client');
105+
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.guzzle5``
109+
* ``httplug.factory.guzzle6``
112110

113111
Plugins
114112
```````
115113

116-
You can configure the clients with plugins.
114+
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.
117115

118116
.. code-block:: yaml
119117
120118
// services.yml
121119
acme_plugin:
122-
class: Acme\Plugin\MyCustonPlugin
123-
arguments: ["%api_key%"]
120+
class: Acme\Plugin\MyCustomPlugin
121+
arguments: ["%some_parameter%"]
124122
125123
.. code-block:: yaml
126124
127125
// config.yml
128-
httpug:
126+
httplug:
129127
plugins:
130128
cache:
131129
cache_pool: 'my_cache_pool'
132130
clients:
133131
acme:
134132
factory: 'httplug.factory.guzzle6'
135-
plugins: ['acme_plugin', 'httplug.plugin.cache', ''httplug.plugin.retry']
136-
config:
137-
base_uri: 'http://google.se/'
133+
plugins: ['acme_plugin', 'httplug.plugin.cache', 'httplug.plugin.retry']
138134
139135
140136
Authentication
141137
``````````````
142138

139+
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>`.
140+
143141
.. code-block:: yaml
144142
145143
// config.yml
146-
httpug:
144+
httplug:
147145
plugins:
148146
authentication:
149-
my_basic:
150-
type: 'basic'
151-
username: 'my_username'
152-
password: 'p4ssw0rd'
153147
my_wsse:
154148
type: 'wsse'
155149
username: 'my_username'
156150
password: 'p4ssw0rd'
157-
my_brearer:
158-
type: 'bearer'
159-
token: 'authentication_token_hash'
160-
my_service:
161-
type: 'service'
162-
service: 'my_authentication_service'
163151
164152
clients:
165153
acme:
166154
factory: 'httplug.factory.guzzle6'
167155
plugins: ['httplug.plugin.authentication.my_wsse']
168156
169157
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.
158+
List of Services
159+
````````````````
160+
161+
+----------------------------------+-------------------------------------------------------------------------+
162+
| Service id | Description |
163+
+==================================+=========================================================================+
164+
| httplug.message_factory | Service* that provides the `Http\Message\MessageFactory` |
165+
+----------------------------------+-------------------------------------------------------------------------+
166+
| httplug.uri_factory | Service* that provides the `Http\Message\UriFactory` |
167+
+----------------------------------+-------------------------------------------------------------------------+
168+
| httplug.stream_factory | Service* that provides the `Http\Message\StreamFactory` |
169+
+----------------------------------+-------------------------------------------------------------------------+
170+
| httplug.client.[name] | There is one service per named client. |
171+
+----------------------------------+-------------------------------------------------------------------------+
172+
| httplug.client | | If there is a client named "default", this service is an alias to |
173+
| | | that client, otherwise it is an alias to the first client configured. |
174+
+----------------------------------+-------------------------------------------------------------------------+
175+
| | httplug.plugin.content_length | | These are plugins that are enabled by default. |
176+
| | httplug.plugin.decoder | | These services are private and should only be used to configure |
177+
| | httplug.plugin.error | | clients or other services. |
178+
| | httplug.plugin.logger | |
179+
| | httplug.plugin.redirect | |
180+
| | httplug.plugin.retry | |
181+
| | httplug.plugin.stopwatch | |
182+
+----------------------------------+-------------------------------------------------------------------------+
183+
| | httplug.plugin.cache | | These are plugins that are disabled by default and only get |
184+
| | httplug.plugin.cookie | | activated when configured. |
185+
| | httplug.plugin.history | | These services are private and should only be used to configure |
186+
| | | clients or other services. |
187+
+----------------------------------+-------------------------------------------------------------------------+
188+
189+
\* *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.*
190+
191+
192+
Useage for Reusable Bundles
193+
``````````````````````````
194+
195+
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.
196+
197+
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)