Completed
Push — master ( 209e5d...97f947 )
by Greg
01:23
created

ProcessManager::getTransportFactory()   A

Complexity

Conditions 3
Paths 3

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