Completed
Push — master ( 895a47...28affd )
by Greg
01:27
created

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