Completed
Push — master ( 4315bb...5b3451 )
by Greg
01:17
created

ProcessManager::setConfig()   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 1
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
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
    protected $transportFactories = [];
29
30
    public function __construct()
31
    {
32
        $this->config = new Config();
33
    }
34
35
    /**
36
     * createDefault creates a Transport manager and add the default transports to it.
37
     */
38
    public static function createDefault()
39
    {
40
        $processManager = new self();
41
42
        $processManager->add(new SshTransportFactory());
43
        $processManager->add(new DockerComposeTransportFactory());
44
45
        return $processManager;
46
    }
47
48
    /**
49
     * Return a site process configured with an appropriate transport
50
     *
51
     * @param AliasRecord $siteAlias Target for command
52
     * @param array $args Command arguments
53
     * @param array $options Associative array of command options
54
     * @param array $optionsPassedAsArgs Associtive array of options to be passed as arguments (after double-dash)
55
     * @return Process
56
     */
57
    public function siteProcess(AliasRecord $siteAlias, $args = [], $options = [], $optionsPassedAsArgs = [])
58
    {
59
        $transport = $this->getTransport($siteAlias);
60
        $process = new SiteProcess($siteAlias, $transport, $args, $options, $optionsPassedAsArgs);
61
        return $process;
62
    }
63
64
    /**
65
     * Create a Process instance from a commandline string.
66
     * @param array $command The command to run and its arguments listed as separate entries
67
     * @param string|null $cwd     The working directory or null to use the working dir of the current PHP process
68
     * @param array|null $env     The environment variables or null to use the same environment as the current PHP process
69
     * @param mixed|null $input   The input as stream resource, scalar or \Traversable, or null for no input
70
     * @param int|float|null $timeout The timeout in seconds or null to disable
71
     * @return Process
72
     */
73
    public function process($command, $cwd = null, array $env = null, $input = null, $timeout = 60)
74
    {
75
        return new ProcessBase($command, $cwd, $env, $input, $timeout);
76
    }
77
78
    /**
79
     * Create a Process instance from a commandline string.
80
     * @param string $command The commandline string to run
81
     * @param string|null $cwd     The working directory or null to use the working dir of the current PHP process
82
     * @param array|null $env     The environment variables or null to use the same environment as the current PHP process
83
     * @param mixed|null $input   The input as stream resource, scalar or \Traversable, or null for no input
84
     * @param int|float|null $timeout The timeout in seconds or null to disable
85
     * @return Process
86
     */
87
    public function shell($command, $cwd = null, array $env = null, $input = null, $timeout = 60)
88
    {
89
        return ProcessBase::fromShellCommandline($command, $cwd, $env, $input, $timeout);
90
    }
91
92
    /**
93
     * add a transport factory to our factory list
94
     * @param TransportFactoryInterface $factory
95
     */
96
    public function add(TransportFactoryInterface $factory)
97
    {
98
        $this->transportFactories[] = $factory;
99
        return $this;
100
    }
101
102
    /**
103
     * hasTransport determines if there is a transport that handles the
104
     * provided site alias.
105
     *
106
     * @param AliasRecord $siteAlias
107
     * @return boolean
108
     */
109
    public function hasTransport(AliasRecord $siteAlias)
110
    {
111
        return $this->getTransportFactory($siteAlias) !== null;
112
    }
113
114
    /**
115
     * getTransport returns a transport that is applicable to the provided site alias.
116
     *
117
     * @param AliasRecord $siteAlias
118
     * @return TransportInterface
119
     */
120
    public function getTransport(AliasRecord $siteAlias)
121
    {
122
        $factory = $this->getTransportFactory($siteAlias);
123
        if ($factory) {
124
            return $factory->create($siteAlias, $this->config);
125
        }
126
        return new LocalTransport();
127
    }
128
129
    /**
130
     * getTransportFactory returns a factory for the provided site alias.
131
     *
132
     * @param AliasRecord $siteAlias
133
     * @return TransportFactoryInterface
134
     */
135
    protected function getTransportFactory(AliasRecord $siteAlias)
136
    {
137
        foreach ($this->transportFactories as $factory) {
138
            if ($factory->check($siteAlias)) {
139
                return $factory;
140
            }
141
        }
142
        return null;
143
    }
144
}
145