Completed
Push — master ( 7e7633...c1baaa )
by Greg
01:22
created

ProcessManager::setConfigRuntime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
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\AliasRecordInterface;
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
61
        $processManager->add(new SshTransportFactory());
62
        $processManager->add(new DockerComposeTransportFactory());
63
64
        return $processManager;
65
    }
66
67
    /**
68
     * Return a site process configured with an appropriate transport
69
     *
70
     * @param AliasRecordInterface $siteAlias Target for command
71
     * @param array $args Command arguments
72
     * @param array $options Associative array of command options
73
     * @param array $optionsPassedAsArgs Associtive array of options to be passed as arguments (after double-dash)
74
     * @return Process
75
     */
76
    public function siteProcess(AliasRecordInterface $siteAlias, $args = [], $options = [], $optionsPassedAsArgs = [])
77
    {
78
        $transport = $this->getTransport($siteAlias);
79
        $process = new SiteProcess($siteAlias, $transport, $args, $options, $optionsPassedAsArgs);
80
        return $process;
81
    }
82
83
    /**
84
     * Create a Process instance from a commandline string.
85
     * @param array $command The command to run and its arguments listed as separate entries
86
     * @param string|null $cwd     The working directory or null to use the working dir of the current PHP process
87
     * @param array|null $env     The environment variables or null to use the same environment as the current PHP process
88
     * @param mixed|null $input   The input as stream resource, scalar or \Traversable, or null for no input
89
     * @param int|float|null $timeout The timeout in seconds or null to disable
90
     * @return Process
91
     */
92
    public function process($command, $cwd = null, array $env = null, $input = null, $timeout = 60)
93
    {
94
        return new ProcessBase($command, $cwd, $env, $input, $timeout);
95
    }
96
97
    /**
98
     * Create a Process instance from a commandline string.
99
     * @param string $command The commandline string to run
100
     * @param string|null $cwd     The working directory or null to use the working dir of the current PHP process
101
     * @param array|null $env     The environment variables or null to use the same environment as the current PHP process
102
     * @param mixed|null $input   The input as stream resource, scalar or \Traversable, or null for no input
103
     * @param int|float|null $timeout The timeout in seconds or null to disable
104
     * @return Process
105
     */
106
    public function shell($command, $cwd = null, array $env = null, $input = null, $timeout = 60)
107
    {
108
        return ProcessBase::fromShellCommandline($command, $cwd, $env, $input, $timeout);
109
    }
110
111
    /**
112
     * add a transport factory to our factory list
113
     * @param TransportFactoryInterface $factory
114
     */
115
    public function add(TransportFactoryInterface $factory)
116
    {
117
        $this->transportFactories[] = $factory;
118
        return $this;
119
    }
120
121
    /**
122
     * hasTransport determines if there is a transport that handles the
123
     * provided site alias.
124
     *
125
     * @param AliasRecordInterface $siteAlias
126
     * @return boolean
127
     */
128
    public function hasTransport(AliasRecordInterface $siteAlias)
129
    {
130
        return $this->getTransportFactory($siteAlias) !== null;
131
    }
132
133
    /**
134
     * getTransport returns a transport that is applicable to the provided site alias.
135
     *
136
     * @param AliasRecordInterface $siteAlias
137
     * @return TransportInterface
138
     */
139
    public function getTransport(AliasRecordInterface $siteAlias)
140
    {
141
        $factory = $this->getTransportFactory($siteAlias);
142
143
        $siteAliasWithConfig = new SiteAliasWithConfig($this->configRuntime, $siteAlias, $this->config);
144
145
        if ($factory) {
146
            return $factory->create($siteAliasWithConfig);
147
        }
148
        return new LocalTransport();
149
    }
150
151
    /**
152
     * getTransportFactory returns a factory for the provided site alias.
153
     *
154
     * @param AliasRecordInterface $siteAlias
155
     * @return TransportFactoryInterface
156
     */
157
    protected function getTransportFactory(AliasRecordInterface $siteAlias)
158
    {
159
        foreach ($this->transportFactories as $factory) {
160
            if ($factory->check($siteAlias)) {
161
                return $factory;
162
            }
163
        }
164
        return null;
165
    }
166
}
167