/
runtime.php
106 lines (85 loc) · 1.87 KB
/
runtime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
class Runtime
{
const FINISHED = 'finished';
const PROGRESS = 'progress';
private $logs;
private $status;
public function __construct($logs, $status)
{
$this->logs = $logs;
$this->status = $status;
}
public function log($line)
{
$this->logs[] = array(strtolower($line), date('H:i:s', time()));
}
public function reset()
{
$this->logs = array();
}
public function finished()
{
$this->status = self::FINISHED;
}
public function progress()
{
$this->status = self::PROGRESS;
}
public function __toString()
{
return json_encode(
array(
'finished' => $this->status == self::FINISHED ? 1 : 0,
'progress' => $this->status == self::PROGRESS ? 1 : 0,
'lines' => array_slice($this->logs, -35)
)
);
}
}
class SH
{
const LOGS = 'SHLOGS';
const STATUS = 'SHSTATUS';
/** @var Runtime */
static $runtime;
public static function load()
{
if (is_object(self::$runtime))
{
return;
}
if (isset($_SESSION[self::LOGS]) && is_object($_SESSION[self::LOGS]))
{
self::$runtime = $_SESSION[self::LOGS];
return;
}
self::reset();
}
public static function log($line)
{
self::load();
self::$runtime->log($line);
}
public static function finish()
{
self::load();
self::$runtime->finished();
}
public static function flush()
{
self::load();
return (string)self::$runtime;
}
public static function reset()
{
$_SESSION[self::LOGS] = self::$runtime = new Runtime(
array(),
Runtime::PROGRESS
);
}
}
function __($line)
{
SH::log($line);
}