-
Notifications
You must be signed in to change notification settings - Fork 50
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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; | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
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 | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be done in service configuration by the user. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 the next step will be to allow to configure plugins and wrapper clients. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 | ||
|
||
|
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"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> |
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('httplug', array()); |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
httplug: |
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', | ||
), | ||
)); |
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> |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
httplug: | ||
classes: | ||
client: Nonexisting\Class |
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'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
framework: | ||
secret: php-http | ||
test: ~ | ||
|
||
httplug: ~ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No empty line.