|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Gerrie package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Gerrie\API\DataService; |
|
12
|
|
|
|
|
13
|
|
|
use Gerrie\Component\Connection\SSH; |
|
14
|
|
|
use Buzz\Client\Curl; |
|
15
|
|
|
use Buzz\Browser; |
|
16
|
|
|
use Buzz\Listener\BasicAuthListener; |
|
17
|
|
|
|
|
18
|
|
|
class DataServiceFactory |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Main factory method to get a data service based on the incoming instance config. |
|
23
|
|
|
* |
|
24
|
|
|
* $instanceConfig is an array in the structure: |
|
25
|
|
|
* [ |
|
26
|
|
|
* 'Instance' => [ |
|
27
|
|
|
* 'scheme' => "ssh", |
|
28
|
|
|
* 'host' => "review.typo3.org", |
|
29
|
|
|
* 'port' => 29418, |
|
30
|
|
|
* 'user' => "max.mustermann", |
|
31
|
|
|
* ... |
|
32
|
|
|
* ], |
|
33
|
|
|
* 'KeyFile' => null | /Path/To/Key/File |
|
34
|
|
|
* ] |
|
35
|
|
|
* |
|
36
|
|
|
* The KeyFile attribute is not needed for the HTTP data service. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $instanceConfig |
|
39
|
|
|
* @return HTTPDataService|SSHDataService |
|
40
|
|
|
* @throws \RuntimeException |
|
41
|
|
|
*/ |
|
42
|
10 |
|
public static function getDataService(array $instanceConfig) |
|
43
|
|
|
{ |
|
44
|
10 |
|
$instanceDetails = parse_url($instanceConfig['Instance']); |
|
45
|
|
|
|
|
46
|
10 |
|
if (isset($instanceDetails['host']) === false || isset($instanceDetails['scheme']) === false) { |
|
47
|
4 |
|
$exceptionMessage = sprintf('Error while parsing instance configuration "%s"', $instanceConfig['Instance']); |
|
48
|
4 |
|
throw new \RuntimeException($exceptionMessage, 1415453791); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
if (!array_key_exists('port', $instanceDetails)) { |
|
52
|
3 |
|
$instanceDetails['port'] = null; |
|
53
|
3 |
|
} |
|
54
|
|
|
|
|
55
|
6 |
|
$instanceConfig['Instance'] = $instanceDetails; |
|
56
|
|
|
|
|
57
|
6 |
|
$scheme = strtoupper($instanceConfig['Instance']['scheme']); |
|
58
|
|
|
switch ($scheme) { |
|
59
|
6 |
|
case 'SSH': |
|
60
|
1 |
|
$dataService = static::bootstrapSSHDataService($instanceConfig); |
|
61
|
1 |
|
break; |
|
62
|
5 |
|
case 'HTTPS': |
|
63
|
5 |
|
case 'HTTP': |
|
64
|
2 |
|
unset($instanceConfig['KeyFile']); |
|
65
|
2 |
|
$dataService = static::bootstrapHTTPDataService($instanceConfig); |
|
66
|
2 |
|
break; |
|
67
|
3 |
|
default: |
|
68
|
3 |
|
$exceptionMessage = sprintf('Data service for scheme "%s" is not available', $scheme); |
|
69
|
3 |
|
throw new \RuntimeException($exceptionMessage, 1364130057); |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
return $dataService; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Bootstraps a data service for the Gerrit SSH API. |
|
77
|
|
|
* |
|
78
|
|
|
* For $instanceConfig documentation see self::getDataService. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $instanceConfig |
|
81
|
|
|
* @return SSHDataService |
|
82
|
|
|
*/ |
|
83
|
1 |
|
protected static function bootstrapSSHDataService(array $instanceConfig) |
|
84
|
|
|
{ |
|
85
|
|
|
// TODO: This is a little bit strange that i have to put KeyFile and Port into the SSH adapter |
|
86
|
|
|
// I think this has to be refactored. |
|
87
|
|
|
// All SSH credentials have to be in one place |
|
88
|
|
|
$sshConfig = [ |
|
89
|
1 |
|
'KeyFile' => $instanceConfig['KeyFile'], |
|
90
|
1 |
|
'Port' => $instanceConfig['Instance']['port'] |
|
91
|
1 |
|
]; |
|
92
|
1 |
|
$ssh = new SSH('ssh', $sshConfig); |
|
93
|
1 |
|
$dataService = new SSHDataService($ssh, $instanceConfig['Instance']); |
|
94
|
|
|
|
|
95
|
1 |
|
return $dataService; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Bootstraps a data service for the Gerrit REST / HTTP API. |
|
100
|
|
|
* |
|
101
|
|
|
* For $instanceConfig documentation see self::getDataService. |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $instanceConfig |
|
104
|
|
|
* @return HTTPDataService |
|
105
|
|
|
*/ |
|
106
|
2 |
|
protected static function bootstrapHTTPDataService(array $instanceConfig) |
|
107
|
|
|
{ |
|
108
|
2 |
|
$restClient = static::getHTTPClientInstance($instanceConfig); |
|
109
|
2 |
|
$dataService = new HTTPDataService($restClient, $instanceConfig['Instance']); |
|
110
|
|
|
|
|
111
|
2 |
|
return $dataService; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Creates the HTTP abstraction object hierarchy. |
|
116
|
|
|
* For this purpose we use an external library called "Buzz". |
|
117
|
|
|
* |
|
118
|
|
|
* For $instanceConfig documentation see self::getDataService. |
|
119
|
|
|
* |
|
120
|
|
|
* @param array $instanceConfig |
|
121
|
|
|
* @return Browser |
|
122
|
|
|
*/ |
|
123
|
2 |
|
protected static function getHTTPClientInstance(array $instanceConfig) |
|
124
|
|
|
{ |
|
125
|
2 |
|
$username = ((isset($instanceConfig['Instance']['user']) === true) ? $instanceConfig['Instance']['user'] : ''); |
|
126
|
2 |
|
$password = ((isset($instanceConfig['Instance']['pass']) === true) ? $instanceConfig['Instance']['pass'] : ''); |
|
127
|
|
|
|
|
128
|
|
|
// Bootstrap the REST client |
|
129
|
2 |
|
$curlClient = new Curl(); |
|
130
|
2 |
|
$curlClient->setVerifyPeer(false); |
|
131
|
2 |
|
$restClient = new Browser($curlClient); |
|
132
|
|
|
|
|
133
|
2 |
|
if ($username && $password) { |
|
134
|
2 |
|
$authListener = new BasicAuthListener($username, $password); |
|
135
|
2 |
|
$restClient->addListener($authListener); |
|
136
|
2 |
|
} |
|
137
|
|
|
|
|
138
|
2 |
|
return $restClient; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|