Skip to content

Commit

Permalink
fixed #5: UndefinedMethodException: getEventDispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Preusner committed Nov 6, 2014
1 parent 12686ea commit cc16342
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
15 changes: 7 additions & 8 deletions DependencyInjection/GuzzleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function load(array $configs, ContainerBuilder $container) {
* Set up HTTP headers
*
* @author Florian Preusner
* @version 1.0
* @version 2.0
* @since 2013-10
*
* @param array $headers
Expand All @@ -70,16 +70,16 @@ public function load(array $configs, ContainerBuilder $container) {
protected function setUpHeaders(array $headers, ContainerBuilder $container) {

$container->setParameter('guzzle.plugin.header.headers', $headers);
$container->getDefinition('guzzle.client')
->addMethodCall('getEventDispatcher')
->addMethodCall('addSubscriber', array($container->getDefinition('guzzle.plugin.header')));

$container->getDefinition('guzzle.emitter')
->addMethodCall('attach', array($container->getDefinition('guzzle.plugin.header')));
} // end: setUpHeaders

/**
* Set up WSSE settings
*
* @author Florian Preusner
* @version 1.0
* @version 2.0
* @since 2013-10
*
* @param array $config
Expand All @@ -94,9 +94,8 @@ protected function setUpWsse(array $config, ContainerBuilder $container) {
$container->setParameter('guzzle.plugin.wsse.username', $config['username']);
$container->setParameter('guzzle.plugin.wsse.password', $config['password']);

$container->getDefinition('guzzle.client')
->addMethodCall('getEventDispatcher')
->addMethodCall('addSubscriber', array($container->getDefinition('guzzle.plugin.wsse')));
$container->getDefinition('guzzle.emitter')
->addMethodCall('attach', array($container->getDefinition('guzzle.plugin.wsse')));
}
} // end: setUpWsse

Expand Down
28 changes: 14 additions & 14 deletions EventListener/HeaderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace EightPoints\Bundle\GuzzleBundle\EventListener;

use Guzzle\Common\Event,
Symfony\Component\EventDispatcher\EventSubscriberInterface;
use GuzzleHttp\Event\BeforeEvent,
GuzzleHttp\Event\SubscriberInterface;

/**
* Adds headers to request
*
* @package EightPoints\Bundle\GuzzleBundle\EventListener
* @author Florian Preusner
*
* @version 1.0
* @version 2.0
* @since 2013-10
*/
class HeaderSubscriber implements EventSubscriberInterface {
class HeaderSubscriber implements SubscriberInterface {

/**
* @var array $headers
Expand Down Expand Up @@ -105,33 +105,33 @@ public function getHeader($key) {
* {@inheritdoc}
*
* @author Florian Preusner
* @version 1.0
* @version 2.0
* @since 2013-10
*/
public static function getSubscribedEvents() {
public function getEvents() {

return array('client.create_request' => 'onRequestCreate');
} // end: getSubscribedEvents
return ['before' => ['onBefore']];
} // end: getEvents

/**
* Add given headers to request
*
* @author Florian Preusner
* @version 1.0
* @version 2.0
* @since 2013-10
*
* @param Event $event
* @param BeforeEvent $event
*
* @return void
*/
public function onRequestCreate(Event $event) {
public function onBefore(BeforeEvent $event) {

$request = $event['request'];
$request = $event->getRequest();

// make sure to keep headers that have been already set
foreach($this->headers as $key => $value) {
foreach($this->getHeaders() as $key => $value) {

$request->addHeader($key, $value);
}
} // end: onRequestCreate
} // end: onBefore
} // end: HeaderSubscriber
5 changes: 4 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@

<parameters>
<parameter key="guzzle.http_client.class">GuzzleHttp\Client</parameter>
<parameter key="guzzle.emitter.class">GuzzleHttp\Event\Emitter</parameter>
<parameter key="guzzle.plugin.header.class">EightPoints\Bundle\GuzzleBundle\EventListener\HeaderSubscriber</parameter>
<parameter key="guzzle.plugin.wsse.class">EightPoints\Guzzle\Plugin\WsseAuthPlugin</parameter>

<parameter key="guzzle.plugin.header.headers" type="collection" ></parameter>
<parameter key="guzzle.plugin.header.headers" type="collection"></parameter>
<parameter key="guzzle.plugin.wsse.username"></parameter>
<parameter key="guzzle.plugin.wsse.password"></parameter>
</parameters>

<services>
<service id="guzzle.emitter" class="%guzzle.emitter.class%"></service>
<service id="guzzle.client" class="%guzzle.http_client.class%">
<argument type="collection">
<argument key="base_url" type="string" id="guzzle.base_url">%guzzle.base_url%</argument>
<argument key="emitter" type="service" id="guzzle.emitter"></argument>
</argument>
</service>
<service id="guzzle.plugin.header" class="%guzzle.plugin.header.class%">
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

"require": {
"php": ">=5.4.0",
"guzzlehttp/guzzle": "5.0.*",
"eightpoints/guzzle-wsse-plugin": "1.*"
"guzzlehttp/guzzle": "~5.0",
"eightpoints/guzzle-wsse-plugin": "~2.0"
},

"target-dir": "EightPoints/Bundle/GuzzleBundle",
Expand Down

0 comments on commit cc16342

Please sign in to comment.