Completed
Push — master ( 90653a...eed34b )
by Greg
01:26
created

ProcessManager::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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