Skip to content

Adding clients factories for Curl, React and Socket #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions ClientFactory/CurlFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Http\HttplugBundle\ClientFactory;

use Http\Client\Curl\Client;
use Http\Message\MessageFactory;
use Http\Message\StreamFactory;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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);
}
}
16 changes: 15 additions & 1 deletion ClientFactory/Guzzle5Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@

use GuzzleHttp\Client;
use Http\Adapter\Guzzle5\Client as Adapter;
use Http\Message\MessageFactory;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class Guzzle5Factory implements ClientFactory
{
/**
* @var MessageFactory
*/
private $messageFactory;

/**
* @param MessageFactory $messageFactory
*/
public function __construct(MessageFactory $messageFactory)
{
$this->messageFactory = $messageFactory;
}

/**
* {@inheritdoc}
*/
Expand All @@ -21,6 +35,6 @@ public function createClient(array $config = [])

$client = new Client($config);

return new Adapter($client);
return new Adapter($client, $this->messageFactory);
}
}
37 changes: 37 additions & 0 deletions ClientFactory/ReactFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Http\HttplugBundle\ClientFactory;

use Http\Adapter\React\Client;
use Http\Message\MessageFactory;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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);
}
}
37 changes: 37 additions & 0 deletions ClientFactory/SocketFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Http\HttplugBundle\ClientFactory;

use Http\Client\Socket\Client;
use Http\Message\MessageFactory;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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);
}
}
14 changes: 13 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
<services>

<!-- ClientFactories -->
<service id="httplug.factory.guzzle5" class="Http\HttplugBundle\ClientFactory\Guzzle5Factory" public="false" />
<service id="httplug.factory.curl" class="Http\HttplugBundle\ClientFactory\CurlFactory" public="false">
<argument type="service" id="httplug.message_factory"/>
<argument type="service" id="httplug.stream_factory"/>
</service>
<service id="httplug.factory.guzzle5" class="Http\HttplugBundle\ClientFactory\Guzzle5Factory" public="false">
<argument type="service" id="httplug.message_factory"/>
</service>
<service id="httplug.factory.guzzle6" class="Http\HttplugBundle\ClientFactory\Guzzle6Factory" public="false" />
<service id="httplug.factory.react" class="Http\HttplugBundle\ClientFactory\ReactFactory" public="false">
<argument type="service" id="httplug.message_factory"/>
</service>
<service id="httplug.factory.socket" class="Http\HttplugBundle\ClientFactory\SocketFactory" public="false">
<argument type="service" id="httplug.message_factory"/>
</service>
</services>
</container>