ci4
72 строки · 1.7 Кб
1<?php
2
3declare(strict_types=1);
4
5/**
6* This file is part of CodeIgniter 4 framework.
7*
8* (c) CodeIgniter Foundation <admin@codeigniter.com>
9*
10* For the full copyright and license information, please view
11* the LICENSE file that was distributed with this source code.
12*/
13
14namespace CodeIgniter\HotReloader;
15
16/**
17* @internal
18*/
19final class HotReloader
20{
21public function run(): void
22{
23if (session_status() === PHP_SESSION_ACTIVE) {
24session_write_close();
25}
26
27ini_set('zlib.output_compression', 'Off');
28
29header('Cache-Control: no-store');
30header('Content-Type: text/event-stream');
31header('Access-Control-Allow-Methods: GET');
32
33ob_end_clean();
34set_time_limit(0);
35
36$hasher = new DirectoryHasher();
37$appHash = $hasher->hash();
38
39while (true) {
40if (connection_status() !== CONNECTION_NORMAL || connection_aborted()) {
41break;
42}
43
44$currentHash = $hasher->hash();
45
46// If hash has changed, tell the browser to reload.
47if ($currentHash !== $appHash) {
48$appHash = $currentHash;
49
50$this->sendEvent('reload', ['time' => date('Y-m-d H:i:s')]);
51break;
52}
53if (mt_rand(1, 10) > 8) {
54$this->sendEvent('ping', ['time' => date('Y-m-d H:i:s')]);
55}
56
57sleep(1);
58}
59}
60
61/**
62* Send an event to the browser.
63*/
64private function sendEvent(string $event, array $data): void
65{
66echo "event: {$event}\n";
67echo 'data: ' . json_encode($data) . "\n\n";
68
69ob_flush();
70flush();
71}
72}
73