1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consolidation\SiteProcess; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Consolidation\SiteAlias\AliasRecord; |
7
|
|
|
use Consolidation\SiteProcess\Factory\SshTransportFactory; |
8
|
|
|
use Consolidation\SiteProcess\Factory\DockerComposeTransportFactory; |
9
|
|
|
use Consolidation\SiteProcess\Factory\TransportFactoryInterface; |
10
|
|
|
use Consolidation\SiteProcess\Transport\LocalTransport; |
11
|
|
|
use Symfony\Component\Process\Process; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* TransportManager manages a collection of transport factories, and |
15
|
|
|
* will produce transport instances as needed for provided site aliases. |
16
|
|
|
*/ |
17
|
|
|
class TransportManager |
18
|
|
|
{ |
19
|
|
|
protected $transportFactories = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* createDefault creates a Transport manager and add the default transports to it. |
23
|
|
|
*/ |
24
|
|
|
public static function createDefault() |
25
|
|
|
{ |
26
|
|
|
$transportManager = new self(); |
27
|
|
|
|
28
|
|
|
$transportManager->add(new SshTransportFactory()); |
29
|
|
|
$transportManager->add(new DockerComposeTransportFactory()); |
30
|
|
|
|
31
|
|
|
return $transportManager; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return a site process configured with an appropriate transport |
36
|
|
|
* |
37
|
|
|
* @return Process |
38
|
|
|
*/ |
39
|
|
|
public function siteProcess(AliasRecord $siteAlias, $args = [], $options = [], $optionsPassedAsArgs = []) |
40
|
|
|
{ |
41
|
|
|
$transport = $this->getTransport($siteAlias); |
42
|
|
|
$process = new SiteProcess($siteAlias, $transport, $args, $options, $optionsPassedAsArgs); |
43
|
|
|
return $process; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* add a transport factory to our factory list |
48
|
|
|
* @param TransportFactoryInterface $factory |
49
|
|
|
*/ |
50
|
|
|
public function add(TransportFactoryInterface $factory) |
51
|
|
|
{ |
52
|
|
|
$this->transportFactories[] = $factory; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* hasTransport determines if there is a transport that handles the |
57
|
|
|
* provided site alias. |
58
|
|
|
*/ |
59
|
|
|
public function hasTransport(AliasRecord $siteAlias) |
60
|
|
|
{ |
61
|
|
|
return $this->getTransportFactory($siteAlias) !== false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* getTransport returns a transport that is applicable to the provided site alias. |
66
|
|
|
*/ |
67
|
|
|
public function getTransport(AliasRecord $siteAlias) |
68
|
|
|
{ |
69
|
|
|
$factory = $this->getTransportFactory($siteAlias); |
70
|
|
|
if ($factory) { |
71
|
|
|
return $factory->create($siteAlias); |
72
|
|
|
} |
73
|
|
|
return new LocalTransport(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* getTransportFactory returns a factory for the provided site alias. |
78
|
|
|
*/ |
79
|
|
|
protected function getTransportFactory(AliasRecord $siteAlias) |
80
|
|
|
{ |
81
|
|
|
foreach ($this->transportFactories as $factory) { |
82
|
|
|
if ($factory->check($siteAlias)) { |
83
|
|
|
return $factory; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|