|
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
|
|
|
* ProcessManager will create a SiteProcess to run a command on a given |
|
15
|
|
|
* site as indicated by a SiteAlias. |
|
16
|
|
|
* |
|
17
|
|
|
* ProcessManager also manages a collection of transport factories, and |
|
18
|
|
|
* will produce transport instances as needed for provided site aliases. |
|
19
|
|
|
*/ |
|
20
|
|
|
class ProcessManager |
|
21
|
|
|
{ |
|
22
|
|
|
protected $transportFactories = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* createDefault creates a Transport manager and add the default transports to it. |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function createDefault() |
|
28
|
|
|
{ |
|
29
|
|
|
$processManager = new self(); |
|
30
|
|
|
|
|
31
|
|
|
$processManager->add(new SshTransportFactory()); |
|
32
|
|
|
$processManager->add(new DockerComposeTransportFactory()); |
|
33
|
|
|
|
|
34
|
|
|
return $processManager; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Return a site process configured with an appropriate transport |
|
39
|
|
|
* |
|
40
|
|
|
* @param AliasRecord $siteAlias Target for command |
|
41
|
|
|
* @param $args Command arguments |
|
42
|
|
|
* @param $options Associative array of command options |
|
43
|
|
|
* @param $optionsPassedAsArgs Associtive array of options to be passed as arguments (after double-dash) |
|
44
|
|
|
* @return Process |
|
45
|
|
|
*/ |
|
46
|
|
|
public function siteProcess(AliasRecord $siteAlias, $args = [], $options = [], $optionsPassedAsArgs = []) |
|
47
|
|
|
{ |
|
48
|
|
|
$transport = $this->getTransport($siteAlias); |
|
49
|
|
|
$process = new SiteProcess($siteAlias, $transport, $args, $options, $optionsPassedAsArgs); |
|
50
|
|
|
return $process; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* add a transport factory to our factory list |
|
55
|
|
|
* @param TransportFactoryInterface $factory |
|
56
|
|
|
*/ |
|
57
|
|
|
public function add(TransportFactoryInterface $factory) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->transportFactories[] = $factory; |
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* hasTransport determines if there is a transport that handles the |
|
65
|
|
|
* provided site alias. |
|
66
|
|
|
* |
|
67
|
|
|
* @param AliasRecord $siteAlias |
|
68
|
|
|
* @return boolean |
|
69
|
|
|
*/ |
|
70
|
|
|
public function hasTransport(AliasRecord $siteAlias) |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->getTransportFactory($siteAlias) !== false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* getTransport returns a transport that is applicable to the provided site alias. |
|
77
|
|
|
* |
|
78
|
|
|
* @param AliasRecord $siteAlias |
|
79
|
|
|
* @return TransportInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getTransport(AliasRecord $siteAlias) |
|
82
|
|
|
{ |
|
83
|
|
|
$factory = $this->getTransportFactory($siteAlias); |
|
84
|
|
|
if ($factory) { |
|
85
|
|
|
return $factory->create($siteAlias); |
|
86
|
|
|
} |
|
87
|
|
|
return new LocalTransport(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* getTransportFactory returns a factory for the provided site alias. |
|
92
|
|
|
* |
|
93
|
|
|
* @param AliasRecord $siteAlias |
|
94
|
|
|
* @return TransportFactoryInterface |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function getTransportFactory(AliasRecord $siteAlias) |
|
97
|
|
|
{ |
|
98
|
|
|
foreach ($this->transportFactories as $factory) { |
|
99
|
|
|
if ($factory->check($siteAlias)) { |
|
100
|
|
|
return $factory; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|