|
1
|
|
|
<?php |
|
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Deployer\Util; |
|
9
|
|
|
|
|
10
|
|
|
class Reporter |
|
11
|
|
|
{ |
|
12
|
|
|
const ENDPOINT = 'https://deployer.org/api/stats'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param array $stats |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function report(array $stats) |
|
18
|
|
|
{ |
|
19
|
|
|
$pid = null; |
|
20
|
|
|
if (extension_loaded('pcntl')) { |
|
21
|
|
|
declare(ticks = 1); |
|
22
|
|
|
$pid = pcntl_fork(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
if (is_null($pid) || $pid === -1) { |
|
26
|
|
|
// Fork fails or there is no `pcntl` extension. |
|
27
|
|
|
self::send($stats); |
|
28
|
|
|
} elseif ($pid === 0) { |
|
29
|
|
|
// Child process. |
|
30
|
|
|
posix_setsid(); |
|
31
|
|
|
self::send($stats); |
|
32
|
|
|
// Close child process after doing job. |
|
33
|
|
|
exit(0); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $stats |
|
39
|
|
|
*/ |
|
40
|
|
|
private static function send(array $stats) |
|
41
|
|
|
{ |
|
42
|
|
|
if (extension_loaded('curl')) { |
|
43
|
|
|
$body = json_encode($stats, JSON_PRETTY_PRINT); |
|
44
|
|
|
$ch = curl_init(self::ENDPOINT); |
|
45
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
|
46
|
|
|
'Content-Type: application/json', |
|
47
|
|
|
'Content-Length: ' . strlen($body) |
|
48
|
|
|
]); |
|
49
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); |
|
50
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
51
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
52
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
53
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); |
|
54
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
|
55
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
|
56
|
|
|
curl_exec($ch); |
|
57
|
|
|
} else { |
|
58
|
|
|
file_get_contents(self::ENDPOINT . '?' . http_build_query($stats)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|