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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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
}

0 commit comments

Comments
 (0)