1: <?php
2:
3: namespace Guzzle\Plugin\CurlAuth;
4:
5: use Guzzle\Common\Event;
6: use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7:
8: /**
9: * Adds specified curl auth to all requests sent from a client. Defaults to CURLAUTH_BASIC if none supplied.
10: */
11: class CurlAuthPlugin implements EventSubscriberInterface
12: {
13: private $username;
14: private $password;
15: private $scheme;
16:
17: /**
18: * Constructor
19: *
20: * @param string $username HTTP basic auth username
21: * @param string $password Password
22: * @param int $scheme Curl auth scheme
23: */
24: public function __construct($username, $password, $scheme=CURLAUTH_BASIC)
25: {
26: $this->username = $username;
27: $this->password = $password;
28: $this->scheme = $scheme;
29: }
30:
31: /**
32: * {@inheritdoc}
33: */
34: public static function getSubscribedEvents()
35: {
36: return array('client.create_request' => array('onRequestCreate', 255));
37: }
38:
39: /**
40: * Add basic auth
41: *
42: * @param Event $event
43: */
44: public function onRequestCreate(Event $event)
45: {
46: $event['request']->setAuth($this->username, $this->password, $this->scheme);
47: }
48: }
49: