Completed
Push — master ( de0dd6...69ce7d )
by Greg
01:27
created

ProcessManager::addTransports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Consolidation\SiteProcess;
4
5
use Psr\Log\LoggerInterface;
6
use Consolidation\SiteAlias\SiteAliasInterface;
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
use Consolidation\Config\Config;
13
use Consolidation\Config\ConfigInterface;
14
use Consolidation\Config\ConfigAwareInterface;
15
use Consolidation\Config\ConfigAwareTrait;
16
use Consolidation\SiteAlias\SiteAliasWithConfig;
17
18
/**
19
 * ProcessManager will create a SiteProcess to run a command on a given
20
 * site as indicated by a SiteAlias.
21
 *
22
 * ProcessManager also manages a collection of transport factories, and
23
 * will produce transport instances as needed for provided site aliases.
24
 */
25
class ProcessManager implements ConfigAwareInterface
26
{
27
    use ConfigAwareTrait;
28
29
    /** @var ConfigInterface */
30
    protected $configRuntime;
31
32
    protected $transportFactories = [];
33
34
    public function __construct()
35
    {
36
        $this->config = new Config();
37
        $this->configRuntime = new Config();
38
    }
39
40
    /**
41
     * setConfigRuntime sets the config object that holds runtime config
42
     * items, i.e. config set from the commandline rather than a config file.
43
     * Configuration priority (highest to lowest) is:
44
     *   - config runtime
45
     *   - site alias
46
     *   - config files
47
     */
48
    public function setConfigRuntime(ConfigInterface $configRuntime)
49
    {
50
        $this->configRuntime = $configRuntime;
51
        return $this;
52
    }
53
54
    /**
55
     * createDefault creates a Transport manager and add the default transports to it.
56
     */
57
    public static function createDefault()
58
    {
59
        $processManager = new self();
60
        return static::addTransports($processManager);
61
    }
62
63
    /**
64
     * addTransports adds the avaiable transports to the
65
     * provided process manager.
66
     */
67
    public static function addTransports(ProcessManager $processManager)
68
    {
69
        $processManager->add(new SshTransportFactory());
70
        $processManager->add(new DockerComposeTransportFactory());
71
72
        return $processManager;
73
    }
74
75
    /**
76
     * Return a site process configured with an appropriate transport
77
     *
78
     * @param SiteAliasInterface $siteAlias Target for command
79
     * @param array $args Command arguments
80
     * @param array $options Associative array of command options
81
     * @param array $optionsPassedAsArgs Associtive array of options to be passed as arguments (after double-dash)
82
     * @return Process
83
     */
84
    public function siteProcess(SiteAliasInterface $siteAlias, $args = [], $options = [], $optionsPassedAsArgs = [])
85
    {
86
        $transport = $this->getTransport($siteAlias);
87
        $process = new SiteProcess($siteAlias, $transport, $args, $options, $optionsPassedAsArgs);
88
        return $process;
89
    }
90
91
    /**
92
     * Create a Process instance from a commandline string.
93
     * @param array $command The command to run and its arguments listed as separate entries
94
     * @param string|null $cwd     The working directory or null to use the working dir of the current PHP process
95
     * @param array|null $env     The environment variables or null to use the same environment as the current PHP process
96
     * @param mixed|null $input   The input as stream resource, scalar or \Traversable, or null for no input
97
     * @param int|float|null $timeout The timeout in seconds or null to disable
98
     * @return Process
99
     */
100
    public function process($command, $cwd = null, array $env = null, $input = null, $timeout = 60)
101
    {
102
        return new ProcessBase($command, $cwd, $env, $input, $timeout);
103
    }
104
105
    /**
106
     * Create a Process instance from a commandline string.
107
     * @param string $command The commandline string to run
108
     * @param string|null $cwd     The working directory or null to use the working dir of the current PHP process
109
     * @param array|null $env     The environment variables or null to use the same environment as the current PHP process
110
     * @param mixed|null $input   The input as stream resource, scalar or \Traversable, or null for no input
111
     * @param int|float|null $timeout The timeout in seconds or null to disable
112
     * @return Process
113
     */
114
    public function shell($command, $cwd = null, array $env = null, $input = null, $timeout = 60)
115
    {
116
        return ProcessBase::fromShellCommandline($command, $cwd, $env, $input, $timeout);
117
    }
118
119
    /**
120
     * add a transport factory to our factory list
121
     * @param TransportFactoryInterface $factory
122
     */
123
    public function add(TransportFactoryInterface $factory)
124
    {
125
        $this->transportFactories[] = $factory;
126
        return $this;
127
    }
128
129
    /**
130
     * hasTransport determines if there is a transport that handles the
131
     * provided site alias.
132
     *
133
     * @param SiteAliasInterface $siteAlias
134
     * @return boolean
135
     */
136
    public function hasTransport(SiteAliasInterface $siteAlias)
137
    {
138
        return $this->getTransportFactory($siteAlias) !== null;
139
    }
140
141
    /**
142
     * getTransport returns a transport that is applicable to the provided site alias.
143
     *
144
     * @param SiteAliasInterface $siteAlias
145
     * @return TransportInterface
146
     */
147
    public function getTransport(SiteAliasInterface $siteAlias)
148
    {
149
        $factory = $this->getTransportFactory($siteAlias);
150
151
        $siteAliasWithConfig = SiteAliasWithConfig::create($siteAlias, $this->config, $this->configRuntime);
152
153
        if ($factory) {
154
            return $factory->create($siteAliasWithConfig);
155
        }
156
        return new LocalTransport();
157
    }
158
159
    /**
160
     * getTransportFactory returns a factory for the provided site alias.
161
     *
162
     * @param SiteAliasInterface $siteAlias
163
     * @return TransportFactoryInterface
164
     */
165
    protected function getTransportFactory(SiteAliasInterface $siteAlias)
166
    {
167
        foreach ($this->transportFactories as $factory) {
168
            if ($factory->check($siteAlias)) {
169
                return $factory;
170
            }
171
        }
172
        return null;
173
    }
174
}
175