<?php
namespace Obs\S3;
use Obs\Log\S3Log;
use Obs\Common\SdkCurlFactory;
use Obs\Common\SdkStreamHandler;
use Obs\Common\Model;
use Monolog\Logger;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\Handler\Proxy;
use GuzzleHttp\Promise\Promise;
define('DEBUG', Logger::DEBUG);
define('INFO', Logger::INFO);
define('NOTICE', Logger::NOTICE);
define('WARNING', Logger::WARNING);
define('WARN', Logger::WARNING);
define('ERROR', Logger::ERROR);
define('CRITICAL', Logger::CRITICAL);
define('ALERT', Logger::ALERT);
define('EMERGENCY', Logger::EMERGENCY);
class ObsClient
{
const SDK_VERSION = '2.1.0';
const DEFAULT_ENDPOINT = 'https://obs.myhwclouds.com:443';
use SendRequestTrait;
use GetResponseTrait;
private $factorys;
private function __construct(){
$this->factorys = [];
}
public function __destruct(){
$this-> close();
}
private static function default_user_agent()
{
static $defaultAgent = '';
if (!$defaultAgent) {
$defaultAgent = 'ObsSdk/' . self::SDK_VERSION;
if (extension_loaded('curl') && function_exists('curl_version')) {
$defaultAgent .= ' curl/' . \curl_version()['version'];
}
$defaultAgent .= ' PHP/' . PHP_VERSION;
}
return $defaultAgent;
}
public static function factory(array $config = [])
{
$obsclient = new ObsClient();
$obsclient-> ak = strval($config['key']);
$obsclient-> sk = strval($config['secret']);
$obsclient-> endpoint = $config['endpoint'] ? strval($config['endpoint']) : self::DEFAULT_ENDPOINT;
while($obsclient-> endpoint[strlen($obsclient-> endpoint)-1] === '/'){
$obsclient-> endpoint = substr($obsclient-> endpoint, 0, strlen($obsclient-> endpoint)-1);
}
if(strpos($obsclient-> endpoint, 'http') !== 0){
$obsclient-> endpoint = 'https://' . $obsclient-> endpoint;
}
if(isset($config['signature'])){
$obsclient-> signature = $config['signature'];
}
if(isset($config['path_style'])){
$obsclient-> pathStyle = $config['path_style'];
}
if(isset($config['region'])){
$obsclient-> region = strval($config['region']);
}
if(isset($config['ssl_verify'])){
$obsclient-> sslVerify = $config['ssl_verify'];
}else if(isset($config['ssl.certificate_authority'])){
$obsclient-> sslVerify = $config['ssl.certificate_authority'];
}
if(isset($config['max_retry_count'])){
$obsclient-> maxRetryCount = intval($config['max_retry_count']);
}
if(isset($config['timeout'])){
$obsclient-> timeout = intval($config['timeout']);
}
if(isset($config['socket_timeout'])){
$obsclient-> socketTimeout = intval($config['socket_timeout']);
}
if(isset($config['connect_timeout'])){
$obsclient-> connectTimeout = intval($config['connect_timeout']);
}
if(isset($config['chunk_size'])){
$obsclient-> chunkSize = intval($config['chunk_size']);
}
if(isset($config['exception_response_mode'])){
$obsclient-> exceptionResponseMode = $config['exception_response_mode'];
}
$handler = self::choose_handler($obsclient);
$obsclient -> httpClient = new Client(
[
'timeout' => 0,
'read_timeout' => $obsclient -> socketTimeout,
'connect_timeout' => $obsclient -> connectTimeout,
'allow_redirects' => false,
'verify' => $obsclient-> sslVerify,
'expect' => false,
'handler' => HandlerStack::create($handler),
'curl' => [
CURLOPT_BUFFERSIZE => $obsclient-> chunkSize
]
]
);
return $obsclient;
}
public function close(){
if($this->factorys){
foreach ($this->factorys as $factory){
$factory->close();
}
}
}
public function initLog(array $logConfig= [])
{
S3Log::initLog($logConfig);
}
private static function choose_handler($obsclient)
{
$handler = null;
if (function_exists('curl_multi_exec') && function_exists('curl_exec')) {
$f1 = new SdkCurlFactory(50);
$f2 = new SdkCurlFactory(3);
$obsclient->factorys[] = $f1;
$obsclient->factorys[] = $f2;
$handler = Proxy::wrapSync(new CurlMultiHandler(['handle_factory' => $f1]), new CurlHandler(['handle_factory' => $f2]));
} elseif (function_exists('curl_exec')) {
$f = new SdkCurlFactory(3);
$obsclient->factorys[] = $f;
$handler = new CurlHandler(['handle_factory' => $f]);
} elseif (function_exists('curl_multi_exec')) {
$f = new SdkCurlFactory(50);
$obsclient->factorys[] = $f;
$handler = new CurlMultiHandler(['handle_factory' => $f1]);
}
if (ini_get('allow_url_fopen')) {
$handler = $handler
? Proxy::wrapStreaming($handler, new SdkStreamHandler())
: new SdkStreamHandler();
} elseif (!$handler) {
throw new \RuntimeException('GuzzleHttp requires cURL, the '
. 'allow_url_fopen ini setting, or a custom HTTP handler.');
}
return $handler;
}
}