PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.3.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.3.0
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Logger / SseEventCache.php
wp-staging / Framework / Logger Last commit date
BackgroundLogger.php 11 months ago SseEventCache.php 11 months ago
SseEventCache.php
92 lines
1 <?php
2
3 namespace WPStaging\Framework\Logger;
4
5 use WPStaging\Framework\Adapter\Directory;
6 use WPStaging\Framework\Utils\Cache\Cache;
7
8 /**
9 * This class is used to cache the events for the SSE (Server-Sent Events) stream.
10 * It stores the events in a cache file and allows to push new events, load existing events,
11 * It is used by BackgroundLogger to push the events to the SSE stream.
12 */
13 class SseEventCache
14 {
15 /**
16 * @var string
17 */
18 const EVENT_TYPE_TASK = 'task';
19
20 /**
21 * @var string
22 */
23 const EVENT_TYPE_COMPLETE = 'complete';
24
25 /**
26 * @var int
27 */
28 protected $count = 0;
29
30 /**
31 * @var array
32 */
33 protected $events = [];
34
35 /**
36 * @var Cache
37 */
38 protected $cache;
39
40 public function __construct(Cache $cache, Directory $directory)
41 {
42 $this->cache = $cache;
43 $this->cache->setPath($directory->getSseCacheDirectory());
44 }
45
46 public function setJobId(string $jobId, bool $checkIfExist = false)
47 {
48 $this->cache->setFilename($jobId . '.sse');
49 if ($checkIfExist && !$this->cache->isValid(false)) {
50 return false;
51 }
52
53 return true;
54 }
55
56 public function push(array $log)
57 {
58 $this->events[] = $log;
59
60 $this->count++;
61 $this->cache->save($this->events);
62 }
63
64 public function load()
65 {
66 if (!$this->cache->isValid()) {
67 return;
68 }
69
70 $this->events = $this->cache->get([]);
71 if (!is_array($this->events)) {
72 $this->events = [];
73 }
74
75 $this->count = count($this->events);
76 }
77
78 public function getCount()
79 {
80 return $this->count;
81 }
82
83 public function getEvents(int $offset = 0)
84 {
85 if ($offset >= $this->count) {
86 return [];
87 }
88
89 return array_slice($this->events, $offset);
90 }
91 }
92