|
4 | 4 |
|
5 | 5 | namespace Laravel\Boost\Telemetry; |
6 | 6 |
|
7 | | -use const PHP_OS_FAMILY; |
8 | | -use const PHP_VERSION; |
9 | | - |
10 | 7 | use Composer\InstalledVersions; |
11 | | -use Illuminate\Support\Facades\Http; |
| 8 | +use Laravel\Boost\Concerns\MakesHttpRequests; |
12 | 9 | use Throwable; |
13 | 10 |
|
14 | 11 | class TelemetryCollector |
15 | 12 | { |
| 13 | + use MakesHttpRequests; |
| 14 | + |
16 | 15 | protected const MAX_TOOLS_PER_FLUSH = 20; |
17 | 16 |
|
18 | 17 | public array $toolCounts = []; |
19 | 18 |
|
20 | | - protected bool $shutdownRegistered = false; |
| 19 | + protected bool $enabled; |
21 | 20 |
|
22 | | - public function record(string $toolName): void |
23 | | - { |
24 | | - if (! config('boost.telemetry.enabled')) { |
25 | | - return; |
26 | | - } |
| 21 | + protected string $url; |
27 | 22 |
|
28 | | - $totalCount = array_sum($this->toolCounts); |
29 | | - if ($totalCount >= self::MAX_TOOLS_PER_FLUSH) { |
30 | | - $this->flush(); |
31 | | - } |
| 23 | + protected string $sessionId; |
| 24 | + |
| 25 | + protected string $laravelVersion; |
| 26 | + |
| 27 | + public function __construct() |
| 28 | + { |
| 29 | + $this->enabled = config('boost.telemetry.enabled', false); |
| 30 | + if ($this->enabled) { |
| 31 | + $this->url = config('boost.telemetry.url', 'https://boost.laravel.com/api/telemetry'); |
| 32 | + $this->sessionId = hash('sha256', base_path()); |
| 33 | + $this->laravelVersion = app()->version(); |
| 34 | + app()->terminating($this->flush(...)); |
32 | 35 |
|
33 | | - if (! $this->shutdownRegistered) { |
34 | 36 | if (extension_loaded('pcntl')) { |
35 | 37 | pcntl_async_signals(true); |
36 | 38 | pcntl_signal(SIGINT, $this->flush(...)); |
37 | 39 | pcntl_signal(SIGTERM, $this->flush(...)); |
38 | 40 | } |
| 41 | + } |
| 42 | + } |
39 | 43 |
|
40 | | - register_shutdown_function([$this, 'flush']); |
| 44 | + public function __destruct() |
| 45 | + { |
| 46 | + $this->flush(); |
| 47 | + } |
41 | 48 |
|
42 | | - app()->terminating($this->flush(...)); |
| 49 | + public function record(string $toolName): void |
| 50 | + { |
| 51 | + if (! $this->enabled) { |
| 52 | + return; |
| 53 | + } |
43 | 54 |
|
44 | | - $this->shutdownRegistered = true; |
| 55 | + $totalCount = array_sum($this->toolCounts); |
| 56 | + if ($totalCount >= self::MAX_TOOLS_PER_FLUSH) { |
| 57 | + $this->flush(); |
45 | 58 | } |
46 | 59 |
|
47 | 60 | $this->toolCounts[$toolName] = ($this->toolCounts[$toolName] ?? 0) + 1; |
48 | 61 | } |
49 | 62 |
|
50 | 63 | public function flush(): void |
51 | 64 | { |
52 | | - if ($this->toolCounts === [] || ! config('boost.telemetry.enabled', true)) { |
| 65 | + if ($this->toolCounts === [] || ! $this->enabled) { |
53 | 66 | return; |
54 | 67 | } |
55 | 68 |
|
56 | 69 | try { |
57 | | - Http::timeout(5) |
58 | | - ->withHeaders(['User-Agent' => 'Laravel Boost Telemetry']) |
59 | | - ->post(config('boost.telemetry.url'), ['data' => $this->buildPayload()]); |
| 70 | + $this->client() |
| 71 | + ->timeout(5) |
| 72 | + ->post($this->url, ['data' => $this->buildPayload()]); |
60 | 73 | } catch (Throwable) { |
61 | 74 | // |
62 | 75 | } finally { |
63 | 76 | $this->toolCounts = []; |
64 | 77 | } |
65 | | - |
66 | 78 | } |
67 | 79 |
|
68 | 80 | protected function buildPayload(): string |
69 | 81 | { |
70 | 82 | $version = InstalledVersions::getVersion('laravel/boost'); |
71 | 83 |
|
72 | 84 | return base64_encode(json_encode([ |
73 | | - 'session_id' => hash('sha256', base_path()), |
| 85 | + 'session_id' => $this->sessionId, |
74 | 86 | 'boost_version' => $version, |
75 | 87 | 'php_version' => PHP_VERSION, |
76 | 88 | 'os' => PHP_OS_FAMILY, |
77 | | - 'laravel_version' => app()->version(), |
| 89 | + 'laravel_version' => $this->laravelVersion, |
78 | 90 | 'tools' => $this->toolCounts, |
79 | | - 'timestamp' => now()->toIso8601String(), |
| 91 | + 'timestamp' => date('c'), |
80 | 92 | ])); |
81 | 93 | } |
82 | 94 | } |
0 commit comments