|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Consolidation\SiteProcess\Transport; |
| 4 | + |
| 5 | +use Consolidation\SiteProcess\SiteProcess; |
| 6 | +use Consolidation\SiteProcess\Util\Escape; |
| 7 | +use Consolidation\SiteAlias\SiteAliasInterface; |
| 8 | +use Consolidation\SiteProcess\Util\Shell; |
| 9 | + |
| 10 | +/** |
| 11 | + * VagrantTransport knows how to wrap a command such that it runs on a remote |
| 12 | + * system via the vagrant cli. |
| 13 | + */ |
| 14 | +class VagrantTransport implements TransportInterface |
| 15 | +{ |
| 16 | + protected $tty; |
| 17 | + protected $siteAlias; |
| 18 | + |
| 19 | + public function __construct(SiteAliasInterface $siteAlias) |
| 20 | + { |
| 21 | + $this->siteAlias = $siteAlias; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @inheritdoc |
| 26 | + */ |
| 27 | + public function configure(SiteProcess $process) |
| 28 | + { |
| 29 | + $this->tty = $process->isTty(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * inheritdoc |
| 34 | + */ |
| 35 | + public function wrap($args) |
| 36 | + { |
| 37 | + $transport = ['vagrant', 'ssh']; |
| 38 | + $transportOptions = $this->getTransportOptions(); |
| 39 | + $commandToExecute = $this->getCommandToExecute($args); |
| 40 | + |
| 41 | + return array_merge( |
| 42 | + $transport, |
| 43 | + $transportOptions, |
| 44 | + ['-c'], |
| 45 | + $commandToExecute |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @inheritdoc |
| 51 | + */ |
| 52 | + public function addChdir($cd_remote, $args) |
| 53 | + { |
| 54 | + return array_merge( |
| 55 | + [ |
| 56 | + 'cd', |
| 57 | + $cd_remote, |
| 58 | + Shell::op('&&'), |
| 59 | + ], |
| 60 | + $args |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * getTransportOptions returns the transport options for the tranport |
| 66 | + * mechanism itself |
| 67 | + */ |
| 68 | + protected function getTransportOptions() |
| 69 | + { |
| 70 | + return $this->tty ? ['-t'] : []; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * getCommandToExecute processes the arguments for the command to |
| 75 | + * be executed such that they are appropriate for the transport mechanism. |
| 76 | + */ |
| 77 | + protected function getCommandToExecute($args) |
| 78 | + { |
| 79 | + // Escape each argument for the target system and then join |
| 80 | + $args = Escape::argsForSite($this->siteAlias, $args); |
| 81 | + $commandToExecute = implode(' ', $args); |
| 82 | + |
| 83 | + return [$commandToExecute]; |
| 84 | + } |
| 85 | +} |
0 commit comments