|
| 1 | +Symfony Bundle |
| 2 | +============== |
| 3 | + |
| 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 | +This guide explains how to configure HTTPlug in the Symfony framework. See the :doc:`../httplug/tutorial` for examples how to use HTTPlug in general. |
| 7 | + |
| 8 | +Installation |
| 9 | +```````````` |
| 10 | + |
| 11 | +Install the HTTPlug bundle with composer and enable it in your AppKernel.php. |
| 12 | + |
| 13 | +.. code-block:: bash |
| 14 | +
|
| 15 | + $ composer require php-http/httplug-bundle |
| 16 | +
|
| 17 | +.. code-block:: php |
| 18 | +
|
| 19 | + public function registerBundles() |
| 20 | + { |
| 21 | + $bundles = array( |
| 22 | + // ... |
| 23 | + new Http\HttplugBundle\HttplugBundle(), |
| 24 | + ); |
| 25 | + } |
| 26 | +
|
| 27 | +You will find all available configuration at the :doc:`full configuration </integrations/symfony-full-configuration>` page. |
| 28 | + |
| 29 | +Usage |
| 30 | +````` |
| 31 | + |
| 32 | +.. code-block:: yaml |
| 33 | +
|
| 34 | + httplug: |
| 35 | + plugins: |
| 36 | + logger: ~ |
| 37 | + clients: |
| 38 | + acme: |
| 39 | + factory: 'httplug.factory.guzzle6' |
| 40 | + plugins: ['httplug.plugin.logger'] |
| 41 | + config: |
| 42 | + verify: false |
| 43 | + timeout: 2 |
| 44 | +
|
| 45 | +.. code-block:: php |
| 46 | +
|
| 47 | + $request = $this->container->get('httplug.message_factory')->createRequest('GET', 'http://example.com'); |
| 48 | + $response = $this->container->get('httplug.client.acme')->sendRequest($request); |
| 49 | +
|
| 50 | +
|
| 51 | +Web Debug Toolbar |
| 52 | +````````````````` |
| 53 | +.. image:: /assets/img/debug-toolbar.png |
| 54 | + :align: right |
| 55 | + :width: 120px |
| 56 | + |
| 57 | +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. |
| 58 | + |
| 59 | +Discovery of Factory Classes |
| 60 | +```````````````````````````` |
| 61 | + |
| 62 | +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: |
| 63 | + |
| 64 | +.. code-block:: yaml |
| 65 | +
|
| 66 | + httplug: |
| 67 | + classes: |
| 68 | + client: Http\Adapter\Guzzle6\Client |
| 69 | + message_factory: Http\Message\MessageFactory\GuzzleMessageFactory |
| 70 | + uri_factory: Http\Message\UriFactory\GuzzleUriFactory |
| 71 | + stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory |
| 72 | +
|
| 73 | +
|
| 74 | +
|
| 75 | +Configure Clients |
| 76 | +````````````````` |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | +.. code-block:: yaml |
| 81 | +
|
| 82 | + httplug: |
| 83 | + clients: |
| 84 | + my_guzzle5: |
| 85 | + factory: 'httplug.factory.guzzle5' |
| 86 | + config: |
| 87 | + # These options are given to Guzzle without validation. |
| 88 | + defaults: |
| 89 | + verify_ssl: false |
| 90 | + timeout: 4 |
| 91 | + acme: |
| 92 | + factory: 'httplug.factory.curl' |
| 93 | + config: |
| 94 | + 78: 4 #CURLOPT_CONNECTTIMEOUT |
| 95 | +
|
| 96 | +.. code-block:: php |
| 97 | +
|
| 98 | + $httpClient = $this->container->get('httplug.client.my_guzzle5'); |
| 99 | + $httpClient = $this->container->get('httplug.client.curl'); |
| 100 | +
|
| 101 | + // will be the same as ``httplug.client.my_guzzle5`` |
| 102 | + $httpClient = $this->container->get('httplug.client'); |
| 103 | +
|
| 104 | +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 built-in services are: |
| 105 | + |
| 106 | +* ``httplug.factory.curl`` |
| 107 | +* ``httplug.factory.guzzle5`` |
| 108 | +* ``httplug.factory.guzzle6`` |
| 109 | +* ``httplug.factory.react`` |
| 110 | +* ``httplug.factory.socket`` |
| 111 | + |
| 112 | +Plugins |
| 113 | +``````` |
| 114 | + |
| 115 | +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. |
| 116 | + |
| 117 | +.. code-block:: yaml |
| 118 | +
|
| 119 | + // services.yml |
| 120 | + acme_plugin: |
| 121 | + class: Acme\Plugin\MyCustomPlugin |
| 122 | + arguments: ["%some_parameter%"] |
| 123 | +
|
| 124 | +.. code-block:: yaml |
| 125 | +
|
| 126 | + // config.yml |
| 127 | + httplug: |
| 128 | + plugins: |
| 129 | + cache: |
| 130 | + cache_pool: 'my_cache_pool' |
| 131 | + clients: |
| 132 | + acme: |
| 133 | + factory: 'httplug.factory.guzzle6' |
| 134 | + plugins: ['acme_plugin', 'httplug.plugin.cache', 'httplug.plugin.retry'] |
| 135 | +
|
| 136 | +
|
| 137 | +Authentication |
| 138 | +`````````````` |
| 139 | + |
| 140 | +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>`. |
| 141 | + |
| 142 | +.. code-block:: yaml |
| 143 | +
|
| 144 | + // config.yml |
| 145 | + httplug: |
| 146 | + plugins: |
| 147 | + authentication: |
| 148 | + my_wsse: |
| 149 | + type: 'wsse' |
| 150 | + username: 'my_username' |
| 151 | + password: 'p4ssw0rd' |
| 152 | +
|
| 153 | + clients: |
| 154 | + acme: |
| 155 | + factory: 'httplug.factory.guzzle6' |
| 156 | + plugins: ['httplug.plugin.authentication.my_wsse'] |
| 157 | +
|
| 158 | +
|
| 159 | +List of Services |
| 160 | +```````````````` |
| 161 | + |
| 162 | ++-------------------------------------+-------------------------------------------------------------------------+ |
| 163 | +| Service id | Description | |
| 164 | ++=====================================+=========================================================================+ |
| 165 | +| ``httplug.message_factory`` | Service* that provides the `Http\Message\MessageFactory` | |
| 166 | ++-------------------------------------+-------------------------------------------------------------------------+ |
| 167 | +| ``httplug.uri_factory`` | Service* that provides the `Http\Message\UriFactory` | |
| 168 | ++-------------------------------------+-------------------------------------------------------------------------+ |
| 169 | +| ``httplug.stream_factory`` | Service* that provides the `Http\Message\StreamFactory` | |
| 170 | ++-------------------------------------+-------------------------------------------------------------------------+ |
| 171 | +| ``httplug.client.[name]`` | There is one service per named client. | |
| 172 | ++-------------------------------------+-------------------------------------------------------------------------+ |
| 173 | +| ``httplug.client`` | | If there is a client named "default", this service is an alias to | |
| 174 | +| | | that client, otherwise it is an alias to the first client configured. | |
| 175 | ++-------------------------------------+-------------------------------------------------------------------------+ |
| 176 | +| | ``httplug.plugin.content_length`` | | These are plugins that are enabled by default. | |
| 177 | +| | ``httplug.plugin.decoder`` | | These services are private and should only be used to configure | |
| 178 | +| | ``httplug.plugin.error`` | | clients or other services. | |
| 179 | +| | ``httplug.plugin.logger`` | | |
| 180 | +| | ``httplug.plugin.redirect`` | | |
| 181 | +| | ``httplug.plugin.retry`` | | |
| 182 | +| | ``httplug.plugin.stopwatch`` | | |
| 183 | ++-------------------------------------+-------------------------------------------------------------------------+ |
| 184 | +| | ``httplug.plugin.cache`` | | These are plugins that are disabled by default and only get | |
| 185 | +| | ``httplug.plugin.cookie`` | | activated when configured. | |
| 186 | +| | ``httplug.plugin.history`` | | These services are private and should only be used to configure | |
| 187 | +| | | clients or other services. | |
| 188 | ++-------------------------------------+-------------------------------------------------------------------------+ |
| 189 | + |
| 190 | +\* *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.* |
| 191 | + |
| 192 | + |
| 193 | +Usage for Reusable Bundles |
| 194 | +`````````````````````````` |
| 195 | + |
| 196 | +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. |
| 197 | + |
| 198 | +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