-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRemoteXdebug.php
More file actions
129 lines (112 loc) · 3.32 KB
/
RemoteXdebug.php
File metadata and controls
129 lines (112 loc) · 3.32 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
<?php
/**
* Code Coverage Driver
*
* @copyright 2013 Anthon Pang
* @license BSD-2-Clause
*/
namespace VIPSoft\CodeCoverageExtension\Driver;
use Guzzle\Http\Client;
use PHP_CodeCoverage_Driver as DriverInterface;
/**
* Remote xdebug driver
*
* @author Anthon Pang <apang@softwaredevelopment.ca>
*/
class RemoteXdebug implements DriverInterface
{
/**
* @var array
*/
private $config;
/**
* @var \Guzzle\Http\Client
*/
private $client;
/**
* Constructor
*
* [
* 'base_url' => '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 \Guzzle\Http\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()
{
$request = $this->buildRequest('create');
try {
$response = $request->send();
if ($response->getStatusCode() !== 200) {
throw new \Exception('remote driver start failed: ' . $response->getReasonPhrase());
}
} catch (\Exception $ex) {
print 'Remote failed due to:' . $ex->getMessage() . "\n";
}
}
/**
* {@inheritdoc}
*/
public function stop()
{
$request = $this->buildRequest('read');
$request->setHeader('Accept', 'application/json');
try {
$response = $request->send();
if ($response->getStatusCode() !== 200) {
throw new \Exception('remote driver fetch failed: ' . $response->getReasonPhrase());
}
$request = $this->buildRequest('delete');
$request->send();
return json_decode($response->getBody(true), true);
} catch (\Exception $ex) {
print 'Remote failed due to:' . $ex->getMessage() . "\n";
return array();
}
}
/**
* Construct request
*
* @param string $endpoint
*
* @return \Guzzle\Http\Message\Request
*/
private function buildRequest($endpoint)
{
$method = strtolower($this->config[$endpoint]['method']);
if (! in_array($method, array('get', 'post', 'put', 'delete'))) {
throw new \Exception($endpoint . ' method must be GET, POST, PUT, or DELETE');
}
$request = $this->client->$method($this->config[$endpoint]['path']);
if (isset($this->config['auth'])) {
$request->setAuth($this->config['auth']['user'], $this->config['auth']['password']);
}
return $request;
}
}