Skip to content

Commit 2a388f8

Browse files
committed
Merge pull request #8 from dbu/initial-bundle
building the initial bundle
2 parents 3c92180 + 702eda2 commit 2a388f8

21 files changed

+562
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
# we diverge from the yml 2 spaces convention of php-http in favor of the symfony code style.

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

HttplugBundle.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;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
/**
8+
* @author David Buchmann <[email protected]>
9+
*/
10+
class HttplugBundle extends Bundle
11+
{
12+
}

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,54 @@ Via Composer
2020
$ composer require php-http/httplug-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\HttplugBundle\HttplugBundle(),
34+
);
35+
}
36+
```
2337

2438
## Usage
2539

40+
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 Httplug interface.
41+
42+
### Use in applications
43+
44+
This bundle provides 3 services:
45+
46+
* `httplug.client` a service that provides the `Http\Client\HttpClient`
47+
* `httplug.message_factory` a service that provides the `Http\Message\MessageFactory`
48+
* `httplug.uri_factory` a service that provides the `Http\Message\UriFactory`
49+
50+
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, as long as those classes can be instantiated without arguments.
51+
52+
If you need a more custom setup, define the services in your application configuration and specify your service in the `main_alias` section. For example, to add authentication headers, you could define a service that decorates the service `httplug.client.default` with a plugin that injects the authentication headers into the request and configure `httplug.main_alias.client` to the name of your service.
53+
54+
```yaml
55+
httplug:
56+
main_alias:
57+
client: httplug.client.default
58+
message_factory: httplug.message_factory.default
59+
uri_factory: httplug.uri_factory.default
60+
classes:
61+
client: ~ # uses discovery if not specified
62+
message_factory: ~
63+
uri_factory: ~
64+
```
65+
66+
### Use for reusable bundles
67+
68+
Rather than code against specific HTTP clients, you want to use the Httplug `Client` interface. To avoid building your own infrastructure to define services for the client, simply `require: php-http/httplug-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 `httplug.client`. This way, the default case needs no additional configuration for your users.
69+
70+
The only steps they need is `require` one of the adapter implementations in their projects `composer.json` and instantiating the HttplugBundle in their kernel.
2671

2772
## Testing
2873

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="httplug.client.default"
8+
class="Http\Client\HttpClient">
9+
<factory class="Http\Discovery\HttpClientDiscovery" method="find"/>
10+
</service>
11+
<service id="httplug.message_factory.default"
12+
class="Http\Message\MessageFactory">
13+
<factory class="Http\Discovery\MessageFactoryDiscovery" method="find"/>
14+
</service>
15+
<service id="httplug.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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Tests\Functional;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
class ServiceInstantiationTest extends WebTestCase
8+
{
9+
public static function setUpBeforeClass()
10+
{
11+
static::bootKernel();
12+
}
13+
14+
public function testHttpClient()
15+
{
16+
$container = static::$kernel->getContainer();
17+
$this->assertTrue($container->has('httplug.client'));
18+
$client = $container->get('httplug.client');
19+
$this->assertInstanceOf('Http\Client\HttpClient', $client);
20+
}
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$container->loadFromExtension('httplug', 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/httplug" />
5+
6+
</container>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
httplug:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$container->loadFromExtension('httplug', 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/httplug">
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+
httplug:
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+
httplug:
2+
classes:
3+
client: Nonexisting\Class

Tests/Resources/app/AppKernel.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Symfony\Component\Config\Loader\LoaderInterface;
4+
use Symfony\Component\HttpKernel\Kernel;
5+
6+
class AppKernel extends Kernel
7+
{
8+
/**
9+
* {@inheritdoc}
10+
*/
11+
public function registerBundles()
12+
{
13+
return array(
14+
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
15+
new \Http\HttplugBundle\HttplugBundle(),
16+
);
17+
}
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function registerContainerConfiguration(LoaderInterface $loader)
23+
{
24+
$loader->load(__DIR__.'/config/config.yml');
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function getCacheDir()
31+
{
32+
return sys_get_temp_dir().'/httplug-bundle/cache';
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function getLogDir()
39+
{
40+
return sys_get_temp_dir().'/httplug-bundle/logs';
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
protected function getContainerBaseClass()
47+
{
48+
return '\PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer';
49+
}
50+
}

Tests/Resources/app/config/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework:
2+
secret: php-http
3+
test: ~
4+
5+
httplug: ~

0 commit comments

Comments
 (0)