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($stats) |
18
|
|
|
{ |
19
|
|
|
$send = function () use ($stats) { |
20
|
|
|
if (extension_loaded('curl')) { |
21
|
|
|
$body = json_encode($stats, JSON_PRETTY_PRINT); |
22
|
|
|
$ch = curl_init(self::ENDPOINT); |
23
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
24
|
|
|
'Content-Type: application/json', |
25
|
|
|
'Content-Length: ' . strlen($body) |
26
|
|
|
]); |
27
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); |
28
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
29
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
30
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
31
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); |
32
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
33
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
34
|
|
|
curl_exec($ch); |
35
|
|
|
} else { |
36
|
|
|
file_get_contents(self::ENDPOINT . '?' . http_build_query($stats)); |
37
|
|
|
} |
38
|
|
|
}; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
if (extension_loaded('pcntl')) { |
42
|
|
|
declare(ticks = 1); |
43
|
|
|
$pid = pcntl_fork(); |
44
|
|
|
if ($pid === 0) { |
45
|
|
|
posix_setsid(); |
46
|
|
|
$send(); |
47
|
|
|
exit(0); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
|
$send(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.