From 342fb17a737e4bef33da9dd6884550ced18ed324 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 1 Mar 2016 18:06:51 +0100 Subject: [PATCH] Adding clients factories for Curl, React and Socket --- ClientFactory/CurlFactory.php | 45 ++++++++++++++++++++++++++++++++ ClientFactory/Guzzle5Factory.php | 16 +++++++++++- ClientFactory/ReactFactory.php | 37 ++++++++++++++++++++++++++ ClientFactory/SocketFactory.php | 37 ++++++++++++++++++++++++++ Resources/config/services.xml | 14 +++++++++- 5 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 ClientFactory/CurlFactory.php create mode 100644 ClientFactory/ReactFactory.php create mode 100644 ClientFactory/SocketFactory.php diff --git a/ClientFactory/CurlFactory.php b/ClientFactory/CurlFactory.php new file mode 100644 index 00000000..12f3814a --- /dev/null +++ b/ClientFactory/CurlFactory.php @@ -0,0 +1,45 @@ + + */ +class CurlFactory implements ClientFactory +{ + /** + * @var MessageFactory + */ + private $messageFactory; + + /** + * @var StreamFactory + */ + private $streamFactory; + + /** + * @param MessageFactory $messageFactory + * @param StreamFactory $streamFactory + */ + public function __construct(MessageFactory $messageFactory, StreamFactory $streamFactory) + { + $this->messageFactory = $messageFactory; + $this->streamFactory = $streamFactory; + } + + /** + * {@inheritdoc} + */ + public function createClient(array $config = []) + { + if (!class_exists('Http\Client\Curl\Client')) { + throw new \LogicException('To use the Curl client you need to install the "php-http/curl-client" package.'); + } + + return new Client($this->messageFactory, $this->streamFactory, $config); + } +} diff --git a/ClientFactory/Guzzle5Factory.php b/ClientFactory/Guzzle5Factory.php index 8f9a1f84..7f5824ae 100644 --- a/ClientFactory/Guzzle5Factory.php +++ b/ClientFactory/Guzzle5Factory.php @@ -4,12 +4,26 @@ use GuzzleHttp\Client; use Http\Adapter\Guzzle5\Client as Adapter; +use Http\Message\MessageFactory; /** * @author Tobias Nyholm */ class Guzzle5Factory implements ClientFactory { + /** + * @var MessageFactory + */ + private $messageFactory; + + /** + * @param MessageFactory $messageFactory + */ + public function __construct(MessageFactory $messageFactory) + { + $this->messageFactory = $messageFactory; + } + /** * {@inheritdoc} */ @@ -21,6 +35,6 @@ public function createClient(array $config = []) $client = new Client($config); - return new Adapter($client); + return new Adapter($client, $this->messageFactory); } } diff --git a/ClientFactory/ReactFactory.php b/ClientFactory/ReactFactory.php new file mode 100644 index 00000000..1e8d6212 --- /dev/null +++ b/ClientFactory/ReactFactory.php @@ -0,0 +1,37 @@ + + */ +class ReactFactory implements ClientFactory +{ + /** + * @var MessageFactory + */ + private $messageFactory; + + /** + * @param MessageFactory $messageFactory + */ + public function __construct(MessageFactory $messageFactory) + { + $this->messageFactory = $messageFactory; + } + + /** + * {@inheritdoc} + */ + public function createClient(array $config = []) + { + if (!class_exists('Http\Adapter\React\Client')) { + throw new \LogicException('To use the React adapter you need to install the "php-http/react-adapter" package.'); + } + + return new Client($this->messageFactory); + } +} diff --git a/ClientFactory/SocketFactory.php b/ClientFactory/SocketFactory.php new file mode 100644 index 00000000..58c93fa6 --- /dev/null +++ b/ClientFactory/SocketFactory.php @@ -0,0 +1,37 @@ + + */ +class SocketFactory implements ClientFactory +{ + /** + * @var MessageFactory + */ + private $messageFactory; + + /** + * @param MessageFactory $messageFactory + */ + public function __construct(MessageFactory $messageFactory) + { + $this->messageFactory = $messageFactory; + } + + /** + * {@inheritdoc} + */ + public function createClient(array $config = []) + { + if (!class_exists('Http\Client\Socket\Client')) { + throw new \LogicException('To use the Socket client you need to install the "php-http/socket-client" package.'); + } + + return new Client($this->messageFactory, $config); + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 369e8902..450ccac3 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -6,7 +6,19 @@ - + + + + + + + + + + + + +