Skip to content

Commit 20e10c2

Browse files
committed
building the initial bundle
1 parent c068832 commit 20e10c2

File tree

17 files changed

+516
-0
lines changed

17 files changed

+516
-0
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ php:
1010
env:
1111
global:
1212
- TEST_COMMAND="composer test"
13+
- SYMFONY_VERSION=2.7.*
1314

1415
matrix:
1516
allow_failures:
@@ -21,11 +22,16 @@ matrix:
2122
- COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
2223
- COVERAGE=true
2324
- TEST_COMMAND="composer test-ci"
25+
- php: 5.6
26+
env: SYMFONY_VERSION=2.8.*
27+
- php: 5.6
28+
env: SYMFONY_VERSION=3.0.*
2429

2530
before_install:
2631
- travis_retry composer self-update
2732

2833
install:
34+
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
2935
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction
3036

3137
script:

DependencyInjection/Configuration.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Http Client bundle.
5+
*
6+
* (c) David Buchmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please read the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Http\ClientBundle\DependencyInjection;
13+
14+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15+
use Symfony\Component\Config\Definition\ConfigurationInterface;
16+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17+
18+
/**
19+
* This class contains the configuration information for the bundle
20+
*
21+
* This information is solely responsible for how the different configuration
22+
* sections are normalized, and merged.
23+
*
24+
* @author David Buchmann <[email protected]>
25+
*/
26+
class Configuration implements ConfigurationInterface
27+
{
28+
/**
29+
* {@inheritDoc}
30+
*/
31+
public function getConfigTreeBuilder()
32+
{
33+
$treeBuilder = new TreeBuilder();
34+
$rootNode = $treeBuilder->root('http_client');
35+
36+
$rootNode
37+
->validate()
38+
->ifTrue(function ($v) {
39+
return !empty($v['classes']['client'])
40+
|| !empty($v['classes']['message_factory'])
41+
|| !empty($v['classes']['uri_factory'])
42+
;
43+
})
44+
->then(function ($v) {
45+
foreach ($v['classes'] as $key => $class) {
46+
if (null !== $class && !class_exists($class)) {
47+
throw new InvalidConfigurationException(sprintf(
48+
'Class %s specified for http_client.classes.%s does not exist.',
49+
$class,
50+
$key
51+
));
52+
}
53+
}
54+
55+
return $v;
56+
})
57+
->end()
58+
->children()
59+
->arrayNode('main_alias')
60+
->addDefaultsIfNotSet()
61+
->info('Configure which service the main alias point to.')
62+
->children()
63+
->scalarNode('client')->defaultValue('http_client.client.default')->end()
64+
->scalarNode('message_factory')->defaultValue('http_client.message_factory.default')->end()
65+
->scalarNode('uri_factory')->defaultValue('http_client.uri_factory.default')->end()
66+
->end()
67+
->end()
68+
->arrayNode('classes')
69+
->addDefaultsIfNotSet()
70+
->info('Overwrite a service class instead of using the discovery mechanism.')
71+
->children()
72+
->scalarNode('client')->defaultNull()->end()
73+
->scalarNode('message_factory')->defaultNull()->end()
74+
->scalarNode('uri_factory')->defaultNull()->end()
75+
->end()
76+
->end()
77+
->end()
78+
;
79+
80+
return $treeBuilder;
81+
}
82+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Http Client bundle.
5+
*
6+
* (c) David Buchmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please read the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Http\ClientBundle\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
17+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
class HttpClientExtension extends Extension
23+
{
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function load(array $configs, ContainerBuilder $container)
28+
{
29+
$configuration = $this->getConfiguration($configs, $container);
30+
$config = $this->processConfiguration($configuration, $configs);
31+
32+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
33+
34+
$loader->load('discovery.xml');
35+
foreach ($config['classes'] as $service => $class) {
36+
if ($class) {
37+
$container->removeDefinition(sprintf('http_client.%s.default', $service));
38+
$container->register(sprintf('http_client.%s.default', $service), $class);
39+
}
40+
}
41+
42+
foreach ($config['main_alias'] as $type => $id) {
43+
$container->setAlias(sprintf('http_client.%s', $type), $id);
44+
}
45+
}
46+
}

HttpClientBundle.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Http\ClientBundle;
13+
14+
use Symfony\Component\HttpKernel\Bundle\Bundle;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler\RequestAwarePass;
17+
use Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler\AddPublishedVotersPass;
18+
19+
class HttpClientBundle extends Bundle
20+
{
21+
}

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,56 @@ Via Composer
2020
$ composer require php-http/php-http-bundle
2121
```
2222

23+
Enable the bundle in your kernel:
24+
25+
``` php
26+
<?php
27+
// app/AppKernel.php
28+
29+
public function registerBundles()
30+
{
31+
$bundles = array(
32+
// ...
33+
new Http\ClientBundle\HttpClientBundle(),
34+
);
35+
}
36+
```
2337

2438
## Usage
2539

40+
TODO: move this to php-http-documentation? or keep it here?
41+
42+
The usage documentation is split into two parts. First we explain how to configure the bundle in an application. The second part is for developing reusable Symfony bundles that depend on an HTTP client defined by the php-http interface.
43+
44+
### Use in applications
45+
46+
This bundle provides 3 services:
47+
48+
* `php_http.client` a service that provides the `Http\Adapter\HttpAdapter` (TODO: proper interface?)
49+
* `php_http.message_factory` a service that provides the `Http\Message\MessageFactory`
50+
* `php_http.uri_factory` a service that provides the `Http\Message\UriFactory`
51+
52+
These services are always an alias to another service. You can specify your own service or leave the default, which is the same name with `.default` appended. The default services in turn use the service discovery mechanism to provide the best available implementation. You can specify a class for each of the default services to use instead of discovery.
53+
54+
If you need to customize the service with decorators, e.g. to add authentication headers, decorate the default service and configure the main alias to your decorating service name.
55+
56+
```yaml
57+
http_client:
58+
main_alias:
59+
client: php_http.client.default
60+
message_factory: php_http.message_factory.default
61+
uri_factory: php_http.uri_factory.default
62+
classes:
63+
client: ~ # uses discovery if not specified
64+
message_factory: ~
65+
uri_factory: ~
66+
```
67+
68+
### Use for reusable bundles
69+
70+
Rather than code against specific HTTP clients, you want to use the php-http client interface. To avoid building your own infrastructure to define services for the client, simply `require: php-http/http-client-bundle` in your bundles `composer.json`. You SHOULD provide configuration for each of your services that needs an HTTP client to specify the service to use, defaulting to `php_http.client`. This way, the default case needs no additional configuration for your users.
71+
72+
The only steps they need is `require` one of the adapter implementations in their projects `composer.json` and instantiating the HttpClientBundle in their kernel.
2673

2774
## Testing
2875

Resources/config/discovery.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="http_client.client.default"
8+
class="Http\Adapter\HttpAdapter">
9+
<factory class="Http\Discovery\HttpAdapterDiscovery" method="find"/>
10+
</service>
11+
<service id="http_client.message_factory.default"
12+
class="Http\Message\MessageFactory">
13+
<factory class="Http\Discovery\MessageFactoryDiscovery" method="find"/>
14+
</service>
15+
<service id="http_client.uri_factory.default"
16+
class="Http\Message\UriFactory">
17+
<factory class="Http\Discovery\UriFactoryDiscovery" method="find"/>
18+
</service>
19+
20+
</services>
21+
</container>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$container->loadFromExtension('http_client', array());
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services">
3+
4+
<config xmlns="http://example.org/schema/dic/http_client" />
5+
6+
</container>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http_client:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$container->loadFromExtension('http_client', array(
4+
'main_alias' => array(
5+
'client' => 'my_client',
6+
'message_factory' => 'my_message_factory',
7+
'uri_factory' => 'my_uri_factory',
8+
),
9+
'classes' => array(
10+
'client' => 'Http\Adapter\Guzzle6HttpAdapter',
11+
'message_factory' => 'Http\Discovery\MessageFactory\GuzzleFactory',
12+
'uri_factory' => 'Http\Discovery\UriFactory\GuzzleFactory',
13+
),
14+
));
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services">
3+
4+
<config xmlns="http://example.org/schema/dic/http_client">
5+
<main-alias>
6+
<client>my_client</client>
7+
<message-factory>my_message_factory</message-factory>
8+
<uri-factory>my_uri_factory</uri-factory>
9+
</main-alias>
10+
<classes>
11+
<client>Http\Adapter\Guzzle6HttpAdapter</client>
12+
<message-factory>Http\Discovery\MessageFactory\GuzzleFactory</message-factory>
13+
<uri-factory>Http\Discovery\UriFactory\GuzzleFactory</uri-factory>
14+
15+
</classes>
16+
</config>
17+
18+
</container>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
http_client:
2+
main_alias:
3+
client: my_client
4+
message_factory: my_message_factory
5+
uri_factory: my_uri_factory
6+
classes:
7+
client: Http\Adapter\Guzzle6HttpAdapter
8+
message_factory: Http\Discovery\MessageFactory\GuzzleFactory
9+
uri_factory: Http\Discovery\UriFactory\GuzzleFactory
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
http_client:
2+
classes:
3+
client: Nonexisting\Class

0 commit comments

Comments
 (0)