Chris@5: siteAlias = $siteAlias; Chris@5: $this->transport = $transport; Chris@5: $this->args = $args; Chris@5: $this->options = $options; Chris@5: $this->optionsPassedAsArgs = $optionsPassedAsArgs; Chris@5: Chris@5: parent::__construct([]); Chris@5: } Chris@5: Chris@5: /** Chris@5: * Get a starting directory for the remote process. Chris@5: * Chris@5: * @return string|null Chris@5: */ Chris@5: public function getWorkingDirectory() Chris@5: { Chris@5: return $this->cd_remote; Chris@5: } Chris@5: Chris@5: /** Chris@5: * Set a starting directory for the remote process. Chris@5: * Chris@5: * @param string $cd_remote Chris@5: * Chris@5: * @return \Consolidation\SiteProcess\SiteProcess Chris@5: */ Chris@5: public function setWorkingDirectory($cd_remote) Chris@5: { Chris@5: $this->cd_remote = $cd_remote; Chris@5: return $this; Chris@5: } Chris@5: Chris@5: /** Chris@5: * Set a starting directory for the initial/local process. Chris@5: * Chris@5: * @param string $cd Chris@5: * Chris@5: * @return \Consolidation\SiteProcess\SiteProcess Chris@5: */ Chris@5: public function setWorkingDirectoryLocal($cd) Chris@5: { Chris@5: return parent::setWorkingDirectory($cd); Chris@5: } Chris@5: Chris@5: /** Chris@5: * Get the starting directory for the initial/local process. Chris@5: * Chris@5: * @return string|null; Chris@5: */ Chris@5: public function getWorkingDirectoryLocal() Chris@5: { Chris@5: return parent::getWorkingDirectory(); Chris@5: } Chris@5: Chris@5: /** Chris@5: * Chris@5: * @param bool $shouldUseSiteRoot Chris@5: * @return $this|\Symfony\Component\Process\Process Chris@5: * @throws \Exception Chris@5: */ Chris@5: public function chdirToSiteRoot($shouldUseSiteRoot = true) Chris@5: { Chris@5: if (!$shouldUseSiteRoot || !$this->siteAlias->hasRoot()) { Chris@5: return $this; Chris@5: } Chris@5: Chris@5: return $this->setWorkingDirectory($this->siteAlias->root()); Chris@5: } Chris@5: Chris@5: /** Chris@5: * Take all of our individual arguments and process them for use. Chris@5: */ Chris@5: protected function processArgs() Chris@5: { Chris@5: $transport = $this->getTransport($this->siteAlias); Chris@5: $transport->configure($this); Chris@5: Chris@5: $processor = new ArgumentProcessor(); Chris@5: $selectedArgs = $processor->selectArgs( Chris@5: $this->siteAlias, Chris@5: $this->args, Chris@5: $this->options, Chris@5: $this->optionsPassedAsArgs Chris@5: ); Chris@5: Chris@5: // Ask the transport to drop in a 'cd' if needed. Chris@5: if ($this->getWorkingDirectory()) { Chris@5: $selectedArgs = $transport->addChdir($this->getWorkingDirectory(), $selectedArgs); Chris@5: } Chris@5: Chris@5: // Do any necessary interpolation on the selected arguments. Chris@5: $processedArgs = $this->interpolate($selectedArgs); Chris@5: Chris@5: // Wrap the command with 'ssh' or some other transport if this is Chris@5: // a remote command; otherwise, leave it as-is. Chris@5: return $transport->wrap($processedArgs); Chris@5: } Chris@5: Chris@5: public function setTransport($transport) Chris@5: { Chris@5: $this->transport = $transport; Chris@5: } Chris@5: Chris@5: /** Chris@5: * Ask the transport manager for the correct transport for the Chris@5: * provided alias. Chris@5: */ Chris@5: protected function getTransport(SiteAliasInterface $siteAlias) Chris@5: { Chris@5: return $this->transport; Chris@5: } Chris@5: Chris@5: /** Chris@5: * @inheritDoc Chris@5: */ Chris@5: public function getCommandLine() Chris@5: { Chris@5: $commandLine = parent::getCommandLine(); Chris@5: if (empty($commandLine)) { Chris@5: $processedArgs = $this->processArgs(); Chris@5: $commandLine = Escape::argsForSite($this->siteAlias, $processedArgs); Chris@5: $commandLine = implode(' ', $commandLine); Chris@5: $this->setCommandLine($commandLine); Chris@5: } Chris@5: return $commandLine; Chris@5: } Chris@5: Chris@5: /** Chris@5: * @inheritDoc Chris@5: */ Chris@5: public function start(callable $callback = null, $env = array()) Chris@5: { Chris@5: $cmd = $this->getCommandLine(); Chris@5: parent::start($callback, $env); Chris@5: } Chris@5: Chris@5: /** Chris@5: * @inheritDoc Chris@5: */ Chris@5: public function wait(callable $callback = null) Chris@5: { Chris@5: $return = parent::wait($callback); Chris@5: return $return; Chris@5: } Chris@5: Chris@5: /** Chris@5: * interpolate examines each of the arguments in the provided argument list Chris@5: * and replaces any token found therein with the value for that key as Chris@5: * pulled from the given site alias. Chris@5: * Chris@5: * Example: "git -C {{root}} status" Chris@5: * Chris@5: * The token "{{root}}" will be converted to a value via $siteAlias->get('root'). Chris@5: * The result will replace the token. Chris@5: * Chris@5: * It is possible to use dot notation in the keys to access nested elements Chris@5: * within the site alias record. Chris@5: * Chris@5: * @param SiteAliasInterface $siteAlias Chris@5: * @param type $args Chris@5: * @return type Chris@5: */ Chris@5: protected function interpolate($args) Chris@5: { Chris@5: $interpolator = new Interpolator(); Chris@5: return array_map( Chris@5: function ($arg) use ($interpolator) { Chris@5: if ($arg instanceof ShellOperatorInterface) { Chris@5: return $arg; Chris@5: } Chris@5: return $interpolator->interpolate($this->siteAlias, $arg, false); Chris@5: }, Chris@5: $args Chris@5: ); Chris@5: } Chris@5: }