Skip to content

Commit 9401709

Browse files
committed
Adding clients factories for Curl, React and Socket
1 parent 5eadbc6 commit 9401709

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed

ClientFactory/CurlFactory.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\ClientFactory;
4+
5+
use Http\Client\Curl\Client;
6+
use Http\Message\MessageFactory;
7+
use Http\Message\StreamFactory;
8+
9+
/**
10+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
11+
*/
12+
class CurlFactory implements ClientFactory
13+
{
14+
/**
15+
* @var MessageFactory
16+
*/
17+
private $messageFactory;
18+
19+
/**
20+
* @var StreamFactory
21+
*/
22+
private $streamFactory;
23+
24+
/**
25+
* @param MessageFactory $messageFactory
26+
* @param StreamFactory $streamFactory
27+
*/
28+
public function __construct(MessageFactory $messageFactory, StreamFactory $streamFactory)
29+
{
30+
$this->messageFactory = $messageFactory;
31+
$this->streamFactory = $streamFactory;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function createClient(array $config = [])
38+
{
39+
if (!class_exists('Http\Client\Curl\Client')) {
40+
throw new \LogicException('To use the Curl client you need to install the "php-http/curl-client" package.');
41+
}
42+
43+
return new Client($this->messageFactory, $this->streamFactory, $config);
44+
}
45+
}

ClientFactory/Guzzle5Factory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@
44

55
use GuzzleHttp\Client;
66
use Http\Adapter\Guzzle5\Client as Adapter;
7+
use Http\Message\MessageFactory;
78

89
/**
910
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1011
*/
1112
class Guzzle5Factory implements ClientFactory
1213
{
14+
/**
15+
* @var MessageFactory
16+
*/
17+
private $messageFactory;
18+
19+
/**
20+
* @param MessageFactory $messageFactory
21+
*/
22+
public function __construct(MessageFactory $messageFactory)
23+
{
24+
$this->messageFactory = $messageFactory;
25+
}
26+
1327
/**
1428
* {@inheritdoc}
1529
*/
@@ -21,6 +35,6 @@ public function createClient(array $config = [])
2135

2236
$client = new Client($config);
2337

24-
return new Adapter($client);
38+
return new Adapter($client, $this->messageFactory);
2539
}
2640
}

ClientFactory/ReactFactory.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\ClientFactory;
4+
5+
use Http\Adapter\React\Client;
6+
use Http\Message\MessageFactory;
7+
8+
/**
9+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
10+
*/
11+
class ReactFactory implements ClientFactory
12+
{
13+
/**
14+
* @var MessageFactory
15+
*/
16+
private $messageFactory;
17+
18+
/**
19+
* @param MessageFactory $messageFactory
20+
*/
21+
public function __construct(MessageFactory $messageFactory)
22+
{
23+
$this->messageFactory = $messageFactory;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function createClient(array $config = [])
30+
{
31+
if (!class_exists('Http\Adapter\React\Client')) {
32+
throw new \LogicException('To use the React adapter you need to install the "php-http/react-adapter" package.');
33+
}
34+
35+
return new Client($this->messageFactory);
36+
}
37+
}

ClientFactory/SocketFactory.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\ClientFactory;
4+
5+
use Http\Client\Socket\Client;
6+
use Http\Message\MessageFactory;
7+
8+
/**
9+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
10+
*/
11+
class SocketFactory implements ClientFactory
12+
{
13+
/**
14+
* @var MessageFactory
15+
*/
16+
private $messageFactory;
17+
18+
/**
19+
* @param MessageFactory $messageFactory
20+
*/
21+
public function __construct(MessageFactory $messageFactory)
22+
{
23+
$this->messageFactory = $messageFactory;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function createClient(array $config = [])
30+
{
31+
if (!class_exists('Http\Client\Socket\Client')) {
32+
throw new \LogicException('To use the Socket clinet you need to install the "php-http/socket-client" package.');
33+
}
34+
35+
return new Client($this->messageFactory, $config);
36+
}
37+
}

Resources/config/services.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66
<services>
77

88
<!-- ClientFactories -->
9-
<service id="httplug.factory.guzzle5" class="Http\HttplugBundle\ClientFactory\Guzzle5Factory" public="false" />
9+
<service id="httplug.factory.curl" class="Http\HttplugBundle\ClientFactory\CurlFactory" public="false">
10+
<argument type="service" id="httplug.message_factory"/>
11+
<argument type="service" id="httplug.stream_factory"/>
12+
</service>
13+
<service id="httplug.factory.guzzle5" class="Http\HttplugBundle\ClientFactory\Guzzle5Factory" public="false">
14+
<argument type="service" id="httplug.message_factory"/>
15+
</service>
1016
<service id="httplug.factory.guzzle6" class="Http\HttplugBundle\ClientFactory\Guzzle6Factory" public="false" />
17+
<service id="httplug.factory.react" class="Http\HttplugBundle\ClientFactory\ReactFactory" public="false">
18+
<argument type="service" id="httplug.message_factory"/>
19+
</service>
20+
<service id="httplug.factory.socket" class="Http\HttplugBundle\ClientFactory\SocketFactory" public="false">
21+
<argument type="service" id="httplug.message_factory"/>
22+
</service>
1123
</services>
1224
</container>

0 commit comments

Comments
 (0)