1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dlindberg\Pasteboard; |
4
|
|
|
|
5
|
|
|
class Pasteboard |
6
|
|
|
{ |
7
|
|
|
|
8
|
9 |
|
public static function set($value) |
9
|
|
|
{ |
10
|
9 |
|
return self::action('pbcopy', $value); |
11
|
|
|
} |
12
|
|
|
|
13
|
9 |
|
public static function get() |
14
|
|
|
{ |
15
|
9 |
|
return self::action('pbpaste'); |
16
|
|
|
} |
17
|
|
|
|
18
|
5 |
|
public static function setArray($array, $options = array()) |
19
|
|
|
{ |
20
|
5 |
|
$config = self::configureArray($options); |
21
|
5 |
|
$initial = null; |
22
|
5 |
|
if ($config['reset']) { |
23
|
1 |
|
$initial = self::get(); |
24
|
|
|
} |
25
|
5 |
|
foreach ($array as $value) { |
26
|
5 |
|
if (!self::setArrayValue($value,$config)) { |
27
|
5 |
|
return false; |
28
|
|
|
} |
29
|
|
|
} |
30
|
5 |
|
if ($config['reset']) { |
31
|
1 |
|
self::set($initial); |
32
|
|
|
} |
33
|
|
|
|
34
|
5 |
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
9 |
|
private static function action($action, $value = null) |
38
|
|
|
{ |
39
|
9 |
|
$output = false; |
40
|
9 |
|
$do = proc_open( |
41
|
9 |
|
$action, |
42
|
|
|
array( |
43
|
9 |
|
0 => array("pipe", "r"), |
44
|
|
|
1 => array("pipe", "w"), |
45
|
|
|
2 => array("pipe", "w"), |
46
|
|
|
), |
47
|
9 |
|
$pipes |
48
|
|
|
); |
49
|
9 |
|
if (is_resource($do)) { |
50
|
|
|
switch ($action) { |
51
|
9 |
|
case 'pbcopy': |
52
|
9 |
|
if (isset($value)) { |
53
|
9 |
|
fwrite($pipes[0], $value); |
54
|
|
|
} |
55
|
9 |
|
break; |
56
|
9 |
|
case 'pbpaste': |
57
|
9 |
|
$output = stream_get_contents($pipes[1]); |
58
|
9 |
|
break; |
59
|
|
|
} |
60
|
9 |
|
foreach ($pipes as $k => $v) { |
61
|
9 |
|
fclose($pipes[$k]); |
62
|
|
|
} |
63
|
9 |
|
$status = proc_close($do); |
64
|
|
|
} |
65
|
9 |
|
if (isset($status) && $status === 0 && $output === false) { |
66
|
9 |
|
$output = true; |
67
|
9 |
|
} elseif (isset($status) && $status === 0 && mb_strlen($output) === 0) { |
68
|
9 |
|
$output = false; |
69
|
|
|
} |
70
|
|
|
|
71
|
9 |
|
return $output; |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
private static function configureArray($initial) |
75
|
|
|
{ |
76
|
|
|
$config = array( |
77
|
5 |
|
'reset' => false, |
78
|
|
|
'depth' => 0, |
79
|
|
|
'wait' => 1, |
80
|
|
|
); |
81
|
5 |
|
if (isset($initial['reset'])) { |
82
|
1 |
|
$config['reset'] = $initial['reset']; |
83
|
|
|
} |
84
|
5 |
|
if (isset($initial['depth'])) { |
85
|
3 |
|
$config['depth'] = $initial['depth']; |
86
|
|
|
} |
87
|
5 |
|
if (isset($initial['wait'])) { |
88
|
4 |
|
$config['wait'] = $initial['wait']; |
89
|
|
|
} |
90
|
5 |
|
if (isset($initial['heartbeat'])) { |
91
|
3 |
|
$config['heartbeat'] = $initial['heartbeat']; |
92
|
|
|
} else { |
93
|
5 |
|
$config['heartbeat'] = function ($result) use ($config) { |
94
|
5 |
|
$return = false; |
95
|
5 |
|
if ($result) { |
96
|
5 |
|
sleep($config['wait']); |
97
|
5 |
|
$return = true; |
98
|
|
|
} |
99
|
|
|
|
100
|
5 |
|
return $return; |
101
|
|
|
}; |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
return $config; |
105
|
|
|
} |
106
|
|
|
|
107
|
5 |
|
private static function setArrayValue($value, $config) { |
108
|
5 |
|
if (!is_array($value)) { |
109
|
5 |
|
$return = $config['heartbeat'](self::set($value)); |
110
|
3 |
|
} elseif ($config['depth'] != 0) { |
111
|
3 |
|
$return = self::setArray($value, array('depth' => $config['depth'] - 1, 'heartbeat' => $config['heartbeat'],)); |
112
|
|
|
} else { |
113
|
2 |
|
$return = true; |
114
|
|
|
} |
115
|
5 |
|
return $return; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|