Skip to content

Commit 533862d

Browse files
committed
Removed the generic factory
1 parent b6d7c68 commit 533862d

File tree

13 files changed

+35
-195
lines changed

13 files changed

+35
-195
lines changed

ClientFactory/ClientFactoryInterface.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,10 @@
22

33
namespace Http\HttplugBundle\ClientFactory;
44

5-
use Http\Client\HttpClient;
6-
use Http\HttplugBundle\Exception\InvalidConfiguration;
7-
85
/**
96
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
107
*/
118
interface ClientFactoryInterface
129
{
13-
/**
14-
* Configure and return a client.
15-
*
16-
* @param array $config
17-
*
18-
* @return HttpClient
19-
*
20-
* @throws InvalidConfiguration
21-
*/
2210
public function createClient(array $config = array());
23-
24-
/**
25-
* Return the name of the client.
26-
*
27-
* @return string
28-
*/
29-
public function getName();
3011
}

ClientFactory/DummyClient.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\ClientFactory;
4+
5+
/**
6+
* This client is used as a placeholder for the dependency injection. It will never be used.
7+
*
8+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
9+
*/
10+
class DummyClient
11+
{
12+
}

ClientFactory/DummyFactory.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

ClientFactory/GenericClientFactory.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

ClientFactory/Guzzle5Factory.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,19 @@
88
/**
99
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1010
*/
11-
class Guzzle5Factory implements ClientFactoryInterface
11+
class Guzzle5Factory
1212
{
1313
/**
1414
* {@inheritdoc}
1515
*/
1616
public function createClient(array $config = array())
1717
{
18+
if (!class_exists('Http\Adapter\Guzzle5HttpAdapter')) {
19+
throw new \LogicException('To use the Guzzle5 adapter you need to install the "php-http/guzzle5-adapter" package.');
20+
}
21+
1822
$client = new Client($config);
1923

2024
return new Guzzle5HttpAdapter($client);
2125
}
22-
23-
/**
24-
* {@inheritdoc}
25-
*/
26-
public function getName()
27-
{
28-
return 'guzzle5';
29-
}
3026
}

ClientFactory/Guzzle6Factory.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ class Guzzle6Factory implements ClientFactoryInterface
1212
{
1313
public function createClient(array $config = array())
1414
{
15+
if (!class_exists('Http\Adapter\Guzzle6HttpAdapter')) {
16+
throw new \LogicException('To use the Guzzle6 adapter you need to install the "php-http/guzzle6-adapter" package.');
17+
}
18+
1519
$client = new Client($config);
1620

1721
return new Guzzle6HttpAdapter($client);
1822
}
19-
20-
public function getName()
21-
{
22-
return 'guzzle6';
23-
}
2423
}

DependencyInjection/CompilerPass/ClientFactoryPass.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

DependencyInjection/Configuration.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
use Symfony\Component\Config\Definition\ArrayNode;
66
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7-
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
87
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
98
use Symfony\Component\Config\Definition\ConfigurationInterface;
109
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1110

1211
/**
13-
* This class contains the configuration information for the bundle
12+
* This class contains the configuration information for the bundle.
1413
*
1514
* This information is solely responsible for how the different configuration
1615
* sections are normalized, and merged.
@@ -86,14 +85,13 @@ protected function configureClients(ArrayNodeDefinition $root)
8685
->useAttributeAsKey('name')
8786
->prototype('array')
8887
->children()
89-
->scalarNode('adapter')
88+
->scalarNode('factory')
9089
->isRequired()
9190
->cannotBeEmpty()
9291
->info('The name of the adapter to use. There must be a factory corresponding to this name. Example values: guzzle5, guzzle6')
9392
->end()
9493
->variableNode('config')->end()
9594
->end()
9695
->end();
97-
9896
}
9997
}

DependencyInjection/HttplugExtension.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Http\HttplugBundle\DependencyInjection;
44

5-
use Http\HttplugBundle\ClientFactory\DummyFactory;
5+
use Http\HttplugBundle\ClientFactory\DummyClient;
66
use Symfony\Component\DependencyInjection\ContainerBuilder;
77
use Symfony\Component\Config\FileLocator;
88
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -37,35 +37,11 @@ public function load(array $configs, ContainerBuilder $container)
3737
$container->setAlias(sprintf('httplug.%s', $type), $id);
3838
}
3939

40-
// Configure services
40+
// Configure client services
4141
foreach ($config['clients'] as $name => $arguments) {
42-
$this->verifyDependencies($arguments['adapter']);
43-
$def = $container->register('httplug.'.$name, DummyFactory::class);
44-
$def->setFactory([new Reference('httplug.client.factory'), 'getClient'])
45-
->addArgument($arguments['adapter'])
42+
$def = $container->register('httplug.client.'.$name, DummyClient::class);
43+
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
4644
->addArgument($arguments['config']);
4745
}
4846
}
49-
50-
/**
51-
* Make sure we got all the dependencies installed.
52-
*
53-
* @param $adapterName
54-
*/
55-
protected function verifyDependencies($adapterName)
56-
{
57-
$map = [
58-
'guzzle5'=>['package'=>'php-http/guzzle5-adapter', 'class'=>'Http\Adapter\Guzzle5HttpAdapter'],
59-
'guzzle6'=>['package'=>'php-http/guzzle6-adapter', 'class'=>'Http\Adapter\Guzzle6HttpAdapter'],
60-
];
61-
62-
if (!isset($map[$adapterName])) {
63-
// This is none of our adapters. We have to assume the user has configured one of his own.
64-
return;
65-
}
66-
67-
if (!class_exists($map[$adapterName]['class'])) {
68-
throw new \LogicException(sprintf('To use the "%s" adapter you need to install the "%s" package. ', $adapterName, $map[$adapterName]['package']));
69-
}
70-
}
7147
}

HttplugBundle.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Http\HttplugBundle;
44

5-
use Http\HttplugBundle\DependencyInjection\CompilerPass\ClientFactoryPass;
65
use Symfony\Component\DependencyInjection\ContainerBuilder;
76
use Symfony\Component\HttpKernel\Bundle\Bundle;
87

@@ -11,13 +10,4 @@
1110
*/
1211
class HttplugBundle extends Bundle
1312
{
14-
/**
15-
* {@inheritdoc}
16-
*/
17-
public function build(ContainerBuilder $container)
18-
{
19-
parent::build($container);
20-
$container->addCompilerPass(new ClientFactoryPass());
21-
22-
}
2313
}

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ You can configure your clients with some good default options. The clients are l
7777
httplug:
7878
clients:
7979
my_guzzle5:
80-
adapter: guzzle5
80+
factory: 'httplug.factory.guzzle5'
8181
config:
8282
# These options are given to Guzzle without validation.
8383
base_url: 'http://google.se/'
@@ -87,17 +87,16 @@ httplug:
8787
headers:
8888
Content-Type: 'application/json'
8989
acme:
90-
adapter: guzzle6
90+
factory: 'httplug.factory.guzzle6'
9191
config:
92-
# These options are given to Guzzle without validation.
9392
base_url: 'http://google.se/'
9493

9594
```
9695

9796
```php
9897

99-
$httpClient = $this->container->get('httplug.my_guzzle5');
100-
$httpClient = $this->container->get('httplug.acme');
98+
$httpClient = $this->container->get('httplug.client.my_guzzle5');
99+
$httpClient = $this->container->get('httplug.client.acme');
101100
```
102101

103102
### Use for Reusable Bundles

Resources/config/services.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55

66
<services>
7-
<service id="httplug.client.factory" class="Http\HttplugBundle\ClientFactory\GenericClientFactory" />
87

98
<!-- ClientFactories -->
10-
<service id="httplug.client.factory.guzzle5" class="Http\HttplugBundle\ClientFactory\Guzzle5Factory" public="false">
11-
<tag name="httplug.client_factory" />
12-
</service>
13-
<service id="httplug.client.factory.guzzle6" class="Http\HttplugBundle\ClientFactory\Guzzle6Factory" public="false">
14-
<tag name="httplug.client_factory" /></service>
9+
<service id="httplug.factory.guzzle5" class="Http\HttplugBundle\ClientFactory\Guzzle5Factory" public="false" />
10+
<service id="httplug.factory.guzzle6" class="Http\HttplugBundle\ClientFactory\Guzzle6Factory" public="false" />
1511
</services>
1612
</container>

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"prefer-stable": true,
1616
"require": {
1717
"php": ">=5.4",
18-
"php-http/discovery": "^0.4",
18+
"php-http/discovery": "^0.5",
1919
"php-http/client-implementation": "^1.0",
2020
"php-http/message-factory": "^1.0",
2121
"php-http/plugins": "dev-master",
@@ -24,7 +24,7 @@
2424
"require-dev": {
2525
"phpunit/phpunit": "^4.4",
2626
"php-http/guzzle6-adapter": "dev-master",
27-
"php-http/message": "dev-master",
27+
"php-http/message": "^0.2",
2828
"php-http/client-common": "^0.1",
2929
"symfony/symfony": "^2.7|^3.0",
3030
"polishsymfonycommunity/symfony-mocker-container": "~1.0",

0 commit comments

Comments
 (0)