Skip to content

Commit

Permalink
Change alternatives from string to array
Browse files Browse the repository at this point in the history
Fix phpcs errors
  • Loading branch information
apotek committed Sep 15, 2023
1 parent 34f60d1 commit 0ade565
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/Robo/Plugin/Commands/DevelopmentModeBaseCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public function databaseRefreshTugboat(): ResultData
try {
$dbPath = $this->databaseDownload($siteName);
} catch (TaskException $te) {
$this->yell("$siteName: No database configured. Download/import skipped.");
$result_data->append($te->getMessage());
// @todo: Should we run a site-install by default?
continue;
Expand Down
22 changes: 11 additions & 11 deletions src/Robo/Task/Discovery/Alternatives.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Robo\Task\BaseTask;
use Robo\Result;
use Robo\ResultData;

/**
* Use this to figure out which binary to use from a series of alternatives.
Expand All @@ -21,9 +22,9 @@
class Alternatives extends BaseTask
{
/**
* @var string
* @var array
*/
protected string $alternatives;
protected array $alternatives;

/**
* @var string
Expand All @@ -40,11 +41,11 @@ class Alternatives extends BaseTask
*
* @param string $command
* The preferred binary or path to binary to execute.
* @param string $alternatives
* A comma-separated list of alternative binaries or paths to binaries to
* @param array $alternatives
* A list of alternative binaries or paths to binaries to
* execute.
*/
public function __construct(string $command, string $alternatives)
public function __construct(string $command, array $alternatives)
{
$this->command = $command;
$this->alternatives = $alternatives;
Expand All @@ -56,17 +57,17 @@ public function __construct(string $command, string $alternatives)
* The array returned by Result::getData() will always contain a 'path'
* property, even if empty.
*/
public function run(): Result
public function run(): Result|ResultData
{
$this->printTaskInfo("Resolving binary for {$this->command}...");
if (@file_exists($this->command) && is_executable($this->command)) {
return Result::success($this, "Found {$this->command}", ['path' => $this->command]);
}
$output = [];
$result_code = NULL;
$alternatives = explode(',', $this->alternatives);
array_unshift($alternatives, $this->command);
foreach ($alternatives as $alternative) {
$result_code = null;

array_unshift($this->alternatives, $this->command);
foreach ($this->alternatives as $alternative) {
$arg = escapeshellarg($alternative);
exec("which $arg", $output, $result_code);
if ($result_code === static::SHELL_SUCCESS) {
Expand All @@ -76,5 +77,4 @@ public function run(): Result
$this->printTaskWarning('Could not resolve any of the executables suggested.');
return Result::cancelled('Could not resolve any of the executables suggested.', ['path' => '']);
}

}
7 changes: 4 additions & 3 deletions src/Robo/Task/Discovery/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
trait Tasks
{
/**
* @param string|string[] $dirs
* @param string $command
* @param array $alternatives
*
* @return \Usher\Robo\Task\Discovery\Alternatives|\Robo\Collection\CollectionBuilder
*/
protected function taskAlternatives($dirs)
protected function taskAlternatives(string $command, array $alternatives)
{
return $this->task(Alternatives::class, $dirs);
return $this->task(Alternatives::class, $command, $alternatives);
}
}

0 comments on commit 0ade565

Please sign in to comment.