public function testDetachesUnderlyingStream() { file_put_contents($this->fname, 'foo'); $l = new LazyOpenStream($this->fname, 'r'); $r = $l->detach(); $this->assertInternalType('resource', $r); fclose($r); }
/** * Applies an array of request client options to a the options array. * * This method uses a large switch rather than double-dispatch to save on * high overhead of calling functions in PHP. */ private function applyHandlerOptions(array $request, array &$options) { foreach ($request['client'] as $key => $value) { switch ($key) { // Violating PSR-4 to provide more room. case 'verify': if ($value === false) { unset($options[CURLOPT_CAINFO]); $options[CURLOPT_SSL_VERIFYHOST] = 0; $options[CURLOPT_SSL_VERIFYPEER] = false; continue; } $options[CURLOPT_SSL_VERIFYHOST] = 2; $options[CURLOPT_SSL_VERIFYPEER] = true; if (is_string($value)) { $options[CURLOPT_CAINFO] = $value; if (!file_exists($value)) { throw new \InvalidArgumentException("SSL CA bundle not found: {$value}"); } } break; case 'decode_content': if ($value === false) { continue; } $accept = Core::firstHeader($request, 'Accept-Encoding'); if ($accept) { $options[CURLOPT_ENCODING] = $accept; } else { $options[CURLOPT_ENCODING] = ''; // Don't let curl send the header over the wire $options[CURLOPT_HTTPHEADER][] = 'Accept-Encoding:'; } break; case 'save_to': if (is_string($value)) { if (!is_dir(dirname($value))) { throw new \RuntimeException(sprintf('Directory %s does not exist for save_to value of %s', dirname($value), $value)); } $value = new LazyOpenStream($value, 'w+'); } if ($value instanceof StreamInterface) { $options[CURLOPT_WRITEFUNCTION] = function ($ch, $write) use($value) { return $value->write($write); }; } elseif (is_resource($value)) { $options[CURLOPT_FILE] = $value; } else { throw new \InvalidArgumentException('save_to must be a ' . 'GuzzleHttp\\Stream\\StreamInterface or resource'); } break; case 'timeout': if (defined('CURLOPT_TIMEOUT_MS')) { $options[CURLOPT_TIMEOUT_MS] = $value * 1000; } else { $options[CURLOPT_TIMEOUT] = $value; } break; case 'connect_timeout': if (defined('CURLOPT_CONNECTTIMEOUT_MS')) { $options[CURLOPT_CONNECTTIMEOUT_MS] = $value * 1000; } else { $options[CURLOPT_CONNECTTIMEOUT] = $value; } break; case 'proxy': if (!is_array($value)) { $options[CURLOPT_PROXY] = $value; } elseif (isset($request['scheme'])) { $scheme = $request['scheme']; if (isset($value[$scheme])) { $options[CURLOPT_PROXY] = $value[$scheme]; } } break; case 'cert': if (is_array($value)) { $options[CURLOPT_SSLCERTPASSWD] = $value[1]; $value = $value[0]; } if (!file_exists($value)) { throw new \InvalidArgumentException("SSL certificate not found: {$value}"); } $options[CURLOPT_SSLCERT] = $value; break; case 'ssl_key': if (is_array($value)) { $options[CURLOPT_SSLKEYPASSWD] = $value[1]; $value = $value[0]; } if (!file_exists($value)) { throw new \InvalidArgumentException("SSL private key not found: {$value}"); } $options[CURLOPT_SSLKEY] = $value; break; case 'progress': if (!is_callable($value)) { throw new \InvalidArgumentException('progress client option must be callable'); } $options[CURLOPT_NOPROGRESS] = false; $options[CURLOPT_PROGRESSFUNCTION] = function () use($value) { $args = func_get_args(); // PHP 5.5 pushed the handle onto the start of the args if (is_resource($args[0])) { array_shift($args); } call_user_func_array($value, $args); }; break; case 'debug': if ($value) { $options[CURLOPT_STDERR] = Core::getDebugResource($value); $options[CURLOPT_VERBOSE] = true; } break; } } }