PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 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 1 day ago EventLoggerConst.php 1 day ago SseEventCache.php 1 day ago
SseEventCache.php
134 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
10
11
12
13 class SseEventCache
14 {
15
16
17
18 const EVENT_TYPE_TASK = 'task';
19
20
21
22
23 const EVENT_TYPE_MEMORY_EXHAUST = 'memory_exhaust';
24
25
26
27
28 const EVENT_TYPE_FATAL_ERROR = 'fatal_error';
29
30
31
32
33 const EVENT_TYPE_COMPLETE = 'complete';
34
35
36
37
38 protected $cacheDirectory = '';
39
40
41
42
43 protected $count = 0;
44
45
46
47
48 protected $events = [];
49
50
51
52
53 protected $cache;
54
55 public function __construct(Cache $cache, Directory $directory)
56 {
57 $this->cacheDirectory = $directory->getSseCacheDirectory();
58 $this->cache = $cache;
59 $this->cache->setPath($this->cacheDirectory);
60 }
61
62
63
64
65 public function deleteSseCacheFiles()
66 {
67 if (!file_exists($this->cacheDirectory)) {
68 return;
69 }
70
71 $iterator = new \DirectoryIterator($this->cacheDirectory);
72 foreach ($iterator as $fileInfo) {
73 if ($fileInfo->isFile() && strpos($fileInfo->getFilename(), 'sse.cache.php') !== false) {
74 unlink($fileInfo->getPathname());
75 }
76 }
77 }
78
79 public function setJobId(string $jobId, bool $checkIfExist = false)
80 {
81 $this->cache->setFilename($jobId . '.sse');
82 if ($checkIfExist && !$this->cache->isValid(false)) {
83 return false;
84 }
85
86 return true;
87 }
88
89 public function push(array $log)
90 {
91
92 $this->load();
93
94 $this->events[] = $log;
95
96 $this->count++;
97 $this->cache->save($this->events);
98 }
99
100 public function load()
101 {
102
103 $filePath = $this->cache->getFilePath();
104 if ($filePath !== '') {
105 clearstatcache(true, $filePath);
106 }
107
108 if (!$this->cache->isValid()) {
109 return;
110 }
111
112 $this->events = $this->cache->get([]);
113 if (!is_array($this->events)) {
114 $this->events = [];
115 }
116
117 $this->count = count($this->events);
118 }
119
120 public function getCount()
121 {
122 return $this->count;
123 }
124
125 public function getEvents(int $offset = 0)
126 {
127 if ($offset >= $this->count) {
128 return [];
129 }
130
131 return array_slice($this->events, $offset);
132 }
133 }
134