|
| 1 | +Symfony Bundle |
| 2 | +============== |
| 3 | + |
| 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. |
| 47 | + |
| 48 | +.. code-block:: yaml |
| 49 | +
|
| 50 | + httplug: |
| 51 | + 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: ~ |
| 66 | +
|
| 67 | +
|
| 68 | +Configuration without auto discovery |
| 69 | +```````````````````````````````````` |
| 70 | + |
| 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): |
| 72 | + |
| 73 | +.. code-block:: yaml |
| 74 | +
|
| 75 | + httplug: |
| 76 | + classes: |
| 77 | + client: Http\Adapter\Guzzle6\Client |
| 78 | + message_factory: Http\Message\MessageFactory\GuzzleMessageFactory |
| 79 | + uri_factory: Http\Message\UriFactory\GuzzleUriFactory |
| 80 | + stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory |
| 81 | +
|
| 82 | +
|
| 83 | +Configure your client |
| 84 | +````````````````````` |
| 85 | + |
| 86 | +You can configure your clients with some good default options. The clients are later registered as services. |
| 87 | + |
| 88 | +.. code-block:: yaml |
| 89 | +
|
| 90 | + httplug: |
| 91 | + clients: |
| 92 | + my_guzzle5: |
| 93 | + factory: 'httplug.factory.guzzle5' |
| 94 | + config: |
| 95 | + # These options are given to Guzzle without validation. |
| 96 | + defaults: |
| 97 | + base_uri: 'http://google.se/' |
| 98 | + verify_ssl: false |
| 99 | + timeout: 4 |
| 100 | + headers: |
| 101 | + Content-Type: 'application/json' |
| 102 | + acme: |
| 103 | + factory: 'httplug.factory.guzzle6' |
| 104 | + config: |
| 105 | + base_uri: 'http://google.se/' |
| 106 | +
|
| 107 | +.. code-block:: php |
| 108 | +
|
| 109 | + $httpClient = $this->container->get('httplug.client.my_guzzle5'); |
| 110 | + $httpClient = $this->container->get('httplug.client.acme'); |
| 111 | +
|
| 112 | +
|
| 113 | +Plugins |
| 114 | +``````` |
| 115 | + |
| 116 | +You can configure the clients with plugins. |
| 117 | + |
| 118 | +.. code-block:: yaml |
| 119 | +
|
| 120 | + // services.yml |
| 121 | + acme_plugin: |
| 122 | + class: Acme\Plugin\MyCustonPlugin |
| 123 | + arguments: ["%api_key%"] |
| 124 | +
|
| 125 | +.. code-block:: yaml |
| 126 | +
|
| 127 | + // config.yml |
| 128 | + httpug: |
| 129 | + plugins: |
| 130 | + cache: |
| 131 | + cache_pool: 'my_cache_pool' |
| 132 | + clients: |
| 133 | + acme: |
| 134 | + factory: 'httplug.factory.guzzle6' |
| 135 | + plugins: ['acme_plugin', 'httplug.plugin.cache', ''httplug.plugin.retry'] |
| 136 | + config: |
| 137 | + base_uri: 'http://google.se/' |
| 138 | +
|
| 139 | +
|
| 140 | +Authentication |
| 141 | +`````````````` |
| 142 | + |
| 143 | +.. code-block:: yaml |
| 144 | +
|
| 145 | + // config.yml |
| 146 | + httpug: |
| 147 | + plugins: |
| 148 | + authentication: |
| 149 | + my_basic: |
| 150 | + type: 'basic' |
| 151 | + username: 'my_username' |
| 152 | + password: 'p4ssw0rd' |
| 153 | + my_wsse: |
| 154 | + type: 'wsse' |
| 155 | + username: 'my_username' |
| 156 | + password: 'p4ssw0rd' |
| 157 | + my_brearer: |
| 158 | + type: 'bearer' |
| 159 | + token: 'authentication_token_hash' |
| 160 | + my_service: |
| 161 | + type: 'service' |
| 162 | + service: 'my_authentication_service' |
| 163 | +
|
| 164 | + clients: |
| 165 | + acme: |
| 166 | + factory: 'httplug.factory.guzzle6' |
| 167 | + plugins: ['httplug.plugin.authentication.my_wsse'] |
| 168 | +
|
| 169 | +
|
| 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. |
0 commit comments