-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.php
More file actions
76 lines (60 loc) · 1.91 KB
/
main.php
File metadata and controls
76 lines (60 loc) · 1.91 KB
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
<?php
require 'vendor/autoload.php';
use Alfred\Workflows\Workflow;
$workflow = new Workflow;
class CodeRunner
{
private $workflow;
public function __construct()
{
$this->workflow = new Workflow;
}
public function execute($code)
{
$isSuccess = true;
$startTime = microtime(true);
$code = $this->transformCode($code);
try {
$result = @eval($code);
} catch(\ParseError $e) {
$isSuccess = false;
$result = $e->getMessage();
} catch(\Error $e) {
$isSuccess = false;
$result = $e->getMessage();
}
$duration = round(microtime(true) - $startTime, 4);
$subtitle = 'Copy to Clipboard | ' . $duration . ' seconds';
$error = error_get_last();
if ($error != NULL) {
$isSuccess = false;
$result = $error['message'];
}
if ($result == null) {
$result = 'No results';
$subtitle = '';
}
$workflowResult = $this->workflow->result()
->title($result)
->subtitle($subtitle)
->valid(true)
->icon($icon);
if ($isSuccess) {
$workflowResult->text('copy', $result)
->icon('/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns')
->arg($result);
} else {
$workflowResult->icon('/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns');
}
return $this->workflow->output();
}
public function transformCode($code) {
if (substr($code, 0, 7) != 'return ') {
$code = 'return ' . $code;
}
if (substr($code, -1) != ';') {
$code .= ';';
}
return $code;
}
}