Skip to content

building the initial bundle #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

# we diverge from the yml 2 spaces convention of php-http in favor of the symfony code style.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ php:
env:
global:
- TEST_COMMAND="composer test"
- SYMFONY_VERSION=2.7.*

matrix:
allow_failures:
Expand All @@ -21,11 +22,16 @@ matrix:
- COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- COVERAGE=true
- TEST_COMMAND="composer test-ci"
- php: 5.6
env: SYMFONY_VERSION=2.8.*
- php: 5.6
env: SYMFONY_VERSION=3.0.*

before_install:
- travis_retry composer self-update

install:
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction

script:
Expand Down
73 changes: 73 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Http\HttplugBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

/**
* This class contains the configuration information for the bundle
*
* This information is solely responsible for how the different configuration
* sections are normalized, and merged.
*
* @author David Buchmann <[email protected]>
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('httplug');

$rootNode
->validate()
->ifTrue(function ($v) {
return !empty($v['classes']['client'])
|| !empty($v['classes']['message_factory'])
|| !empty($v['classes']['uri_factory'])
;
})
->then(function ($v) {
foreach ($v['classes'] as $key => $class) {
if (null !== $class && !class_exists($class)) {
throw new InvalidConfigurationException(sprintf(
'Class %s specified for httplug.classes.%s does not exist.',
$class,
$key
));
}
}

return $v;
})
->end()
->children()
->arrayNode('main_alias')
->addDefaultsIfNotSet()
->info('Configure which service the main alias point to.')
->children()
->scalarNode('client')->defaultValue('httplug.client.default')->end()
->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end()
->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end()
->end()
->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->info('Overwrite a service class instead of using the discovery mechanism.')
->children()
->scalarNode('client')->defaultNull()->end()
->scalarNode('message_factory')->defaultNull()->end()
->scalarNode('uri_factory')->defaultNull()->end()
->end()
->end()
->end()
;

return $treeBuilder;
}
}
37 changes: 37 additions & 0 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Http\HttplugBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* @author David Buchmann <[email protected]>
*/
class HttplugExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

$loader->load('discovery.xml');
foreach ($config['classes'] as $service => $class) {
if (!empty($class)) {
$container->removeDefinition(sprintf('httplug.%s.default', $service));
$container->register(sprintf('httplug.%s.default', $service), $class);
}
}

foreach ($config['main_alias'] as $type => $id) {
$container->setAlias(sprintf('httplug.%s', $type), $id);
}
}
}
12 changes: 12 additions & 0 deletions HttplugBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Http\HttplugBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* @author David Buchmann <[email protected]>
*/
class HttplugBundle extends Bundle
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what is the PSR2 standard here: empty line between curly braces necessary or not? @hannesvdvreken you corrected me earlier.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No empty line.

}
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,54 @@ Via Composer
$ composer require php-http/httplug-bundle
```

Enable the bundle in your kernel:

``` php
<?php
// app/AppKernel.php

public function registerBundles()
{
$bundles = array(
// ...
new Http\HttplugBundle\HttplugBundle(),
);
}
```

## Usage

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.

### Use in applications

This bundle provides 3 services:

* `httplug.client` a service that provides the `Http\Client\HttpClient`
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depending on the decision on php-http/httplug#68 (comment) this might need to be changed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and fine as well.

* `httplug.message_factory` a service that provides the `Http\Message\MessageFactory`
* `httplug.uri_factory` a service that provides the `Http\Message\UriFactory`

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.

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.

```yaml
httplug:
main_alias:
client: httplug.client.default
message_factory: httplug.message_factory.default
uri_factory: httplug.uri_factory.default
classes:
client: ~ # uses discovery if not specified
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, maybe we can have a class for each adapter which can inject/send a NodeConfiguration ? So we would now how to configure each adapter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be done in service configuration by the user.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the user can specify a class for the client / adapter and we just
instantiate that class without arguments. in a first step, i would
expect the user to define a service for their adapter if they need
specific configuration on their client/adapter.

the next step will be to allow to configure plugins and wrapper clients.
i want to avoid configuring the actual guzzle client, but if the
guzzle adapter has specific configuration, we can add that at some
point. not sure how much specific configuration the adapters will have
however.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how much specific configuration the adapters will have however.

Shouldn't be any at all.

Is it possible to specify adapter classes as services, so we can configure the underlying client?

For example create a service for Guzzle6Adapter where a Guzzle Client instance is being injected which can be cpnfigured as a service as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to specify adapter classes as services

that seems too complicated to me. either use the factory or simply define a service for the underlying client and another one for the adapter, then configure this bundle to use your service.

i updated the readme to better explain the idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simply define a service for the underlying client and another one for the adapter, then configure this bundle to use your service.

That's exactly what I want.

message_factory: ~
uri_factory: ~
```

### Use for reusable bundles

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.

The only steps they need is `require` one of the adapter implementations in their projects `composer.json` and instantiating the HttplugBundle in their kernel.

## Testing

Expand Down
21 changes: 21 additions & 0 deletions Resources/config/discovery.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="httplug.client.default"
class="Http\Client\HttpClient">
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depending on the decision on php-http/httplug#68 (comment) this might need to be changed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, this is fine.

<factory class="Http\Discovery\HttpClientDiscovery" method="find"/>
</service>
<service id="httplug.message_factory.default"
class="Http\Message\MessageFactory">
<factory class="Http\Discovery\MessageFactoryDiscovery" method="find"/>
</service>
<service id="httplug.uri_factory.default"
class="Http\Message\UriFactory">
<factory class="Http\Discovery\UriFactoryDiscovery" method="find"/>
</service>

</services>
</container>
21 changes: 21 additions & 0 deletions Tests/Functional/ServiceInstantiationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Http\HttplugBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ServiceInstantiationTest extends WebTestCase
{
public static function setUpBeforeClass()
{
static::bootKernel();
}

public function testHttpClient()
{
$container = static::$kernel->getContainer();
$this->assertTrue($container->has('httplug.client'));
$client = $container->get('httplug.client');
$this->assertInstanceOf('Http\Client\HttpClient', $client);
}
}
3 changes: 3 additions & 0 deletions Tests/Resources/Fixtures/config/empty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$container->loadFromExtension('httplug', array());
6 changes: 6 additions & 0 deletions Tests/Resources/Fixtures/config/empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services">

<config xmlns="http://example.org/schema/dic/httplug" />

</container>
1 change: 1 addition & 0 deletions Tests/Resources/Fixtures/config/empty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
httplug:
14 changes: 14 additions & 0 deletions Tests/Resources/Fixtures/config/full.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

$container->loadFromExtension('httplug', array(
'main_alias' => array(
'client' => 'my_client',
'message_factory' => 'my_message_factory',
'uri_factory' => 'my_uri_factory',
),
'classes' => array(
'client' => 'Http\Adapter\Guzzle6HttpAdapter',
'message_factory' => 'Http\Discovery\MessageFactory\GuzzleFactory',
'uri_factory' => 'Http\Discovery\UriFactory\GuzzleFactory',
),
));
18 changes: 18 additions & 0 deletions Tests/Resources/Fixtures/config/full.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services">

<config xmlns="http://example.org/schema/dic/httplug">
<main-alias>
<client>my_client</client>
<message-factory>my_message_factory</message-factory>
<uri-factory>my_uri_factory</uri-factory>
</main-alias>
<classes>
<client>Http\Adapter\Guzzle6HttpAdapter</client>
<message-factory>Http\Discovery\MessageFactory\GuzzleFactory</message-factory>
<uri-factory>Http\Discovery\UriFactory\GuzzleFactory</uri-factory>

</classes>
</config>

</container>
9 changes: 9 additions & 0 deletions Tests/Resources/Fixtures/config/full.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
httplug:
main_alias:
client: my_client
message_factory: my_message_factory
uri_factory: my_uri_factory
classes:
client: Http\Adapter\Guzzle6HttpAdapter
message_factory: Http\Discovery\MessageFactory\GuzzleFactory
uri_factory: Http\Discovery\UriFactory\GuzzleFactory
3 changes: 3 additions & 0 deletions Tests/Resources/Fixtures/config/invalid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
httplug:
classes:
client: Nonexisting\Class
50 changes: 50 additions & 0 deletions Tests/Resources/app/AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel
{
/**
* {@inheritdoc}
*/
public function registerBundles()
{
return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Http\HttplugBundle\HttplugBundle(),
);
}

/**
* {@inheritdoc}
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config.yml');
}

/**
* {@inheritdoc}
*/
public function getCacheDir()
{
return sys_get_temp_dir().'/httplug-bundle/cache';
}

/**
* {@inheritdoc}
*/
public function getLogDir()
{
return sys_get_temp_dir().'/httplug-bundle/logs';
}

/**
* {@inheritdoc}
*/
protected function getContainerBaseClass()
{
return '\PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer';
}
}
5 changes: 5 additions & 0 deletions Tests/Resources/app/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
framework:
secret: php-http
test: ~

httplug: ~
Loading