@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3abstract class DrydockCommandInterface extends DrydockInterface {
4
5 const INTERFACE_TYPE = 'command';
6
7 private $workingDirectoryStack = array();
8
9 public function pushWorkingDirectory($working_directory) {
10 $this->workingDirectoryStack[] = $working_directory;
11 return $this;
12 }
13
14 public function popWorkingDirectory() {
15 if (!$this->workingDirectoryStack) {
16 throw new Exception(
17 pht(
18 'Unable to pop working directory, directory stack is empty.'));
19 }
20 return array_pop($this->workingDirectoryStack);
21 }
22
23 public function peekWorkingDirectory() {
24 if ($this->workingDirectoryStack) {
25 return last($this->workingDirectoryStack);
26 }
27 return null;
28 }
29
30 final public function getInterfaceType() {
31 return self::INTERFACE_TYPE;
32 }
33
34 final public function exec($command) {
35 $argv = func_get_args();
36 $exec = call_user_func_array(
37 array($this, 'getExecFuture'),
38 $argv);
39 return $exec->resolve();
40 }
41
42 final public function execx($command) {
43 $argv = func_get_args();
44 $exec = call_user_func_array(
45 array($this, 'getExecFuture'),
46 $argv);
47 return $exec->resolvex();
48 }
49
50 abstract public function getExecFuture($command);
51
52 protected function applyWorkingDirectoryToArgv(array $argv) {
53 $directory = $this->peekWorkingDirectory();
54
55 if ($directory !== null) {
56 $cmd = $argv[0];
57 $cmd = "(cd %s && {$cmd})";
58 $argv = array_merge(
59 array($cmd),
60 array($directory),
61 array_slice($argv, 1));
62 }
63
64 return $argv;
65 }
66
67}