/**
  * Default method to execute when credentials are not specified
  *
  * @param Collection $config Config options
  *
  * @return CredentialsInterface
  */
 protected function defaultMissingFunction(Collection $config)
 {
     if ($config->get(Options::KEY) && $config->get(Options::SECRET)) {
         // Credentials were not provided, so create them using keys
         return Credentials::factory($config->getAll());
     }
     // Attempt to get credentials from the EC2 instance profile server
     return new RefreshableInstanceProfileCredentials(new Credentials('', '', '', 1));
 }
 /**
  * Create a Cookie by parsing a Cookie HTTP header
  *
  * @param string $cookieString Cookie HTTP header
  *
  * @return Cookie
  */
 public static function factory($cookieString)
 {
     $data = new Collection();
     if ($cookieString) {
         foreach (explode(';', $cookieString) as $kvp) {
             $parts = explode('=', $kvp, 2);
             $key = urldecode(trim($parts[0]));
             $value = isset($parts[1]) ? trim($parts[1]) : '';
             $data->add($key, urldecode($value));
         }
     }
     return new static($data->getAll());
 }
示例#3
0
 public function testOverwriteWithTraversable()
 {
     $c = new Collection(array('foo' => 1, 'baz' => 2, 'bar' => 3));
     $b = new Collection(array('foo' => 10, 'bar' => 300));
     $c->overwriteWith($b->getIterator());
     $this->assertEquals(array('foo' => 10, 'baz' => 2, 'bar' => 300), $c->getAll());
 }
示例#4
0
 /**
  * @covers Guzzle\Common\Collection::replace
  */
 public function testCanReplaceAllData()
 {
     $this->assertSame($this->coll, $this->coll->replace(array('a' => '123')));
     $this->assertEquals(array('a' => '123'), $this->coll->getAll());
 }
 /**
  * {@inheritdoc}
  */
 public function parseMessage($message)
 {
     if (!$message) {
         return false;
     }
     $headers = new Collection();
     $scheme = $host = $body = $method = $user = $pass = $query = $port = $version = $protocol = '';
     $path = '/';
     // Inspired by https://github.com/kriswallsmith/Buzz/blob/message-interfaces/lib/Buzz/Message/Parser/Parser.php#L16
     $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
     for ($i = 0, $c = count($lines); $i < $c; $i += 2) {
         $line = $lines[$i];
         // If two line breaks were encountered, then this is the body
         if (empty($line)) {
             $body = implode('', array_slice($lines, $i + 2));
             break;
         }
         // Parse message headers
         if (!$method) {
             list($method, $path, $proto) = explode(' ', $line);
             $method = strtoupper($method);
             list($protocol, $version) = explode('/', strtoupper($proto));
             $scheme = 'http';
         } else {
             if (strpos($line, ':')) {
                 list($key, $value) = explode(':', $line, 2);
                 $key = trim($key);
                 // Normalize standard HTTP headers
                 if (in_array(strtolower($key), static::$requestHeaders)) {
                     $key = str_replace(' ', '-', ucwords(str_replace('-', ' ', $key)));
                 }
                 // Headers are case insensitive
                 $headers->add($key, trim($value));
             }
         }
     }
     // Check for the Host header
     if (isset($headers['Host'])) {
         $host = $headers['Host'];
     }
     if (strpos($host, ':')) {
         list($host, $port) = array_map('trim', explode(':', $host));
         if ($port == 443) {
             $scheme = 'https';
         }
     } else {
         $port = '';
     }
     // Check for basic authorization
     $auth = isset($headers['Authorization']) ? $headers['Authorization'] : '';
     if ($auth) {
         list($type, $data) = explode(' ', $auth);
         if (strtolower($type) == 'basic') {
             $data = base64_decode($data);
             list($user, $pass) = explode(':', $data);
         }
     }
     // Check if a query is present
     $qpos = strpos($path, '?');
     if ($qpos) {
         $query = substr($path, $qpos);
         $path = substr($path, 0, $qpos);
     }
     return array('method' => $method, 'protocol' => $protocol, 'protocol_version' => $version, 'parts' => array('scheme' => $scheme, 'host' => $host, 'port' => $port, 'user' => $user, 'pass' => $pass, 'path' => $path, 'query' => $query), 'headers' => $headers->getAll(), 'body' => $body);
 }
示例#6
0
 /**
  * {@inheritdoc}
  */
 public function getCookies()
 {
     $cookieData = new Collection();
     if ($cookies = $this->getHeader('Cookie')) {
         foreach ($cookies as $cookie) {
             $parts = explode('=', $cookie, 2);
             $cookieData->add($parts[0], isset($parts[1]) ? $parts[1] : '');
         }
     }
     return $cookieData->getAll();
 }
 public function testOverridesSettings()
 {
     $c = new Collection(array('foo' => 1, 'baz' => 2, 'bar' => 3));
     $c->overwriteWith(array('foo' => 10, 'bar' => 300));
     $this->assertEquals(array('foo' => 10, 'baz' => 2, 'bar' => 300), $c->getAll());
 }