Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exclude_paths:
- ".github/**"
- "example/**"
- "test/**"

13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
}
},

"scripts": {
"phpunit": "vendor/bin/phpunit --configuration phpunit.xml",
"phpstan": "vendor/bin/phpstan analyse --memory-limit=512M --level 6 src",
"phpcs": "vendor/bin/phpcs src --standard=phpcs.xml",
"phpmd": "vendor/bin/phpmd src/ text phpmd.xml",
"test": [
"@phpunit",
"@phpstan",
"@phpcs",
"@phpmd"
]
},

"funding": [
{
"type": "github",
Expand Down
93 changes: 47 additions & 46 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@
class Pool {
/** @var Process[] Associative array of name=>Process */
protected array $processList;
/** @var array<int, callable(Process):void> */
protected array $completeCallbackList;

public function __construct() {
$this->processList = [];
$this->completeCallbackList = [];
}

public function add(string $name, Process $process):void {
$this->processList[$name] = $process;
$process->onComplete(
function(Process $completedProcess):void {
$this->dispatchCompletionCallback($completedProcess);
}
);
}

public function onComplete(callable $callback):void {
$this->completeCallbackList[] = $callback;
}

/** Starts the execution of all processes */
Expand Down Expand Up @@ -102,4 +114,10 @@ public function close():array {

return $codes;
}

protected function dispatchCompletionCallback(Process $process):void {
foreach($this->completeCallbackList as $callback) {
$callback($process);
}
}
}
31 changes: 31 additions & 0 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ class Process {
protected bool $isBlocking = false;
/** @var array<string, string> */
protected array $env;
/** @var array<int, callable(Process):void> */
protected array $completeCallbackList;
protected bool $hasCompleted;

public function __construct(string...$command) {
$this->command = $command;
$this->cwd = getcwd();
$this->pipes = [];
$this->env = getenv();
$this->completeCallbackList = [];
$this->hasCompleted = false;
}

public function __destruct() {
Expand All @@ -38,6 +43,15 @@ public function setEnv(string $key, string $value):void {
$this->env[$key] = $value;
}

public function onComplete(callable $callback):void {
if($this->hasCompleted) {
$callback($this);
return;
}

array_push($this->completeCallbackList, $callback);
}

/**
* Runs the command in a concurrent thread.
* Sets the input, output and errors streams.
Expand Down Expand Up @@ -80,6 +94,7 @@ public function exec():void {
}

$this->refreshStatus();
$this->dispatchCompletionCallback();

if($this->status["exitcode"] === 127) {
throw new CommandNotFoundException($this->command[0]);
Expand All @@ -90,6 +105,7 @@ public function exec():void {

public function isRunning():bool {
$this->refreshStatus();
$this->dispatchCompletionCallback();

$running = $this->status["running"] ?? false;
return (bool)$running;
Expand Down Expand Up @@ -177,4 +193,19 @@ protected function refreshStatus():void {
}
}
}

protected function dispatchCompletionCallback():void {
if($this->hasCompleted || empty($this->status)) {
return;
}

if($this->status["running"] ?? false) {
return;
}

$this->hasCompleted = true;
foreach($this->completeCallbackList as $callback) {
$callback($this);
}
}
}
Loading
Loading