-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathRemoteXdebug.php
More file actions
132 lines (116 loc) · 3.42 KB
/
RemoteXdebug.php
File metadata and controls
132 lines (116 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Code Coverage Driver
*
* @copyright 2013 Anthon Pang
*
* @license BSD-2-Clause
*/
namespace LeanPHP\Behat\CodeCoverage\Driver;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use SebastianBergmann\CodeCoverage\Driver\Driver as DriverInterface;
/**
* Remote xdebug driver
*
* @author Anthon Pang <apang@softwaredevelopment.ca>
*/
class RemoteXdebug implements DriverInterface
{
/**
* @var array
*/
private $config;
/**
* @var GuzzleHttp\Client
*/
private $client;
/**
* Constructor
*
* [
* 'base_uri' => 'http://api.example.com/1.0/coverage',
* 'auth' => [
* 'user' => 'user name',
* 'password' => 'password',
* ],
* 'create' => [
* 'method' => 'POST',
* 'path' => '/',
* ],
* 'read' => [
* 'method' => 'GET',
* 'path' => '/',
* ],
* 'delete' => [
* 'method' => 'DELETE',
* 'path' => '/',
* ],
* ]
*
* @param array $config Configuration
* @param GuzzleHttp\Client $client HTTP client
*/
public function __construct(array $config, Client $client)
{
$this->config = $config;
$this->client = $client;
//$this->client->setBaseUrl($config['base_url']);
}
/**
* {@inheritdoc}
*/
public function start(bool $determineUnusedAndDead = true): void
{
$response = $this->sendRequest('create');
if ($response->getStatusCode() !== 200) {
throw new \Exception(sprintf('remote driver start failed: %s', $response->getReasonPhrase()));
}
}
/**
* {@inheritdoc}
*/
public function stop(): array
{
$response = $this->sendRequest('read', ['headers' => ['Accept' => 'application/json']]);
if ($response->getStatusCode() !== 200) {
throw new \Exception(sprintf('remote driver fetch failed: %s', $response->getReasonPhrase()));
}
$response = $this->sendRequest('delete');
return json_decode($response->getBody(true), true);
}
/**
* Construct request
*
* @param string $endpoint
* @param array $headers
*
* @return GuzzleHttp\Psr7\Response
*/
private function sendRequest($endpoint, $headers = array())
{
$method = strtolower($this->config[$endpoint]['method']);
if (! in_array($method, array('get', 'post', 'put', 'delete'))) {
throw new \Exception(sprintf('%s method must be GET, POST, PUT, or DELETE', $endpoint));
}
if (isset($this->config['auth'])) {
$response = $this->client->request(
$method,
$this->config[$endpoint]['path'],
[
'auth' => [$this->config['auth']['user'], $this->config['auth']['password']],
'headers' => $headers,
]
);
} else {
$response = $this->client->request(
$method,
$this->config[$endpoint]['path'],
[
'headers' => $headers,
]
);
}
return $response;
}
}