Conditions | 5 |
Paths | 5 |
Total Lines | 29 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 30 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
15 | public static function fork( |
||
16 | int $numberOfWorkers, |
||
17 | callable $asyncServer, |
||
18 | int $numberOfFork = self::DEFAULT_NUMBER_OF_FORKS |
||
19 | ): int { |
||
20 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
21 | throw new RuntimeException('The PHP pcntl extension is not available for Windows systems'); |
||
22 | } |
||
23 | |||
24 | $pid = pcntl_fork(); |
||
25 | if (-1 === $pid) { |
||
26 | throw new RuntimeException('Fork Failed'); |
||
27 | } |
||
28 | |||
29 | if (0 === $pid) { |
||
30 | $asyncServer(); |
||
31 | pcntl_waitpid($pid, $status); |
||
32 | return $pid; |
||
33 | } |
||
34 | |||
35 | // @parent |
||
36 | $numberOfWorkers--; |
||
37 | ++$numberOfFork; |
||
38 | if (self::DEFAULT_NUMBER_OF_FORKS < $numberOfWorkers) { |
||
39 | self::fork($numberOfWorkers, $asyncServer, $numberOfFork); |
||
40 | } |
||
41 | |||
42 | pcntl_waitpid($pid, $status); |
||
43 | return $pid; |
||
44 | } |
||
46 |