@@ -10,108 +10,108 @@ |
||
10 | 10 | */ |
11 | 11 | class StreamClient extends AbstractClient |
12 | 12 | { |
13 | - /** @var array|NULL */ |
|
14 | - private $sslOptions; |
|
15 | - |
|
16 | - |
|
17 | - /** |
|
18 | - * @param array SSL context options {@link http://php.net/manual/en/context.ssl.php} |
|
19 | - */ |
|
20 | - public function __construct(array $sslOptions = NULL) |
|
21 | - { |
|
22 | - $this->sslOptions = $sslOptions; |
|
23 | - } |
|
24 | - |
|
25 | - |
|
26 | - protected function setupRequest(Request $request) |
|
27 | - { |
|
28 | - parent::setupRequest($request); |
|
29 | - $request->setHeader('Connection', 'close'); |
|
30 | - } |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * @return Response |
|
35 | - * |
|
36 | - * @throws BadResponseException |
|
37 | - */ |
|
38 | - protected function process(Request $request) |
|
39 | - { |
|
40 | - $headerStr = []; |
|
41 | - foreach ($request->getHeaders() as $name => $value) { |
|
42 | - foreach ((array) $value as $v) { |
|
43 | - $headerStr[] = "$name: $v"; |
|
44 | - } |
|
45 | - } |
|
46 | - |
|
47 | - $options = [ |
|
48 | - 'http' => [ |
|
49 | - 'method' => $request->getMethod(), |
|
50 | - 'header' => implode("\r\n", $headerStr) . "\r\n", |
|
51 | - 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
52 | - 'protocol_version' => 1.1, |
|
53 | - 'ignore_errors' => TRUE, |
|
54 | - ], |
|
55 | - 'ssl' => [ |
|
56 | - 'verify_peer' => TRUE, |
|
57 | - 'cafile' => realpath(__DIR__ . '/../../ca-chain.crt'), |
|
58 | - 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
59 | - ], |
|
60 | - ]; |
|
61 | - |
|
62 | - if (($content = $request->getContent()) !== NULL) { |
|
63 | - $options['http']['content'] = $content; |
|
64 | - } |
|
65 | - |
|
66 | - if ($this->sslOptions) { |
|
67 | - $options['ssl'] = $this->sslOptions + $options['ssl']; |
|
68 | - } |
|
69 | - |
|
70 | - list($code, $headers, $content) = $this->fileGetContents($request->getUrl(), $options); |
|
71 | - return new Response($code, $headers, $content); |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * @internal |
|
77 | - * @param string |
|
78 | - * @param array |
|
79 | - * @return array |
|
80 | - * |
|
81 | - * @throws BadResponseException |
|
82 | - */ |
|
83 | - protected function fileGetContents($url, array $contextOptions) |
|
84 | - { |
|
85 | - $context = stream_context_create($contextOptions); |
|
86 | - |
|
87 | - $e = NULL; |
|
88 | - set_error_handler(function($severity, $message, $file, $line) use (& $e) { |
|
89 | - $e = new \ErrorException($message, 0, $severity, $file, $line, $e); |
|
90 | - }, E_WARNING); |
|
91 | - |
|
92 | - $content = file_get_contents($url, FALSE, $context); |
|
93 | - restore_error_handler(); |
|
94 | - |
|
95 | - if (!isset($http_response_header)) { |
|
96 | - throw new BadResponseException('Missing HTTP headers, request failed.', 0, $e); |
|
97 | - } |
|
98 | - |
|
99 | - if (!isset($http_response_header[0]) || !preg_match('~^HTTP/1[.]. (\d{3})~i', $http_response_header[0], $m)) { |
|
100 | - throw new BadResponseException('HTTP status code is missing.', 0, $e); |
|
101 | - } |
|
102 | - unset($http_response_header[0]); |
|
103 | - |
|
104 | - $headers = []; |
|
105 | - foreach ($http_response_header as $header) { |
|
106 | - if (in_array(substr($header, 0, 1), [' ', "\t"], TRUE)) { |
|
107 | - $headers[$last] .= ' ' . trim($header); # RFC2616, 2.2 |
|
108 | - } else { |
|
109 | - list($name, $value) = explode(':', $header, 2) + [NULL, NULL]; |
|
110 | - $headers[$last = trim($name)] = trim($value); |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - return [$m[1], $headers, $content]; |
|
115 | - } |
|
13 | + /** @var array|NULL */ |
|
14 | + private $sslOptions; |
|
15 | + |
|
16 | + |
|
17 | + /** |
|
18 | + * @param array SSL context options {@link http://php.net/manual/en/context.ssl.php} |
|
19 | + */ |
|
20 | + public function __construct(array $sslOptions = NULL) |
|
21 | + { |
|
22 | + $this->sslOptions = $sslOptions; |
|
23 | + } |
|
24 | + |
|
25 | + |
|
26 | + protected function setupRequest(Request $request) |
|
27 | + { |
|
28 | + parent::setupRequest($request); |
|
29 | + $request->setHeader('Connection', 'close'); |
|
30 | + } |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * @return Response |
|
35 | + * |
|
36 | + * @throws BadResponseException |
|
37 | + */ |
|
38 | + protected function process(Request $request) |
|
39 | + { |
|
40 | + $headerStr = []; |
|
41 | + foreach ($request->getHeaders() as $name => $value) { |
|
42 | + foreach ((array) $value as $v) { |
|
43 | + $headerStr[] = "$name: $v"; |
|
44 | + } |
|
45 | + } |
|
46 | + |
|
47 | + $options = [ |
|
48 | + 'http' => [ |
|
49 | + 'method' => $request->getMethod(), |
|
50 | + 'header' => implode("\r\n", $headerStr) . "\r\n", |
|
51 | + 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
52 | + 'protocol_version' => 1.1, |
|
53 | + 'ignore_errors' => TRUE, |
|
54 | + ], |
|
55 | + 'ssl' => [ |
|
56 | + 'verify_peer' => TRUE, |
|
57 | + 'cafile' => realpath(__DIR__ . '/../../ca-chain.crt'), |
|
58 | + 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
59 | + ], |
|
60 | + ]; |
|
61 | + |
|
62 | + if (($content = $request->getContent()) !== NULL) { |
|
63 | + $options['http']['content'] = $content; |
|
64 | + } |
|
65 | + |
|
66 | + if ($this->sslOptions) { |
|
67 | + $options['ssl'] = $this->sslOptions + $options['ssl']; |
|
68 | + } |
|
69 | + |
|
70 | + list($code, $headers, $content) = $this->fileGetContents($request->getUrl(), $options); |
|
71 | + return new Response($code, $headers, $content); |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * @internal |
|
77 | + * @param string |
|
78 | + * @param array |
|
79 | + * @return array |
|
80 | + * |
|
81 | + * @throws BadResponseException |
|
82 | + */ |
|
83 | + protected function fileGetContents($url, array $contextOptions) |
|
84 | + { |
|
85 | + $context = stream_context_create($contextOptions); |
|
86 | + |
|
87 | + $e = NULL; |
|
88 | + set_error_handler(function($severity, $message, $file, $line) use (& $e) { |
|
89 | + $e = new \ErrorException($message, 0, $severity, $file, $line, $e); |
|
90 | + }, E_WARNING); |
|
91 | + |
|
92 | + $content = file_get_contents($url, FALSE, $context); |
|
93 | + restore_error_handler(); |
|
94 | + |
|
95 | + if (!isset($http_response_header)) { |
|
96 | + throw new BadResponseException('Missing HTTP headers, request failed.', 0, $e); |
|
97 | + } |
|
98 | + |
|
99 | + if (!isset($http_response_header[0]) || !preg_match('~^HTTP/1[.]. (\d{3})~i', $http_response_header[0], $m)) { |
|
100 | + throw new BadResponseException('HTTP status code is missing.', 0, $e); |
|
101 | + } |
|
102 | + unset($http_response_header[0]); |
|
103 | + |
|
104 | + $headers = []; |
|
105 | + foreach ($http_response_header as $header) { |
|
106 | + if (in_array(substr($header, 0, 1), [' ', "\t"], TRUE)) { |
|
107 | + $headers[$last] .= ' ' . trim($header); # RFC2616, 2.2 |
|
108 | + } else { |
|
109 | + list($name, $value) = explode(':', $header, 2) + [NULL, NULL]; |
|
110 | + $headers[$last = trim($name)] = trim($value); |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + return [$m[1], $headers, $content]; |
|
115 | + } |
|
116 | 116 | |
117 | 117 | } |
@@ -12,84 +12,84 @@ |
||
12 | 12 | */ |
13 | 13 | class Request extends Message |
14 | 14 | { |
15 | - /** HTTP request method */ |
|
16 | - const |
|
17 | - DELETE = 'DELETE', |
|
18 | - GET = 'GET', |
|
19 | - HEAD = 'HEAD', |
|
20 | - PATCH = 'PATCH', |
|
21 | - POST = 'POST', |
|
22 | - PUT = 'PUT'; |
|
23 | - |
|
24 | - |
|
25 | - /** @var string */ |
|
26 | - private $method; |
|
27 | - |
|
28 | - /** @var string */ |
|
29 | - private $url; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * @param string |
|
34 | - * @param string |
|
35 | - * @param array |
|
36 | - * @param string|NULL |
|
37 | - */ |
|
38 | - public function __construct($method, $url, array $headers = [], $content = NULL) |
|
39 | - { |
|
40 | - $this->method = $method; |
|
41 | - $this->url = $url; |
|
42 | - parent::__construct($headers, $content); |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * @param string |
|
48 | - * @return bool |
|
49 | - */ |
|
50 | - public function isMethod($method) |
|
51 | - { |
|
52 | - return strcasecmp($this->method, $method) === 0; |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function getMethod() |
|
60 | - { |
|
61 | - return $this->method; |
|
62 | - } |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * @return string |
|
67 | - */ |
|
68 | - public function getUrl() |
|
69 | - { |
|
70 | - return $this->url; |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * @param string |
|
76 | - * @param string |
|
77 | - * @return self |
|
78 | - */ |
|
79 | - public function addHeader($name, $value) |
|
80 | - { |
|
81 | - return parent::addHeader($name, $value); |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * @param string |
|
87 | - * @param string|NULL |
|
88 | - * @return self |
|
89 | - */ |
|
90 | - public function setHeader($name, $value) |
|
91 | - { |
|
92 | - return parent::setHeader($name, $value); |
|
93 | - } |
|
15 | + /** HTTP request method */ |
|
16 | + const |
|
17 | + DELETE = 'DELETE', |
|
18 | + GET = 'GET', |
|
19 | + HEAD = 'HEAD', |
|
20 | + PATCH = 'PATCH', |
|
21 | + POST = 'POST', |
|
22 | + PUT = 'PUT'; |
|
23 | + |
|
24 | + |
|
25 | + /** @var string */ |
|
26 | + private $method; |
|
27 | + |
|
28 | + /** @var string */ |
|
29 | + private $url; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * @param string |
|
34 | + * @param string |
|
35 | + * @param array |
|
36 | + * @param string|NULL |
|
37 | + */ |
|
38 | + public function __construct($method, $url, array $headers = [], $content = NULL) |
|
39 | + { |
|
40 | + $this->method = $method; |
|
41 | + $this->url = $url; |
|
42 | + parent::__construct($headers, $content); |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * @param string |
|
48 | + * @return bool |
|
49 | + */ |
|
50 | + public function isMethod($method) |
|
51 | + { |
|
52 | + return strcasecmp($this->method, $method) === 0; |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function getMethod() |
|
60 | + { |
|
61 | + return $this->method; |
|
62 | + } |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * @return string |
|
67 | + */ |
|
68 | + public function getUrl() |
|
69 | + { |
|
70 | + return $this->url; |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * @param string |
|
76 | + * @param string |
|
77 | + * @return self |
|
78 | + */ |
|
79 | + public function addHeader($name, $value) |
|
80 | + { |
|
81 | + return parent::addHeader($name, $value); |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * @param string |
|
87 | + * @param string|NULL |
|
88 | + * @return self |
|
89 | + */ |
|
90 | + public function setHeader($name, $value) |
|
91 | + { |
|
92 | + return parent::setHeader($name, $value); |
|
93 | + } |
|
94 | 94 | |
95 | 95 | } |
@@ -12,97 +12,97 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class Message extends Github\Sanity |
14 | 14 | { |
15 | - /** @var array[name => value] */ |
|
16 | - private $headers = []; |
|
17 | - |
|
18 | - /** @var string|NULL */ |
|
19 | - private $content; |
|
20 | - |
|
21 | - |
|
22 | - /** |
|
23 | - * @param array |
|
24 | - * @param string|NULL |
|
25 | - */ |
|
26 | - public function __construct(array $headers = [], $content = NULL) |
|
27 | - { |
|
28 | - $this->headers = array_change_key_case($headers, CASE_LOWER); |
|
29 | - $this->content = $content; |
|
30 | - } |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * @param string |
|
35 | - * @return bool |
|
36 | - */ |
|
37 | - public function hasHeader($name) |
|
38 | - { |
|
39 | - return array_key_exists(strtolower($name), $this->headers); |
|
40 | - } |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * @param string |
|
45 | - * @param mixed |
|
46 | - * @return mixed |
|
47 | - */ |
|
48 | - public function getHeader($name, $default = NULL) |
|
49 | - { |
|
50 | - $name = strtolower($name); |
|
51 | - return array_key_exists($name, $this->headers) |
|
52 | - ? $this->headers[$name] |
|
53 | - : $default; |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string |
|
59 | - * @param string |
|
60 | - * @return self |
|
61 | - */ |
|
62 | - protected function addHeader($name, $value) |
|
63 | - { |
|
64 | - $name = strtolower($name); |
|
65 | - if (!array_key_exists($name, $this->headers) && $value !== NULL) { |
|
66 | - $this->headers[$name] = $value; |
|
67 | - } |
|
68 | - |
|
69 | - return $this; |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * @param string |
|
75 | - * @param string|NULL |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - protected function setHeader($name, $value) |
|
79 | - { |
|
80 | - $name = strtolower($name); |
|
81 | - if ($value === NULL) { |
|
82 | - unset($this->headers[$name]); |
|
83 | - } else { |
|
84 | - $this->headers[$name] = $value; |
|
85 | - } |
|
86 | - |
|
87 | - return $this; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - public function getHeaders() |
|
95 | - { |
|
96 | - return $this->headers; |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @return string|NULL |
|
102 | - */ |
|
103 | - public function getContent() |
|
104 | - { |
|
105 | - return $this->content; |
|
106 | - } |
|
15 | + /** @var array[name => value] */ |
|
16 | + private $headers = []; |
|
17 | + |
|
18 | + /** @var string|NULL */ |
|
19 | + private $content; |
|
20 | + |
|
21 | + |
|
22 | + /** |
|
23 | + * @param array |
|
24 | + * @param string|NULL |
|
25 | + */ |
|
26 | + public function __construct(array $headers = [], $content = NULL) |
|
27 | + { |
|
28 | + $this->headers = array_change_key_case($headers, CASE_LOWER); |
|
29 | + $this->content = $content; |
|
30 | + } |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * @param string |
|
35 | + * @return bool |
|
36 | + */ |
|
37 | + public function hasHeader($name) |
|
38 | + { |
|
39 | + return array_key_exists(strtolower($name), $this->headers); |
|
40 | + } |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * @param string |
|
45 | + * @param mixed |
|
46 | + * @return mixed |
|
47 | + */ |
|
48 | + public function getHeader($name, $default = NULL) |
|
49 | + { |
|
50 | + $name = strtolower($name); |
|
51 | + return array_key_exists($name, $this->headers) |
|
52 | + ? $this->headers[$name] |
|
53 | + : $default; |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string |
|
59 | + * @param string |
|
60 | + * @return self |
|
61 | + */ |
|
62 | + protected function addHeader($name, $value) |
|
63 | + { |
|
64 | + $name = strtolower($name); |
|
65 | + if (!array_key_exists($name, $this->headers) && $value !== NULL) { |
|
66 | + $this->headers[$name] = $value; |
|
67 | + } |
|
68 | + |
|
69 | + return $this; |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * @param string |
|
75 | + * @param string|NULL |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + protected function setHeader($name, $value) |
|
79 | + { |
|
80 | + $name = strtolower($name); |
|
81 | + if ($value === NULL) { |
|
82 | + unset($this->headers[$name]); |
|
83 | + } else { |
|
84 | + $this->headers[$name] = $value; |
|
85 | + } |
|
86 | + |
|
87 | + return $this; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + public function getHeaders() |
|
95 | + { |
|
96 | + return $this->headers; |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @return string|NULL |
|
102 | + */ |
|
103 | + public function getContent() |
|
104 | + { |
|
105 | + return $this->content; |
|
106 | + } |
|
107 | 107 | |
108 | 108 | } |
@@ -12,110 +12,110 @@ |
||
12 | 12 | */ |
13 | 13 | class CurlClient extends AbstractClient |
14 | 14 | { |
15 | - /** @var array|NULL */ |
|
16 | - private $options; |
|
17 | - |
|
18 | - /** @var resource */ |
|
19 | - private $curl; |
|
20 | - |
|
21 | - |
|
22 | - /** |
|
23 | - * @param array cURL options {@link http://php.net/manual/en/function.curl-setopt.php} |
|
24 | - * |
|
25 | - * @throws Github\LogicException |
|
26 | - */ |
|
27 | - public function __construct(array $options = NULL) |
|
28 | - { |
|
29 | - if (!extension_loaded('curl')) { |
|
30 | - throw new Github\LogicException('cURL extension is not loaded.'); |
|
31 | - } |
|
32 | - |
|
33 | - $this->options = $options; |
|
34 | - } |
|
35 | - |
|
36 | - |
|
37 | - protected function setupRequest(Request $request) |
|
38 | - { |
|
39 | - parent::setupRequest($request); |
|
40 | - $request->addHeader('Connection', 'keep-alive'); |
|
41 | - } |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * @return Response |
|
46 | - * |
|
47 | - * @throws BadResponseException |
|
48 | - */ |
|
49 | - protected function process(Request $request) |
|
50 | - { |
|
51 | - $headers = []; |
|
52 | - foreach ($request->getHeaders() as $name => $value) { |
|
53 | - $headers[] = "$name: $value"; |
|
54 | - } |
|
55 | - |
|
56 | - $responseHeaders = []; |
|
57 | - $softOptions = [ |
|
58 | - CURLOPT_CONNECTTIMEOUT => 10, |
|
59 | - CURLOPT_SSL_VERIFYHOST => 2, |
|
60 | - CURLOPT_SSL_VERIFYPEER => 1, |
|
61 | - CURLOPT_CAINFO => realpath(__DIR__ . '/../ca-chain.crt'), |
|
62 | - ]; |
|
63 | - |
|
64 | - $hardOptions = [ |
|
65 | - CURLOPT_FOLLOWLOCATION => FALSE, # Github sets the Location header for 201 code too and redirection is not required for us |
|
66 | - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
67 | - CURLOPT_CUSTOMREQUEST => $request->getMethod(), |
|
68 | - CURLOPT_NOBODY => $request->isMethod(Request::HEAD), |
|
69 | - CURLOPT_URL => $request->getUrl(), |
|
70 | - CURLOPT_HTTPHEADER => $headers, |
|
71 | - CURLOPT_RETURNTRANSFER => TRUE, |
|
72 | - CURLOPT_POSTFIELDS => $request->getContent(), |
|
73 | - CURLOPT_HEADER => FALSE, |
|
74 | - CURLOPT_HEADERFUNCTION => function($curl, $line) use (& $responseHeaders, & $last) { |
|
75 | - if (strncasecmp($line, 'HTTP/', 5) === 0) { |
|
76 | - /** @todo Set proxy response as Response::setPrevious($proxyResponse)? */ |
|
77 | - # The HTTP/x.y may occur multiple times with proxy (HTTP/1.1 200 Connection Established) |
|
78 | - $responseHeaders = []; |
|
79 | - |
|
80 | - } elseif (in_array(substr($line, 0, 1), [' ', "\t"], TRUE)) { |
|
81 | - $responseHeaders[$last] .= ' ' . trim($line); # RFC2616, 2.2 |
|
82 | - |
|
83 | - } elseif ($line !== "\r\n") { |
|
84 | - list($name, $value) = explode(':', $line, 2); |
|
85 | - $responseHeaders[$last = trim($name)] = trim($value); |
|
86 | - } |
|
87 | - |
|
88 | - return strlen($line); |
|
89 | - }, |
|
90 | - ]; |
|
91 | - |
|
92 | - if (defined('CURLOPT_PROTOCOLS')) { # HHVM issue. Even cURL v7.26.0, constants are missing. |
|
93 | - $hardOptions[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
94 | - } |
|
95 | - |
|
96 | - if (!$this->curl) { |
|
97 | - $this->curl = curl_init(); |
|
98 | - if ($this->curl === FALSE) { |
|
99 | - throw new BadResponseException('Cannot init cURL handler.'); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - $result = curl_setopt_array($this->curl, $hardOptions + ($this->options ?: []) + $softOptions); |
|
104 | - if ($result === FALSE) { |
|
105 | - throw new BadResponseException('Setting cURL options failed: ' . curl_error($this->curl), curl_errno($this->curl)); |
|
106 | - } |
|
107 | - |
|
108 | - $content = curl_exec($this->curl); |
|
109 | - if ($content === FALSE) { |
|
110 | - throw new BadResponseException(curl_error($this->curl), curl_errno($this->curl)); |
|
111 | - } |
|
112 | - |
|
113 | - $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
114 | - if ($code === FALSE) { |
|
115 | - throw new BadResponseException('HTTP status code is missing:' . curl_error($this->curl), curl_errno($this->curl)); |
|
116 | - } |
|
117 | - |
|
118 | - return new Response($code, $responseHeaders, $content); |
|
119 | - } |
|
15 | + /** @var array|NULL */ |
|
16 | + private $options; |
|
17 | + |
|
18 | + /** @var resource */ |
|
19 | + private $curl; |
|
20 | + |
|
21 | + |
|
22 | + /** |
|
23 | + * @param array cURL options {@link http://php.net/manual/en/function.curl-setopt.php} |
|
24 | + * |
|
25 | + * @throws Github\LogicException |
|
26 | + */ |
|
27 | + public function __construct(array $options = NULL) |
|
28 | + { |
|
29 | + if (!extension_loaded('curl')) { |
|
30 | + throw new Github\LogicException('cURL extension is not loaded.'); |
|
31 | + } |
|
32 | + |
|
33 | + $this->options = $options; |
|
34 | + } |
|
35 | + |
|
36 | + |
|
37 | + protected function setupRequest(Request $request) |
|
38 | + { |
|
39 | + parent::setupRequest($request); |
|
40 | + $request->addHeader('Connection', 'keep-alive'); |
|
41 | + } |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * @return Response |
|
46 | + * |
|
47 | + * @throws BadResponseException |
|
48 | + */ |
|
49 | + protected function process(Request $request) |
|
50 | + { |
|
51 | + $headers = []; |
|
52 | + foreach ($request->getHeaders() as $name => $value) { |
|
53 | + $headers[] = "$name: $value"; |
|
54 | + } |
|
55 | + |
|
56 | + $responseHeaders = []; |
|
57 | + $softOptions = [ |
|
58 | + CURLOPT_CONNECTTIMEOUT => 10, |
|
59 | + CURLOPT_SSL_VERIFYHOST => 2, |
|
60 | + CURLOPT_SSL_VERIFYPEER => 1, |
|
61 | + CURLOPT_CAINFO => realpath(__DIR__ . '/../ca-chain.crt'), |
|
62 | + ]; |
|
63 | + |
|
64 | + $hardOptions = [ |
|
65 | + CURLOPT_FOLLOWLOCATION => FALSE, # Github sets the Location header for 201 code too and redirection is not required for us |
|
66 | + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
67 | + CURLOPT_CUSTOMREQUEST => $request->getMethod(), |
|
68 | + CURLOPT_NOBODY => $request->isMethod(Request::HEAD), |
|
69 | + CURLOPT_URL => $request->getUrl(), |
|
70 | + CURLOPT_HTTPHEADER => $headers, |
|
71 | + CURLOPT_RETURNTRANSFER => TRUE, |
|
72 | + CURLOPT_POSTFIELDS => $request->getContent(), |
|
73 | + CURLOPT_HEADER => FALSE, |
|
74 | + CURLOPT_HEADERFUNCTION => function($curl, $line) use (& $responseHeaders, & $last) { |
|
75 | + if (strncasecmp($line, 'HTTP/', 5) === 0) { |
|
76 | + /** @todo Set proxy response as Response::setPrevious($proxyResponse)? */ |
|
77 | + # The HTTP/x.y may occur multiple times with proxy (HTTP/1.1 200 Connection Established) |
|
78 | + $responseHeaders = []; |
|
79 | + |
|
80 | + } elseif (in_array(substr($line, 0, 1), [' ', "\t"], TRUE)) { |
|
81 | + $responseHeaders[$last] .= ' ' . trim($line); # RFC2616, 2.2 |
|
82 | + |
|
83 | + } elseif ($line !== "\r\n") { |
|
84 | + list($name, $value) = explode(':', $line, 2); |
|
85 | + $responseHeaders[$last = trim($name)] = trim($value); |
|
86 | + } |
|
87 | + |
|
88 | + return strlen($line); |
|
89 | + }, |
|
90 | + ]; |
|
91 | + |
|
92 | + if (defined('CURLOPT_PROTOCOLS')) { # HHVM issue. Even cURL v7.26.0, constants are missing. |
|
93 | + $hardOptions[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
|
94 | + } |
|
95 | + |
|
96 | + if (!$this->curl) { |
|
97 | + $this->curl = curl_init(); |
|
98 | + if ($this->curl === FALSE) { |
|
99 | + throw new BadResponseException('Cannot init cURL handler.'); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + $result = curl_setopt_array($this->curl, $hardOptions + ($this->options ?: []) + $softOptions); |
|
104 | + if ($result === FALSE) { |
|
105 | + throw new BadResponseException('Setting cURL options failed: ' . curl_error($this->curl), curl_errno($this->curl)); |
|
106 | + } |
|
107 | + |
|
108 | + $content = curl_exec($this->curl); |
|
109 | + if ($content === FALSE) { |
|
110 | + throw new BadResponseException(curl_error($this->curl), curl_errno($this->curl)); |
|
111 | + } |
|
112 | + |
|
113 | + $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
114 | + if ($code === FALSE) { |
|
115 | + throw new BadResponseException('HTTP status code is missing:' . curl_error($this->curl), curl_errno($this->curl)); |
|
116 | + } |
|
117 | + |
|
118 | + return new Response($code, $responseHeaders, $content); |
|
119 | + } |
|
120 | 120 | |
121 | 121 | } |
@@ -12,80 +12,80 @@ |
||
12 | 12 | */ |
13 | 13 | class Response extends Message |
14 | 14 | { |
15 | - /** HTTP 1.1 code */ |
|
16 | - const |
|
17 | - S200_OK = 200, |
|
18 | - S301_MOVED_PERMANENTLY = 301, |
|
19 | - S302_FOUND = 302, |
|
20 | - S304_NOT_MODIFIED = 304, |
|
21 | - S307_TEMPORARY_REDIRECT = 307, |
|
22 | - S400_BAD_REQUEST = 400, |
|
23 | - S401_UNAUTHORIZED = 401, |
|
24 | - S403_FORBIDDEN = 403, |
|
25 | - S404_NOT_FOUND = 404, |
|
26 | - S422_UNPROCESSABLE_ENTITY = 422; |
|
27 | - |
|
28 | - /** @var int */ |
|
29 | - private $code; |
|
30 | - |
|
31 | - /** @var Response */ |
|
32 | - private $previous; |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * @param int |
|
37 | - * @param array |
|
38 | - * @param string |
|
39 | - */ |
|
40 | - public function __construct($code, array $headers, $content) |
|
41 | - { |
|
42 | - $this->code = (int) $code; |
|
43 | - parent::__construct($headers, $content); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * HTTP code. |
|
49 | - * @return int |
|
50 | - */ |
|
51 | - public function getCode() |
|
52 | - { |
|
53 | - return $this->code; |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * @param int |
|
59 | - * @return bool |
|
60 | - */ |
|
61 | - public function isCode($code) |
|
62 | - { |
|
63 | - return $this->code === (int) $code; |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * @return Response|NULL |
|
69 | - */ |
|
70 | - public function getPrevious() |
|
71 | - { |
|
72 | - return $this->previous; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * @return self |
|
78 | - * |
|
79 | - * @throws Github\LogicException |
|
80 | - */ |
|
81 | - public function setPrevious(Response $previous = NULL) |
|
82 | - { |
|
83 | - if ($this->previous) { |
|
84 | - throw new Github\LogicException('Previous response is already set.'); |
|
85 | - } |
|
86 | - $this->previous = $previous; |
|
87 | - |
|
88 | - return $this; |
|
89 | - } |
|
15 | + /** HTTP 1.1 code */ |
|
16 | + const |
|
17 | + S200_OK = 200, |
|
18 | + S301_MOVED_PERMANENTLY = 301, |
|
19 | + S302_FOUND = 302, |
|
20 | + S304_NOT_MODIFIED = 304, |
|
21 | + S307_TEMPORARY_REDIRECT = 307, |
|
22 | + S400_BAD_REQUEST = 400, |
|
23 | + S401_UNAUTHORIZED = 401, |
|
24 | + S403_FORBIDDEN = 403, |
|
25 | + S404_NOT_FOUND = 404, |
|
26 | + S422_UNPROCESSABLE_ENTITY = 422; |
|
27 | + |
|
28 | + /** @var int */ |
|
29 | + private $code; |
|
30 | + |
|
31 | + /** @var Response */ |
|
32 | + private $previous; |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * @param int |
|
37 | + * @param array |
|
38 | + * @param string |
|
39 | + */ |
|
40 | + public function __construct($code, array $headers, $content) |
|
41 | + { |
|
42 | + $this->code = (int) $code; |
|
43 | + parent::__construct($headers, $content); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * HTTP code. |
|
49 | + * @return int |
|
50 | + */ |
|
51 | + public function getCode() |
|
52 | + { |
|
53 | + return $this->code; |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * @param int |
|
59 | + * @return bool |
|
60 | + */ |
|
61 | + public function isCode($code) |
|
62 | + { |
|
63 | + return $this->code === (int) $code; |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * @return Response|NULL |
|
69 | + */ |
|
70 | + public function getPrevious() |
|
71 | + { |
|
72 | + return $this->previous; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * @return self |
|
78 | + * |
|
79 | + * @throws Github\LogicException |
|
80 | + */ |
|
81 | + public function setPrevious(Response $previous = NULL) |
|
82 | + { |
|
83 | + if ($this->previous) { |
|
84 | + throw new Github\LogicException('Previous response is already set.'); |
|
85 | + } |
|
86 | + $this->previous = $previous; |
|
87 | + |
|
88 | + return $this; |
|
89 | + } |
|
90 | 90 | |
91 | 91 | } |
@@ -12,98 +12,98 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class AbstractClient extends Github\Sanity implements IClient |
14 | 14 | { |
15 | - /** @var int[] will follow Location header on these response codes */ |
|
16 | - public $redirectCodes = [ |
|
17 | - Response::S301_MOVED_PERMANENTLY, |
|
18 | - Response::S302_FOUND, |
|
19 | - Response::S307_TEMPORARY_REDIRECT, |
|
20 | - ]; |
|
21 | - |
|
22 | - /** @var int maximum redirects per request*/ |
|
23 | - public $maxRedirects = 5; |
|
24 | - |
|
25 | - /** @var callable|NULL */ |
|
26 | - private $onRequest; |
|
27 | - |
|
28 | - /** @var callable|NULL */ |
|
29 | - private $onResponse; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * @see https://developer.github.com/v3/#http-redirects |
|
34 | - * |
|
35 | - * @return Response |
|
36 | - * |
|
37 | - * @throws BadResponseException |
|
38 | - */ |
|
39 | - public function request(Request $request) |
|
40 | - { |
|
41 | - $request = clone $request; |
|
42 | - |
|
43 | - $counter = $this->maxRedirects; |
|
44 | - $previous = NULL; |
|
45 | - do { |
|
46 | - $this->setupRequest($request); |
|
47 | - |
|
48 | - $this->onRequest && call_user_func($this->onRequest, $request); |
|
49 | - $response = $this->process($request); |
|
50 | - $this->onResponse && call_user_func($this->onResponse, $response); |
|
51 | - |
|
52 | - $previous = $response->setPrevious($previous); |
|
53 | - |
|
54 | - if ($counter > 0 && in_array($response->getCode(), $this->redirectCodes) && $response->hasHeader('Location')) { |
|
55 | - /** @todo Use the same HTTP $method for redirection? Set $content to NULL? */ |
|
56 | - $request = new Request( |
|
57 | - $request->getMethod(), |
|
58 | - $response->getHeader('Location'), |
|
59 | - $request->getHeaders(), |
|
60 | - $request->getContent() |
|
61 | - ); |
|
62 | - |
|
63 | - $counter--; |
|
64 | - continue; |
|
65 | - } |
|
66 | - break; |
|
67 | - |
|
68 | - } while (TRUE); |
|
69 | - |
|
70 | - return $response; |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * @param callable|NULL function(Request $request) |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - public function onRequest($callback) |
|
79 | - { |
|
80 | - $this->onRequest = $callback; |
|
81 | - return $this; |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * @param callable|NULL function(Response $response) |
|
87 | - * @return self |
|
88 | - */ |
|
89 | - public function onResponse($callback) |
|
90 | - { |
|
91 | - $this->onResponse = $callback; |
|
92 | - return $this; |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - protected function setupRequest(Request $request) |
|
97 | - { |
|
98 | - $request->addHeader('Expect', ''); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * @return Response |
|
104 | - * |
|
105 | - * @throws BadResponseException |
|
106 | - */ |
|
107 | - abstract protected function process(Request $request); |
|
15 | + /** @var int[] will follow Location header on these response codes */ |
|
16 | + public $redirectCodes = [ |
|
17 | + Response::S301_MOVED_PERMANENTLY, |
|
18 | + Response::S302_FOUND, |
|
19 | + Response::S307_TEMPORARY_REDIRECT, |
|
20 | + ]; |
|
21 | + |
|
22 | + /** @var int maximum redirects per request*/ |
|
23 | + public $maxRedirects = 5; |
|
24 | + |
|
25 | + /** @var callable|NULL */ |
|
26 | + private $onRequest; |
|
27 | + |
|
28 | + /** @var callable|NULL */ |
|
29 | + private $onResponse; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * @see https://developer.github.com/v3/#http-redirects |
|
34 | + * |
|
35 | + * @return Response |
|
36 | + * |
|
37 | + * @throws BadResponseException |
|
38 | + */ |
|
39 | + public function request(Request $request) |
|
40 | + { |
|
41 | + $request = clone $request; |
|
42 | + |
|
43 | + $counter = $this->maxRedirects; |
|
44 | + $previous = NULL; |
|
45 | + do { |
|
46 | + $this->setupRequest($request); |
|
47 | + |
|
48 | + $this->onRequest && call_user_func($this->onRequest, $request); |
|
49 | + $response = $this->process($request); |
|
50 | + $this->onResponse && call_user_func($this->onResponse, $response); |
|
51 | + |
|
52 | + $previous = $response->setPrevious($previous); |
|
53 | + |
|
54 | + if ($counter > 0 && in_array($response->getCode(), $this->redirectCodes) && $response->hasHeader('Location')) { |
|
55 | + /** @todo Use the same HTTP $method for redirection? Set $content to NULL? */ |
|
56 | + $request = new Request( |
|
57 | + $request->getMethod(), |
|
58 | + $response->getHeader('Location'), |
|
59 | + $request->getHeaders(), |
|
60 | + $request->getContent() |
|
61 | + ); |
|
62 | + |
|
63 | + $counter--; |
|
64 | + continue; |
|
65 | + } |
|
66 | + break; |
|
67 | + |
|
68 | + } while (TRUE); |
|
69 | + |
|
70 | + return $response; |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * @param callable|NULL function(Request $request) |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + public function onRequest($callback) |
|
79 | + { |
|
80 | + $this->onRequest = $callback; |
|
81 | + return $this; |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * @param callable|NULL function(Response $response) |
|
87 | + * @return self |
|
88 | + */ |
|
89 | + public function onResponse($callback) |
|
90 | + { |
|
91 | + $this->onResponse = $callback; |
|
92 | + return $this; |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + protected function setupRequest(Request $request) |
|
97 | + { |
|
98 | + $request->addHeader('Expect', ''); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * @return Response |
|
104 | + * |
|
105 | + * @throws BadResponseException |
|
106 | + */ |
|
107 | + abstract protected function process(Request $request); |
|
108 | 108 | |
109 | 109 | } |
@@ -10,21 +10,21 @@ |
||
10 | 10 | */ |
11 | 11 | interface IClient |
12 | 12 | { |
13 | - /** |
|
14 | - * @return Response |
|
15 | - */ |
|
16 | - function request(Request $request); |
|
13 | + /** |
|
14 | + * @return Response |
|
15 | + */ |
|
16 | + function request(Request $request); |
|
17 | 17 | |
18 | - /** |
|
19 | - * @param callable|NULL |
|
20 | - * @return self |
|
21 | - */ |
|
22 | - function onRequest($callback); |
|
18 | + /** |
|
19 | + * @param callable|NULL |
|
20 | + * @return self |
|
21 | + */ |
|
22 | + function onRequest($callback); |
|
23 | 23 | |
24 | - /** |
|
25 | - * @param callable|NULL |
|
26 | - * @return self |
|
27 | - */ |
|
28 | - function onResponse($callback); |
|
24 | + /** |
|
25 | + * @param callable|NULL |
|
26 | + * @return self |
|
27 | + */ |
|
28 | + function onResponse($callback); |
|
29 | 29 | |
30 | 30 | } |
@@ -12,97 +12,97 @@ |
||
12 | 12 | */ |
13 | 13 | class Token extends Github\Sanity |
14 | 14 | { |
15 | - /** @var string */ |
|
16 | - private $value; |
|
17 | - |
|
18 | - /** @var string */ |
|
19 | - private $type; |
|
20 | - |
|
21 | - /** @var string[] */ |
|
22 | - private $scopes; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * @param string |
|
27 | - * @param string |
|
28 | - * @param string[] |
|
29 | - */ |
|
30 | - public function __construct($value, $type = '', array $scopes = []) |
|
31 | - { |
|
32 | - $this->value = $value; |
|
33 | - $this->type = $type; |
|
34 | - $this->scopes = $scopes; |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function getValue() |
|
42 | - { |
|
43 | - return $this->value; |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public function getType() |
|
51 | - { |
|
52 | - return $this->type; |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * @return string[] |
|
58 | - */ |
|
59 | - public function getScopes() |
|
60 | - { |
|
61 | - return $this->scopes; |
|
62 | - } |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * @see https://developer.github.com/v3/oauth/#scopes |
|
67 | - * |
|
68 | - * @param string |
|
69 | - * @return bool |
|
70 | - */ |
|
71 | - public function hasScope($scope) |
|
72 | - { |
|
73 | - if (in_array($scope, $this->scopes, TRUE)) { |
|
74 | - return TRUE; |
|
75 | - } |
|
76 | - |
|
77 | - static $superiors = [ |
|
78 | - 'user:email' => 'user', |
|
79 | - 'user:follow' => 'user', |
|
80 | - 'notifications' => 'repo', |
|
81 | - ]; |
|
82 | - |
|
83 | - if (array_key_exists($scope, $superiors) && in_array($superiors[$scope], $this->scopes, TRUE)) { |
|
84 | - return TRUE; |
|
85 | - } |
|
86 | - |
|
87 | - return FALSE; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** @internal */ |
|
92 | - public function toArray() |
|
93 | - { |
|
94 | - return [ |
|
95 | - 'value' => $this->value, |
|
96 | - 'type' => $this->type, |
|
97 | - 'scopes' => $this->scopes, |
|
98 | - ]; |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** @internal */ |
|
103 | - public static function createFromArray(array $data) |
|
104 | - { |
|
105 | - return new static($data['value'], $data['type'], $data['scopes']); |
|
106 | - } |
|
15 | + /** @var string */ |
|
16 | + private $value; |
|
17 | + |
|
18 | + /** @var string */ |
|
19 | + private $type; |
|
20 | + |
|
21 | + /** @var string[] */ |
|
22 | + private $scopes; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * @param string |
|
27 | + * @param string |
|
28 | + * @param string[] |
|
29 | + */ |
|
30 | + public function __construct($value, $type = '', array $scopes = []) |
|
31 | + { |
|
32 | + $this->value = $value; |
|
33 | + $this->type = $type; |
|
34 | + $this->scopes = $scopes; |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function getValue() |
|
42 | + { |
|
43 | + return $this->value; |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public function getType() |
|
51 | + { |
|
52 | + return $this->type; |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * @return string[] |
|
58 | + */ |
|
59 | + public function getScopes() |
|
60 | + { |
|
61 | + return $this->scopes; |
|
62 | + } |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * @see https://developer.github.com/v3/oauth/#scopes |
|
67 | + * |
|
68 | + * @param string |
|
69 | + * @return bool |
|
70 | + */ |
|
71 | + public function hasScope($scope) |
|
72 | + { |
|
73 | + if (in_array($scope, $this->scopes, TRUE)) { |
|
74 | + return TRUE; |
|
75 | + } |
|
76 | + |
|
77 | + static $superiors = [ |
|
78 | + 'user:email' => 'user', |
|
79 | + 'user:follow' => 'user', |
|
80 | + 'notifications' => 'repo', |
|
81 | + ]; |
|
82 | + |
|
83 | + if (array_key_exists($scope, $superiors) && in_array($superiors[$scope], $this->scopes, TRUE)) { |
|
84 | + return TRUE; |
|
85 | + } |
|
86 | + |
|
87 | + return FALSE; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** @internal */ |
|
92 | + public function toArray() |
|
93 | + { |
|
94 | + return [ |
|
95 | + 'value' => $this->value, |
|
96 | + 'type' => $this->type, |
|
97 | + 'scopes' => $this->scopes, |
|
98 | + ]; |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** @internal */ |
|
103 | + public static function createFromArray(array $data) |
|
104 | + { |
|
105 | + return new static($data['value'], $data['type'], $data['scopes']); |
|
106 | + } |
|
107 | 107 | |
108 | 108 | } |
@@ -12,35 +12,35 @@ |
||
12 | 12 | */ |
13 | 13 | class Configuration extends Github\Sanity |
14 | 14 | { |
15 | - /** @var string */ |
|
16 | - public $clientId; |
|
17 | - |
|
18 | - /** @var string */ |
|
19 | - public $clientSecret; |
|
20 | - |
|
21 | - /** @var string[] */ |
|
22 | - public $scopes; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * @param string |
|
27 | - * @param string |
|
28 | - * @param string[] |
|
29 | - */ |
|
30 | - public function __construct($clientId, $clientSecret, array $scopes = []) |
|
31 | - { |
|
32 | - $this->clientId = $clientId; |
|
33 | - $this->clientSecret = $clientSecret; |
|
34 | - $this->scopes = $scopes; |
|
15 | + /** @var string */ |
|
16 | + public $clientId; |
|
17 | + |
|
18 | + /** @var string */ |
|
19 | + public $clientSecret; |
|
20 | + |
|
21 | + /** @var string[] */ |
|
22 | + public $scopes; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * @param string |
|
27 | + * @param string |
|
28 | + * @param string[] |
|
29 | + */ |
|
30 | + public function __construct($clientId, $clientSecret, array $scopes = []) |
|
31 | + { |
|
32 | + $this->clientId = $clientId; |
|
33 | + $this->clientSecret = $clientSecret; |
|
34 | + $this->scopes = $scopes; |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | |
38 | - /** |
|
39 | - * @return Configuration |
|
40 | - */ |
|
41 | - public static function fromArray(array $conf) |
|
42 | - { |
|
43 | - return new static($conf['clientId'], $conf['clientSecret'], isset($conf['scopes']) ? $conf['scopes'] : []); |
|
44 | - } |
|
38 | + /** |
|
39 | + * @return Configuration |
|
40 | + */ |
|
41 | + public static function fromArray(array $conf) |
|
42 | + { |
|
43 | + return new static($conf['clientId'], $conf['clientSecret'], isset($conf['scopes']) ? $conf['scopes'] : []); |
|
44 | + } |
|
45 | 45 | |
46 | 46 | } |