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