]> BookStack Code Mirror - system-cli/blobdiff - src/Services/ProgramRunner.php
Range of changes to MySQL execution
[system-cli] / src / Services / ProgramRunner.php
index eb3bda7e9abf1453874f7d7c1aa37191d1e4dad8..df1b815f090410bdf7cc6fe3e49de9f5db8a56a4 100644 (file)
@@ -1,21 +1,29 @@
-<?php
+<?php declare(strict_types=1);
 
 namespace Cli\Services;
 
+use Exception;
 use Symfony\Component\Process\ExecutableFinder;
 use Symfony\Component\Process\Process;
 
 class ProgramRunner
 {
+    /**
+     * Names of the program to search for.
+     * @var string[]
+     */
+    protected array $programNames = [];
+
     protected int $timeout = 240;
     protected int $idleTimeout = 15;
     protected array $environment = [];
     protected array $additionalProgramDirectories = [];
 
     public function __construct(
-        protected string $program,
+        string|array $program,
         protected string $defaultPath
     ) {
+        $this->programNames = is_string($program) ? [$program] : $program;
     }
 
     public function withTimeout(int $timeoutSeconds): static
@@ -81,7 +89,7 @@ class ProgramRunner
     }
 
     /**
-     * @throws \Exception
+     * @throws Exception
      */
     public function ensureFound(): void
     {
@@ -93,7 +101,7 @@ class ProgramRunner
         try {
             $this->ensureFound();
             return true;
-        } catch (\Exception $exception) {
+        } catch (Exception $exception) {
             return false;
         }
     }
@@ -108,15 +116,21 @@ class ProgramRunner
         return $process;
     }
 
+    /**
+     * @throws Exception
+     */
     protected function resolveProgramPath(): string
     {
-        $executableFinder = new ExecutableFinder();
-        $path = $executableFinder->find($this->program, $this->defaultPath, $this->additionalProgramDirectories);
+        foreach ($this->programNames as $programName) {
+            $executableFinder = new ExecutableFinder();
+            $path = $executableFinder->find($programName, $this->defaultPath, $this->additionalProgramDirectories);
 
-        if (is_null($path) || !is_file($path)) {
-            throw new \Exception("Could not locate \"{$this->program}\" program.");
+            if (!is_null($path) && is_file($path)) {
+                return $path;
+            }
         }
 
-        return $path;
+        $combinedProgram = implode('|', $this->programNames);
+        throw new Exception("Could not locate \"{$combinedProgram}\" program.");
     }
 }