]> BookStack Code Mirror - system-cli/blobdiff - scripts/Services/ProgramRunner.php
Added dep check and composer auto-install to init command
[system-cli] / scripts / Services / ProgramRunner.php
index 6f94f1e307865917b45f1c41e8c8e97482bf3bbb..acc51b959b66206102d62e0e021a50640dd438af 100644 (file)
@@ -10,6 +10,7 @@ class ProgramRunner
     protected int $timeout = 240;
     protected int $idleTimeout = 15;
     protected array $environment = [];
+    protected array $additionalProgramDirectories = [];
 
     public function __construct(
         protected string $program,
@@ -35,6 +36,12 @@ class ProgramRunner
         return $this;
     }
 
+    public function withAdditionalPathLocation(string $directoryPath): static
+    {
+        $this->additionalProgramDirectories[] = $directoryPath;
+        return $this;
+    }
+
     public function runCapturingAllOutput(array $args): string
     {
         $output = '';
@@ -55,16 +62,30 @@ class ProgramRunner
         return $err;
     }
 
-    public function runWithoutOutputCallbacks(array $args, callable $stdOutCallback, callable $stdErrCallback): void
+    public function runWithoutOutputCallbacks(array $args, callable $stdOutCallback = null, callable $stdErrCallback = null): int
     {
         $process = $this->startProcess($args);
         foreach ($process as $type => $data) {
             if ($type === $process::ERR) {
-                $stdErrCallback($data);
+                if ($stdErrCallback) {
+                    $stdErrCallback($data);
+                }
             } else {
-                $stdOutCallback($data);
+                if ($stdOutCallback) {
+                    $stdOutCallback($data);
+                }
             }
         }
+
+        return $process->getExitCode() ?? 1;
+    }
+
+    /**
+     * @throws \Exception
+     */
+    public function ensureFound(): void
+    {
+        $this->resolveProgramPath();
     }
 
     protected function startProcess(array $args): Process
@@ -80,7 +101,7 @@ class ProgramRunner
     protected function resolveProgramPath(): string
     {
         $executableFinder = new ExecutableFinder();
-        $path = $executableFinder->find($this->program, $this->defaultPath);
+        $path = $executableFinder->find($this->program, $this->defaultPath, $this->additionalProgramDirectories);
 
         if (is_null($path) || !is_file($path)) {
             throw new \Exception("Could not locate \"{$this->program}\" program.");