1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the teamneusta/hosts project. |
4
|
|
|
* Copyright (c) 2017 neusta GmbH | Ein team neusta Unternehmen |
5
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code. |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace TeamNeusta\Hosts\Services; |
11
|
|
|
|
12
|
|
|
use TeamNeusta\Hosts\Services\Provider\Filesystem; |
13
|
|
|
|
14
|
|
|
class HostService |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Filesystem |
18
|
|
|
*/ |
19
|
|
|
private $fs; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* scope to interact with. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $_scope = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* HostService constructor. |
30
|
|
|
*/ |
31
|
5 |
|
public function __construct(Filesystem $fs = null) |
32
|
|
|
{ |
33
|
5 |
|
$this->fs = $fs ?? new Filesystem(); |
34
|
5 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add given configuration to .magehost file. |
38
|
|
|
* |
39
|
|
|
* @param $name |
40
|
|
|
* @param $host |
41
|
|
|
* @param $user |
42
|
|
|
* @param int $port |
43
|
|
|
*/ |
44
|
1 |
|
public function update($name, $host, $user, $port = 22) |
45
|
|
|
{ |
46
|
|
|
$config = [ |
47
|
1 |
|
'name' => $name, |
48
|
1 |
|
'host' => $host, |
49
|
1 |
|
'user' => $user, |
50
|
1 |
|
'port' => $port |
51
|
|
|
]; |
52
|
|
|
|
53
|
1 |
|
$this->fs->addHostToConfiguration($config, $this->_scope); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
5 |
|
public function getHosts($scope) : array |
58
|
|
|
{ |
59
|
|
|
switch ($scope) { |
60
|
5 |
|
case 'local': |
|
|
|
|
61
|
3 |
|
$config = $this->fs->getLocalConfiguration(); |
62
|
3 |
|
break; |
63
|
2 |
|
case 'project': |
64
|
1 |
|
$config = $this->fs->getProjectConfiguration(); |
65
|
1 |
|
break; |
66
|
|
|
default: |
67
|
1 |
|
$local = $this->fs->getLocalConfiguration(); |
68
|
1 |
|
$project = $this->fs->getProjectConfiguration(); |
69
|
1 |
|
$global = $this->fs->getGlobalConfiguration(); // temporary disabled. |
70
|
1 |
|
$config = $this->mergeConfigs($local, $project, $global); |
71
|
1 |
|
break; |
72
|
|
|
} |
73
|
5 |
|
return $config['hosts']; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
private function mergeConfigs() : array |
77
|
|
|
{ |
78
|
1 |
|
$args = func_get_args(); |
79
|
1 |
|
$config = ['hosts' => []]; |
80
|
1 |
|
foreach ($args as $hosts) { |
81
|
1 |
|
if (isset($hosts['hosts'])) { |
82
|
1 |
|
foreach ($hosts['hosts'] as $entry) { |
83
|
1 |
|
$config['hosts'][] = $entry; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
1 |
|
return $config; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function getHostsForQuestionhelper() : array |
91
|
|
|
{ |
92
|
1 |
|
$config = $this->getHosts($this->_scope); |
93
|
1 |
|
$hosts = []; |
94
|
1 |
|
foreach ($config as $host) { |
95
|
1 |
|
$hosts[] = $host['name']; |
96
|
|
|
} |
97
|
1 |
|
return $hosts; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function getConnectionStringByName($host) : string |
101
|
|
|
{ |
102
|
1 |
|
$config = $this->getHosts($this->_scope); |
103
|
|
|
|
104
|
1 |
|
$hostKey = array_search($host, array_column($config, 'name')); |
105
|
|
|
|
106
|
1 |
|
$sshCommand = $config[$hostKey]['user'] . '@' . $config[$hostKey]['host'] . ' -p ' . $config[$hostKey]['port']; |
107
|
|
|
|
108
|
1 |
|
return $sshCommand; |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
public function setScope($scope) |
112
|
|
|
{ |
113
|
5 |
|
$this->_scope = $scope; |
114
|
|
|
} |
115
|
|
|
} |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.