Differential D14337 Diff 34622 src/applications/drydock/interface/command/DrydockCommandInterface.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/interface/command/DrydockCommandInterface.php
| <?php | <?php | ||||
| abstract class DrydockCommandInterface extends DrydockInterface { | abstract class DrydockCommandInterface extends DrydockInterface { | ||||
| const INTERFACE_TYPE = 'command'; | const INTERFACE_TYPE = 'command'; | ||||
| private $workingDirectory; | private $workingDirectoryStack = array(); | ||||
| public function setWorkingDirectory($working_directory) { | public function pushWorkingDirectory($working_directory) { | ||||
| $this->workingDirectory = $working_directory; | $this->workingDirectoryStack[] = $working_directory; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getWorkingDirectory() { | public function popWorkingDirectory() { | ||||
| return $this->workingDirectory; | if (!$this->workingDirectoryStack) { | ||||
| throw new Exception( | |||||
| pht( | |||||
| 'Unable to pop working directory, directory stack is empty.')); | |||||
| } | |||||
| return array_pop($this->workingDirectoryStack); | |||||
| } | |||||
| public function peekWorkingDirectory() { | |||||
| if ($this->workingDirectoryStack) { | |||||
| return last($this->workingDirectoryStack); | |||||
| } | |||||
| return null; | |||||
| } | } | ||||
| final public function getInterfaceType() { | final public function getInterfaceType() { | ||||
| return self::INTERFACE_TYPE; | return self::INTERFACE_TYPE; | ||||
| } | } | ||||
| final public function exec($command) { | final public function exec($command) { | ||||
| $argv = func_get_args(); | $argv = func_get_args(); | ||||
| Show All 9 Lines | $exec = call_user_func_array( | ||||
| array($this, 'getExecFuture'), | array($this, 'getExecFuture'), | ||||
| $argv); | $argv); | ||||
| return $exec->resolvex(); | return $exec->resolvex(); | ||||
| } | } | ||||
| abstract public function getExecFuture($command); | abstract public function getExecFuture($command); | ||||
| protected function applyWorkingDirectoryToArgv(array $argv) { | protected function applyWorkingDirectoryToArgv(array $argv) { | ||||
| if ($this->getWorkingDirectory() !== null) { | $directory = $this->peekWorkingDirectory(); | ||||
| if ($directory !== null) { | |||||
| $cmd = $argv[0]; | $cmd = $argv[0]; | ||||
| $cmd = "(cd %s && {$cmd})"; | $cmd = "(cd %s && {$cmd})"; | ||||
| $argv = array_merge( | $argv = array_merge( | ||||
| array($cmd), | array($cmd), | ||||
| array($this->getWorkingDirectory()), | array($directory), | ||||
| array_slice($argv, 1)); | array_slice($argv, 1)); | ||||
| } | } | ||||
| return $argv; | return $argv; | ||||
| } | } | ||||
| } | } | ||||