xp-framework / core

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

Enhance Process::resolve() to be able to resolve multiple commands #279

Closed thekid closed 3 years ago

thekid commented 3 years ago

This pull request adds the ability to resolve multiple commands, and control whether an exception should be raised or NULL returned instead.

use lang\Process;

// Resolve either docker or podman, raise an exception if neither is found
$containers= Process::resolve(['docker', 'podman']);

// Optionally resolve commands by passing true
$download= match (true) {
  !is_null($cmd= Process::resolve('curl', true)) => fn($url, $target) => $cmd.' "-#" -L "'.$url.'" -o "'.$target.'"',
  !is_null($cmd= Process::resolve('wget', true)) => fn($url, $target) => $cmd.' -nv "'.$url.'" -O "'.$target.'"',
  default => throw new IllegalArgumentException('Either curl or wget is required'),
};
thekid commented 3 years ago

Superseded by #281