Extracted Process Control to separate interface to allow better tests
This commit is contained in:
28
src/ProcessControl/Fork.php
Normal file
28
src/ProcessControl/Fork.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\Multiprocessing\ProcessControl;
|
||||
|
||||
class Fork
|
||||
{
|
||||
public int $pid;
|
||||
|
||||
public function __construct(int $pid)
|
||||
{
|
||||
$this->pid = $pid;
|
||||
}
|
||||
|
||||
public function failed(): bool
|
||||
{
|
||||
return $this->pid < 0;
|
||||
}
|
||||
|
||||
public function isChild(): bool
|
||||
{
|
||||
return $this->pid === 0;
|
||||
}
|
||||
|
||||
public function isParent(): bool
|
||||
{
|
||||
return $this->pid !== 0;
|
||||
}
|
||||
}
|
||||
18
src/ProcessControl/PCNTL.php
Normal file
18
src/ProcessControl/PCNTL.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\Multiprocessing\ProcessControl;
|
||||
|
||||
class PCNTL implements ProcessControl
|
||||
{
|
||||
public function fork(): Fork
|
||||
{
|
||||
$pid = pcntl_fork();
|
||||
return new Fork($pid);
|
||||
}
|
||||
|
||||
public function wait(int $options = 0): Wait
|
||||
{
|
||||
$pid = pcntl_wait($status, $options);
|
||||
return new Wait($pid, $status);
|
||||
}
|
||||
}
|
||||
10
src/ProcessControl/ProcessControl.php
Normal file
10
src/ProcessControl/ProcessControl.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\Multiprocessing\ProcessControl;
|
||||
|
||||
interface ProcessControl
|
||||
{
|
||||
public function fork(): Fork;
|
||||
|
||||
public function wait(int $options = 0): Wait;
|
||||
}
|
||||
25
src/ProcessControl/Wait.php
Normal file
25
src/ProcessControl/Wait.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\Multiprocessing\ProcessControl;
|
||||
|
||||
class Wait
|
||||
{
|
||||
public int $pid;
|
||||
public int $status;
|
||||
|
||||
public function __construct(int $pid, int $status = 0)
|
||||
{
|
||||
$this->pid = $pid;
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
public function childStopped(): bool
|
||||
{
|
||||
return $this->pid > 0;
|
||||
}
|
||||
|
||||
public function failed(): bool
|
||||
{
|
||||
return $this->pid < 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user