|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CRM library |
|
4
|
|
|
* @author Tao <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Slince\Crm; |
|
7
|
|
|
|
|
8
|
|
|
use Slince\Crm\Exception\InvalidArgumentException; |
|
9
|
|
|
use Slince\Crm\Exception\RegistryNotExistsException; |
|
10
|
|
|
use Slince\Crm\Exception\RuntimeException; |
|
11
|
|
|
|
|
12
|
|
|
class RegistryManager |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* registry collection |
|
16
|
|
|
* @var RegistryCollection |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $registries; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->registries = new RegistryCollection(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Read repositories from file |
|
27
|
|
|
* @param $file |
|
28
|
|
|
*/ |
|
29
|
|
|
public function readRegistriesFromFile($file) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->registries = RegistryCollection::fromArray(Utils::readJsonFile($file)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Dump all registries to file |
|
36
|
|
|
* @param $file |
|
37
|
|
|
*/ |
|
38
|
|
|
public function dumpRepositoriesToFile($file) |
|
39
|
|
|
{ |
|
40
|
|
|
$registries = $this->registries->toArray(); |
|
41
|
|
|
Utils::getFilesystem()->dumpFile($file, json_encode($registries, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return RegistryCollection |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getRegistries() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->registries; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Add one registry |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
* @param string $url |
|
56
|
|
|
* @throws InvalidArgumentException |
|
57
|
|
|
* @return Registry |
|
58
|
|
|
*/ |
|
59
|
|
|
public function addRegistry($name, $url) |
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
$this->findRegistry($name); |
|
63
|
|
|
throw new InvalidArgumentException(sprintf("Registry [%s] already exists", $name)); |
|
64
|
|
|
} catch (RegistryNotExistsException $exception) { |
|
65
|
|
|
$registry = Registry::create([ |
|
66
|
|
|
'name' => $name, |
|
67
|
|
|
'url' => $url |
|
68
|
|
|
]); |
|
69
|
|
|
$this->registries->add($registry); |
|
70
|
|
|
} |
|
71
|
|
|
return $registry; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Remove registry |
|
76
|
|
|
* @param string $name |
|
77
|
|
|
*/ |
|
78
|
|
|
public function removeRegistry($name) |
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
|
|
$registry = $this->findRegistry($name); |
|
82
|
|
|
$this->registries->remove($registry); |
|
83
|
|
|
} catch (RegistryNotExistsException $exception) {} |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Find registry |
|
88
|
|
|
* @param $name |
|
89
|
|
|
* @return Registry |
|
90
|
|
|
* @throws RegistryNotExistsException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function findRegistry($name) |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($this->registries as $registry) { |
|
95
|
|
|
if (strcasecmp($name, $registry->getName()) == 0) { |
|
96
|
|
|
return $registry; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
throw new RegistryNotExistsException($name); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Use Registry |
|
104
|
|
|
* @param Registry $registry |
|
105
|
|
|
* @param boolean $isCurrent |
|
106
|
|
|
* @return void |
|
107
|
|
|
* @codeCoverageIgnore |
|
108
|
|
|
*/ |
|
109
|
|
|
public function useRegistry(Registry $registry, $isCurrent = false) |
|
110
|
|
|
{ |
|
111
|
|
|
$command = $this->makeUseRegistryCommand($registry, $isCurrent); |
|
112
|
|
|
$this->runSystemCommand($command); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get Current Registry |
|
117
|
|
|
* @param boolean $isCurrent |
|
118
|
|
|
* @throws RuntimeException |
|
119
|
|
|
* @return Registry |
|
120
|
|
|
* @codeCoverageIgnore |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getCurrentRegistry($isCurrent = false) |
|
123
|
|
|
{ |
|
124
|
|
|
$rawOutput = $this->runSystemCommand($isCurrent |
|
125
|
|
|
? "composer config repo.packagist.org" |
|
126
|
|
|
: "composer config -g repo.packagist.org"); |
|
127
|
|
|
$registryData = json_decode($rawOutput, true); |
|
128
|
|
|
if (json_last_error()) { |
|
129
|
|
|
throw new RuntimeException(sprintf("Can not find current registry, error: %s", json_last_error_msg())); |
|
130
|
|
|
} |
|
131
|
|
|
foreach ($this->registries as $registry) { |
|
132
|
|
|
if (strcasecmp($registry->getUrl(), $registryData['url']) == 0) { |
|
133
|
|
|
return $registry; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
return Registry::create([ |
|
137
|
|
|
'url' => $registryData['url'], |
|
138
|
|
|
'name' => 'unknown' |
|
139
|
|
|
]); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Make change registry command string |
|
144
|
|
|
* @param Registry $registry |
|
145
|
|
|
* @param boolean $isCurrent |
|
146
|
|
|
* @return string |
|
147
|
|
|
* @codeCoverageIgnore |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function makeUseRegistryCommand(Registry $registry, $isCurrent) |
|
150
|
|
|
{ |
|
151
|
|
|
$command = $isCurrent |
|
152
|
|
|
? "composer config repo.packagist composer %s" |
|
153
|
|
|
: "composer config -g repo.packagist composer %s"; |
|
154
|
|
|
return sprintf($command, $registry->getUrl()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Run command |
|
159
|
|
|
* @param string $command |
|
160
|
|
|
* @throws RuntimeException |
|
161
|
|
|
* @return string |
|
162
|
|
|
* @codeCoverageIgnore |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function runSystemCommand($command) |
|
165
|
|
|
{ |
|
166
|
|
|
$return = exec($command, $output, $returnVar); |
|
167
|
|
|
if ($returnVar !== 0) { |
|
168
|
|
|
throw new RuntimeException(sprintf('Execute the command "%s" failed, "composer" must be set for crm to run correctly', $command)); |
|
169
|
|
|
} |
|
170
|
|
|
return $return; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|