Skip to content

Commit

Permalink
#77 Workaround/Hotfix: Cannot set headers in config (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Preusner authored Dec 11, 2016
1 parent cd72a5c commit 2f041eb
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 280 deletions.
7 changes: 6 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,23 @@ private function createClientsNode()
->children()
->scalarNode('base_url')->defaultValue(null)->end()

// @deprecated
->arrayNode('headers')
->prototype('scalar')
->end()
->end()

->arrayNode('options')
->children()
->arrayNode('headers')
->prototype('scalar')
->end()
->end()
->arrayNode('auth')
->prototype('scalar')
->end()
->end()
->arrayNode('query')
->arrayNode('query')
->prototype('scalar')
->end()
->end()
Expand Down
29 changes: 6 additions & 23 deletions DependencyInjection/GuzzleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public function load(array $configs, ContainerBuilder $container)
}
}

// header hotfix/workaround #77
// @deprecated
if (isset($options['headers'])) {
$argument['headers'] = $this->cleanUpHeaders($options['headers']);
}

$client = new Definition('%guzzle.http_client.class%');
$client->addArgument($argument);

Expand All @@ -90,16 +96,11 @@ protected function createHandler(ContainerBuilder $container, $name, array $conf
$log = $this->createLogMiddleware();
$container->setDefinition($logServiceName, $log);

$headerServiceName = sprintf('guzzle_bundle.middleware.request_header.%s', $name);
$requestHeader = $this->createRequestHeaderMiddleware($config['headers']);
$container->setDefinition($headerServiceName, $requestHeader);

// Event Dispatching service
$eventServiceName = sprintf('guzzle_bundle.middleware.event_dispatch.%s', $name);
$eventService = $this->createEventMiddleware($name);
$container->setDefinition($eventServiceName, $eventService);

$headerExpression = new Expression(sprintf("service('%s').attach()", $headerServiceName));
$logExpression = new Expression(sprintf("service('%s').log()", $logServiceName));
// Create the event Dispatch Middleware
$eventExpression = new Expression(sprintf("service('%s').dispatchEvent()", $eventServiceName));
Expand Down Expand Up @@ -136,7 +137,6 @@ protected function createHandler(ContainerBuilder $container, $name, array $conf
}
}

$handler->addMethodCall('push', [$headerExpression]);
$handler->addMethodCall('push', [$logExpression]);
// goes on the end of the stack.
$handler->addMethodCall('unshift', [$eventExpression]);
Expand Down Expand Up @@ -186,23 +186,6 @@ protected function createLogMiddleware()
return $log;
}

/**
* Create Middleware For Request Headers
*
* @since 2015-07
*
* @param array $headers
*
* @return Definition
*/
protected function createRequestHeaderMiddleware(array $headers)
{
$requestHeader = new Definition('%guzzle_bundle.middleware.request_header.class%');
$requestHeader->addArgument($this->cleanUpHeaders($headers));

return $requestHeader;
}

/**
* Create Middleware For dispatching events
*
Expand Down
119 changes: 0 additions & 119 deletions Middleware/RequestHeaderMiddleware.php

This file was deleted.

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ guzzle:
api_payment:
base_url: "http://api.domain.tld"

# custom headers
# custom headers (@deprecated, will be removed in v5; new: set headers in options)
headers:
Accept: "application/json"

Expand All @@ -76,6 +76,10 @@ guzzle:
auth:
- acme # login
- pa55w0rd # password

headers:
Accept: "application/json"

timeout: 30

# plugin settings
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parameter key="guzzle_bundle.data_collector.class">EightPoints\Bundle\GuzzleBundle\DataCollector\HttpDataCollector</parameter>
<parameter key="guzzle_bundle.logger.class">EightPoints\Bundle\GuzzleBundle\Log\Logger</parameter>
<parameter key="guzzle_bundle.middleware.log.class">EightPoints\Bundle\GuzzleBundle\Middleware\LogMiddleware</parameter>
<parameter key="guzzle_bundle.middleware.request_header.class">EightPoints\Bundle\GuzzleBundle\Middleware\RequestHeaderMiddleware</parameter>

<!-- Event MiddleWare Dispatcher class -->
<parameter key="guzzle_bundle.middleware.event_dispatcher.class">EightPoints\Bundle\GuzzleBundle\Middleware\EventDispatchMiddleware</parameter>

Expand Down
3 changes: 3 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function testSingleClientConfigWithOptions()
'user',
'pass'
],
'headers' => [
'Accept' => 'application/json'
],
'query' => [
],
'cert' => 'path/to/cert',
Expand Down
1 change: 0 additions & 1 deletion Tests/DependencyInjection/GuzzleExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function testGuzzleExtension()

// test Services
$this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.log.test_api'));
$this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.request_header.test_api'));
$this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.event_dispatch.test_api'));

// test WSSE Plugin
Expand Down
134 changes: 0 additions & 134 deletions Tests/Middleware/RequestHeaderMiddlewareTest.php

This file was deleted.

0 comments on commit 2f041eb

Please sign in to comment.