xp-framework / core

The XP Framework is an all-purpose, object oriented PHP framework.
Other
19 stars 6 forks source link

Add Process::locate() to find commands in PATH #280

Closed thekid closed 3 years ago

thekid commented 3 years ago

This pull request adds the ability to resolve multiple commands, like #279 but by adding a new method instead of overloading the existing one.

use lang\Process;

// Resolve either docker or podman. Will contain NULL if neither is found.
$containers= Process::locate(['docker', 'podman'])->current();

// See which command was resolved and compose command line accordingly
$locate= Process::locate(['curl', 'wget']);
$download= match ($locate->key()) {
  'curl' => fn($url, $target) => $locate->current().' "-#" -L "'.$url.'" -o "'.$target.'"',
  'wget' => fn($url, $target) => $locate->current().' -nv "'.$url.'" -O "'.$target.'"',
  default => throw new IllegalArgumentException('Either curl or wget is required'),
};
thekid commented 3 years ago

Superseded by #281