# PHP-HTTP

The PHP-HTTP (formerly HTTPlug) plugin system allows to wrap a Client with PluginClient and add some processing logic prior to and/or after sending the actual request. Fansipan includes an adapter that lets you use the plugin within the middleware system.

# Installation

You may use Composer to install package

composer require fansipan/http-plugin-adapter

If you want to use the cache or logger plugins, you'll need to install those as well:

composer require fansipan/http-plugin-adapter php-http/cache-plugin

# or

composer require fansipan/http-plugin-adapter php-http/logger-plugin

# Usage

Once the plugin has been installed, you may use the PluginAdapter middleware to setup the plugin:

use Fansipan\Contracts\ConnectorInterface;
use Fansipan\Traits\ConnectorTrait;
use Fansipan\HttpPluginAdapter\PluginAdapter;
use Http\Client\Common\Plugin\CachePlugin;
use Http\Client\Common\Plugin\CookiePlugin;
use Http\Client\Common\Plugin\LoggerPlugin;
use Http\Message\CookieJar;

final class Connector implements ConnectorInterface
{
    use ConnectorTrait;

    protected function defaultMiddleware(): array
    {
        return [
            new PluginAdapter([
                /** @var \Psr\Cache\CacheItemPoolInterface $pool */
                /** @var \Psr\Http\Message\StreamFactoryInterface $streamFactory */
                new CachePlugin($pool, $streamFactory),
                new CookiePlugin(new CookieJar()),
                /** @var \Psr\Log\LoggerInterface $logger */
                new Logger($logger),
            ])
        ];
    }
}
use Fansipan\HttpPluginAdapter\PluginAdapter;
use Http\Client\Common\Plugin\CachePlugin;
use Http\Client\Common\Plugin\CookiePlugin;
use Http\Client\Common\Plugin\LoggerPlugin;
use Http\Message\CookieJar;

$connector->middleware()->push(new PluginAdapter([
    /** @var \Psr\Cache\CacheItemPoolInterface $pool */
    /** @var \Psr\Http\Message\StreamFactoryInterface $streamFactory */
    new CachePlugin($pool, $streamFactory),
    new CookiePlugin(new CookieJar()),
    /** @var \Psr\Log\LoggerInterface $logger */
    new Logger($logger),
]));