1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher Updater Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
namespace SWP\UpdaterBundle\Client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Default Client class. |
18
|
|
|
* Allows to fetch data from the remote update server. |
19
|
|
|
*/ |
20
|
|
|
class DefaultClient implements ClientInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Default request options. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $options = array( |
28
|
|
|
'http' => array( |
29
|
|
|
'header' => array( |
30
|
|
|
"Accept: application/json\r\n", |
31
|
|
|
), |
32
|
|
|
), |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Remote's server URI. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $baseUri; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function __construct(array $config = array(), array $options = array()) |
46
|
|
|
{ |
47
|
|
|
$this->options = $options; |
48
|
|
|
$this->baseUri = $config['base_uri']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function call( |
55
|
|
|
$endpoint = '/', |
56
|
|
|
array $arguments = array(), |
57
|
|
|
array $options = array(), |
58
|
|
|
$fullResponse = false |
59
|
|
|
) { |
60
|
|
|
$context = stream_context_create($this->processOptions($options)); |
61
|
|
|
$response = @file_get_contents( |
62
|
|
|
$this->buildUrl($endpoint, $this->processParameters($arguments)), |
63
|
|
|
false, |
64
|
|
|
$context |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
if ($response === false) { |
68
|
|
|
throw new ClientException('Invalid response.'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($fullResponse) { |
72
|
|
|
return $this->decodeResponse($response); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function saveFile($fromUrl, $filePath) |
82
|
|
|
{ |
83
|
|
|
$opened = @fopen($fromUrl, 'r'); |
84
|
|
|
if ($opened) { |
85
|
|
|
$result = file_put_contents($filePath, $opened); |
86
|
|
|
if ($result) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function getBaseUrl() |
95
|
|
|
{ |
96
|
|
|
return rtrim($this->baseUri, '/'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function buildUrl($url, $params) |
100
|
|
|
{ |
101
|
|
|
$url = sprintf( |
102
|
|
|
'%s/%s?%s', |
103
|
|
|
$this->getBaseUrl(), |
104
|
|
|
ltrim($url, '/'), |
105
|
|
|
((!is_null($params)) ? http_build_query($params) : '') |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
return $url; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function processParameters($params) |
112
|
|
|
{ |
113
|
|
|
if (!is_array($params)) { |
114
|
|
|
return $params; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $params; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function processOptions($options) |
121
|
|
|
{ |
122
|
|
|
if (is_array($options)) { |
123
|
|
|
$options = array_merge($this->options, $options); |
124
|
|
|
} else { |
125
|
|
|
$options = $this->options; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $options; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function decodeResponse($response) |
132
|
|
|
{ |
133
|
|
|
return array( |
134
|
|
|
'headers' => array(), |
135
|
|
|
'status' => '200', |
136
|
|
|
'reason' => 'OK', |
137
|
|
|
'version' => '', |
138
|
|
|
'body' => $response, |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|