|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ackintosh\Snidel; |
|
3
|
|
|
|
|
4
|
|
|
use Ackintosh\Snidel\Fork; |
|
5
|
|
|
use Ackintosh\Snidel\Pcntl; |
|
6
|
|
|
|
|
7
|
|
|
class ForkContainer implements \ArrayAccess |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var \Ackintosh\Snidel\Fork[] */ |
|
10
|
|
|
private $forks = array(); |
|
11
|
|
|
|
|
12
|
|
|
/** @var \Ackintosh\Snidel\Pcntl */ |
|
13
|
|
|
private $pcntl; |
|
14
|
|
|
|
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
private $tagsToPids = array(); |
|
17
|
|
|
|
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->pcntl = new Pcntl(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* fork process |
|
25
|
|
|
* |
|
26
|
|
|
* @return \Ackintosh\Snidel\Fork |
|
27
|
|
|
* @throws \RuntimeException |
|
28
|
|
|
*/ |
|
29
|
|
|
public function fork($tag = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$pid = $this->pcntl->fork(); |
|
32
|
|
|
if ($pid === -1) { |
|
33
|
|
|
throw new \RuntimeException('could not fork a new process'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->forks[$pid] = new Fork($pid); |
|
37
|
|
|
if ($tag !== null) { |
|
38
|
|
|
$this->tagsToPids[$pid] = $tag; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->forks[$pid]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $tag |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
public function hasTag($tag) |
|
50
|
|
|
{ |
|
51
|
|
|
return in_array($tag, $this->tagsToPids, true); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* wait child |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Ackintosh\Snidel\Fork |
|
58
|
|
|
*/ |
|
59
|
|
|
public function wait() |
|
60
|
|
|
{ |
|
61
|
|
|
$status = null; |
|
62
|
|
|
$childPid = $this->pcntl->waitpid(-1, $status); |
|
63
|
|
|
$this[$childPid]->setStatus($status); |
|
64
|
|
|
$this[$childPid]->loadResult(); |
|
65
|
|
|
|
|
66
|
|
|
return $this[$childPid]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function get($tag = null) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($tag === null) { |
|
72
|
|
|
return $this->forks; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->getWithTag($tag); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* return forks |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $tag |
|
82
|
|
|
* @return \Ackintosh\Snidel\Fork[] |
|
83
|
|
|
*/ |
|
84
|
|
|
private function getWithTag($tag) |
|
85
|
|
|
{ |
|
86
|
|
|
return array_filter($this->forks, function ($fork) use ($tag) { |
|
87
|
|
|
return $this->tagsToPids[$fork->getPid()] === $tag; |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param mixed $offset |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function offsetExists($offset) |
|
96
|
|
|
{ |
|
97
|
|
|
if (isset($this->forks[$offset]) && $this->forks[$offset] !== '') { |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param mixed $offset |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
|
|
public function offsetGet($offset) |
|
109
|
|
|
{ |
|
110
|
|
|
if (!$this->offsetExists($offset)) { |
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->forks[$offset]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param mixed $offset |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
public function offsetSet($offset, $value) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->forks[$offset] = $value; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param mixed $offset |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function offsetUnset($offset) |
|
131
|
|
|
{ |
|
132
|
|
|
unset($this->forks[$offset]); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|