Chris@5
|
1 <?php
|
Chris@5
|
2
|
Chris@5
|
3 namespace Consolidation\SiteProcess\Transport;
|
Chris@5
|
4
|
Chris@5
|
5 use Consolidation\SiteProcess\SiteProcess;
|
Chris@5
|
6 use Consolidation\SiteAlias\SiteAliasInterface;
|
Chris@5
|
7 use Consolidation\SiteProcess\Util\Shell;
|
Chris@5
|
8 use Consolidation\Config\ConfigInterface;
|
Chris@5
|
9
|
Chris@5
|
10 /**
|
Chris@5
|
11 * DockerComposeTransport knows how to wrap a command such that it executes
|
Chris@5
|
12 * on a Docker Compose service.
|
Chris@5
|
13 */
|
Chris@5
|
14 class DockerComposeTransport implements TransportInterface
|
Chris@5
|
15 {
|
Chris@5
|
16 protected $tty;
|
Chris@5
|
17 protected $siteAlias;
|
Chris@5
|
18 protected $cd_remote;
|
Chris@5
|
19
|
Chris@5
|
20 public function __construct(SiteAliasInterface $siteAlias)
|
Chris@5
|
21 {
|
Chris@5
|
22 $this->siteAlias = $siteAlias;
|
Chris@5
|
23 }
|
Chris@5
|
24
|
Chris@5
|
25 /**
|
Chris@5
|
26 * @inheritdoc
|
Chris@5
|
27 */
|
Chris@5
|
28 public function configure(SiteProcess $process)
|
Chris@5
|
29 {
|
Chris@5
|
30 $this->tty = $process->isTty();
|
Chris@5
|
31 }
|
Chris@5
|
32
|
Chris@5
|
33 /**
|
Chris@5
|
34 * @inheritdoc
|
Chris@5
|
35 */
|
Chris@5
|
36 public function wrap($args)
|
Chris@5
|
37 {
|
Chris@5
|
38 $transport = $this->getTransport();
|
Chris@5
|
39 $transportOptions = $this->getTransportOptions();
|
Chris@5
|
40 $commandToExecute = $this->getCommandToExecute($args);
|
Chris@5
|
41
|
Chris@5
|
42 return array_merge(
|
Chris@5
|
43 $transport,
|
Chris@5
|
44 $transportOptions,
|
Chris@5
|
45 $commandToExecute
|
Chris@5
|
46 );
|
Chris@5
|
47 }
|
Chris@5
|
48
|
Chris@5
|
49 /**
|
Chris@5
|
50 * @inheritdoc
|
Chris@5
|
51 */
|
Chris@5
|
52 public function addChdir($cd, $args)
|
Chris@5
|
53 {
|
Chris@5
|
54 $this->cd_remote = $cd;
|
Chris@5
|
55 return $args;
|
Chris@5
|
56 }
|
Chris@5
|
57
|
Chris@5
|
58 /**
|
Chris@5
|
59 * getTransport returns the transport along with the docker-compose
|
Chris@5
|
60 * project in case it is defined.
|
Chris@5
|
61 */
|
Chris@5
|
62 protected function getTransport()
|
Chris@5
|
63 {
|
Chris@5
|
64 $transport = ['docker-compose'];
|
Chris@5
|
65 $project = $this->siteAlias->get('docker.project', '');
|
Chris@5
|
66 $options = $this->siteAlias->get('docker.compose.options', '');
|
Chris@5
|
67 if ($project && (strpos($options, '-p') === false || strpos($options, '--project') === false)) {
|
Chris@5
|
68 $transport = array_merge($transport, ['-p', $project]);
|
Chris@5
|
69 }
|
Chris@5
|
70 if ($options) {
|
Chris@5
|
71 $transport[] = Shell::preEscaped($options);
|
Chris@5
|
72 }
|
Chris@5
|
73 return array_merge($transport, ['exec']);
|
Chris@5
|
74 }
|
Chris@5
|
75
|
Chris@5
|
76 /**
|
Chris@5
|
77 * getTransportOptions returns the transport options for the tranport
|
Chris@5
|
78 * mechanism itself
|
Chris@5
|
79 */
|
Chris@5
|
80 protected function getTransportOptions()
|
Chris@5
|
81 {
|
Chris@5
|
82 $transportOptions = [
|
Chris@5
|
83 $this->siteAlias->get('docker.service', ''),
|
Chris@5
|
84 ];
|
Chris@5
|
85 if ($options = $this->siteAlias->get('docker.exec.options', '')) {
|
Chris@5
|
86 array_unshift($transportOptions, Shell::preEscaped($options));
|
Chris@5
|
87 }
|
Chris@5
|
88 if (!$this->tty) {
|
Chris@5
|
89 array_unshift($transportOptions, '-T');
|
Chris@5
|
90 }
|
Chris@5
|
91 if ($this->cd_remote) {
|
Chris@5
|
92 $transportOptions = array_merge(['--workdir', $this->cd_remote], $transportOptions);
|
Chris@5
|
93 }
|
Chris@5
|
94 return array_filter($transportOptions);
|
Chris@5
|
95 }
|
Chris@5
|
96
|
Chris@5
|
97 /**
|
Chris@5
|
98 * getCommandToExecute processes the arguments for the command to
|
Chris@5
|
99 * be executed such that they are appropriate for the transport mechanism.
|
Chris@5
|
100 *
|
Chris@5
|
101 * Nothing to do for this transport.
|
Chris@5
|
102 */
|
Chris@5
|
103 protected function getCommandToExecute($args)
|
Chris@5
|
104 {
|
Chris@5
|
105 return $args;
|
Chris@5
|
106 }
|
Chris@5
|
107 }
|