Completed
Pull Request — master (#26)
by Greg
06:48
created

ProcessManager::createDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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