Skip to content

Commit

Permalink
Merge pull request #12 from Mapudo/MAP-2146
Browse files Browse the repository at this point in the history
MAP-2146 Add `__invoke()` support for middlewares
  • Loading branch information
cjost1988 authored Oct 10, 2017
2 parents 295cf93 + 2f55f02 commit b1d8b9b
Show file tree
Hide file tree
Showing 28 changed files with 121 additions and 15 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ All Notable changes to `mapudo/guzzle-bundle` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

Note: This is WIP and will be edited when the other issues for the 1.1.0 milestone are finished as well.
## [2.0.0] - 2017-09-28
### Changed
- **[BC break]** - Annotated `auth` in client `request_options` can also be a string, due to [guzzle/oauth-subscriber](https://github.com/guzzle/oauth-subscriber)
- When registering middleware `method` is not required any more, as middleware can work with `__invoke` (e.g. [guzzle/oauth-subscriber](https://github.com/guzzle/oauth-subscriber))

## [1.1.0] - 2017-03-10
### Changed
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,14 @@ or just do what you want.
This bundle comes with two middleware services already implemented. One
to dispatch events and one to log with given handlers.

#### Add your own Middleware
#### Add your own Middleware
The bundle supports registering Middlewares by using `__invoke()` or creating a custom
`method`.

The CompilerPass searches for services which are tagged with
`guzzle.middleware`. A tag `method` is also required to define which
method should be executed. You also need to add the tag `client` with
the name of the client. However, if you want to create a middleware for
`guzzle.middleware`. A tag `method` is optional to define which
method should be executed if you don't use `__invoke()` in your middleware.
You need to add the tag `client` with the name of the client. However, if you want to create a middleware for
all clients you can omit the tag.

YAML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testSingleClientConfigWithOptions(array $config)
if (!isset($config['guzzle']['clients']['test_client']['request_options']['auth'])) {
// Since we defined how the content of the "auth" option looks like in the configuration
// by default an empty array is added
$config['guzzle']['clients']['test_client']['request_options']['auth'] = [];
$config['guzzle']['clients']['test_client']['request_options']['auth'] = null;
}

$this->assertEquals($config['guzzle'], $processedConfig);
Expand All @@ -53,6 +53,10 @@ public function testSingleClientConfigWithOptions(array $config)
*/
public function dataProviderSingleClientConfigWithOptions(): array
{
return [[Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/sample_config.yml'))]];
return [
[Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/sample_config.yml'))],
[Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/sample_config_auth_string.yml'))],
[Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/sample_config_auth_array.yml'))],
];
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions Tests/Resources/config/sample_config_auth_array.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file shows a sample configuration made in Symfony's config.yml
guzzle:
clients:
test_client:
base_uri: 'https://example.com/path'
headers:
Accept: 'application/json'
Accept-Language: 'de'
X-Auth: 'token'
request_options:
allow_redirects: true
auth: ['username', 'password']
cert:
- '/path/to/cert.pem'
- 'password'
connect_timeout: 10.5
decode_content: 'gzip'
delay: 1
expect: 1048576
force_ip_resolve: 'v6'
http_errors: true
proxy:
http: 'httpProxy'
https: 'httpsProxy'
no: 'anything_else'
query:
foo: bar
read_timeout: 23
ssl_key:
- '/path/to/cert.pem'
- 'password'
stream: true
synchronous: false
verify: true
timeout: 5
version: 1.1
36 changes: 36 additions & 0 deletions Tests/Resources/config/sample_config_auth_string.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file shows a sample configuration made in Symfony's config.yml
guzzle:
clients:
test_client:
base_uri: 'https://example.com/path'
headers:
Accept: 'application/json'
Accept-Language: 'de'
X-Auth: 'token'
request_options:
allow_redirects: true
auth: "oauth"
cert:
- '/path/to/cert.pem'
- 'password'
connect_timeout: 10.5
decode_content: 'gzip'
delay: 1
expect: 1048576
force_ip_resolve: 'v6'
http_errors: true
proxy:
http: 'httpProxy'
https: 'httpsProxy'
no: 'anything_else'
query:
foo: bar
read_timeout: 23
ssl_key:
- '/path/to/cert.pem'
- 'password'
stream: true
synchronous: false
verify: true
timeout: 5
version: 1.1
File renamed without changes.
File renamed without changes.
22 changes: 21 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
{
"name": "mapudo/guzzle-bundle",
"license": "MIT",
"description": "The Mapudo GuzzleBundle allows integrating Guzzle into Symfony projects",
"type": "symfony-bundle",
"license": "MIT",
"authors": [
{
"name": "Theo Tzaferis",
"email": "[email protected]",
"homepage": "http://www.mapudo.com"
},
{
"name": "Community contributions",
"homepage": "https://github.com/Mapudo/guzzle-bundle/contributors"
}
],
"support": {
"issues": "https://github.com/Mapudo/guzzle-bundle/issues",
"source": "https://github.com/Mapudo/guzzle-bundle",
"docs": "https://github.com/Mapudo/guzzle-bundle/blob/master/README.md"
},
"require": {
"php": "^7.0",
"guzzlehttp/guzzle": "~6.0",
Expand All @@ -18,6 +35,9 @@
"phpunit/phpunit": "~5.5",
"symfony/config": "2.3|~3.0"
},
"scripts": {
"test": "phpunit"
},
"autoload": {
"psr-4": { "Mapudo\\Bundle\\GuzzleBundle\\": "src/" },
"exclude-from-classmap": [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
failOnRisky="true"
failOnWarning="true">
<php>
<ini name="error_reporting" value="-1" />
</php>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public function getHandlerDefinition(

foreach ($middleware as $id => $tags) {
$attributes = reset($tags);
$middlewareExpression = new Expression(sprintf('service("%s").%s()', $id, $attributes['method']));

if (!empty($attributes['method'])) {
$middlewareExpression = new Expression(sprintf('service("%s").%s()', $id, $attributes['method']));
} else {
$middlewareExpression = new Expression(sprintf('service("%s")', $id));
}
$handler->addMethodCall('push', [$middlewareExpression]);
}

Expand Down
4 changes: 1 addition & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ private function createClientsNode()
->arrayNode('request_options')
->children()
->variableNode('allow_redirects')->end()
->arrayNode('auth')
->prototype('scalar')->end()
->end()
->variableNode('auth')->defaultNull()->end()
->variableNode('cert')->end()
->floatNode('connect_timeout')->end()
->scalarNode('decode_content')->end()
Expand Down

0 comments on commit b1d8b9b

Please sign in to comment.