/
Config.php
159 lines (126 loc) · 4.47 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php declare(strict_types=1);
/*
* This file is part of Phive.
*
* Copyright (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de> and contributors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PharIo\Phive;
use function explode;
use function strpos;
use function substr;
use function trim;
use DateInterval;
use DateTimeImmutable;
use PharIo\FileSystem\Directory;
use PharIo\FileSystem\Filename;
use PharIo\Phive\Cli\Options;
class Config {
/** @var Environment */
private $environment;
/** @var Options */
private $cliOptions;
/** @var DateTimeImmutable */
private $now;
public function __construct(
Environment $environment,
Options $cliOptions,
?DateTimeImmutable $now = null
) {
$this->environment = $environment;
$this->cliOptions = $cliOptions;
if ($now === null) {
$now = new DateTimeImmutable();
}
$this->now = $now;
}
public function getHomeDirectory(): Directory {
if ($this->cliOptions->hasOption('home')) {
$dir = new Directory($this->cliOptions->getOption('home'));
$dir->ensureExists();
return $dir;
}
if ($this->environment->hasPhiveHomeVariable()) {
$dir = new Directory($this->environment->getPhiveHomeVariable());
$dir->ensureExists();
return $dir;
}
$dir = $this->environment->getHomeDirectory()->child('.phive');
$dir->ensureExists();
return $dir;
}
public function getRegistry(): Filename {
return $this->getHomeDirectory()->file('registry.xml');
}
public function getGlobalAuth(): Filename {
return $this->getHomeDirectory()->file('auth.xml');
}
public function getPharIoRepositories(): Filename {
return $this->getHomeDirectory()->file('repositories.xml');
}
public function getLocalRepositories(): Filename {
return $this->getHomeDirectory()->file('local.xml');
}
public function getPharsDirectory(): Directory {
return $this->getHomeDirectory()->child('phars');
}
public function getGPGDirectory(): Directory {
return $this->getHomeDirectory()->child('gpg');
}
public function getHttpCacheDirectory(): Directory {
return $this->getHomeDirectory()->child('http-cache');
}
public function getTemporaryWorkingDirectory(): Directory {
return $this->getHomeDirectory()->child('_tmp_wrk');
}
public function getGlobalInstallation(): Filename {
return $this->getHomeDirectory()->file('global.xml');
}
public function getWorkingDirectory(): Directory {
return $this->environment->getWorkingDirectory();
}
public function getProjectRepositories(): Filename {
return $this->getWorkingDirectory()->child('.phive')->file('repositories.xml');
}
public function getProjectAuth(): Filename {
return $this->getWorkingDirectory()->child('.phive')->file('auth.xml');
}
public function getProjectInstallation(): Filename {
return $this->getWorkingDirectory()->child('.phive')->file('phars.xml');
}
public function getToolsDirectory(): Directory {
return $this->getWorkingDirectory()->child('tools');
}
/**
* @throws NoGPGBinaryFoundException
*/
public function getGPGBinaryPath(): Filename {
try {
return $this->environment->getPathToCommand('gpg');
} catch (EnvironmentException $e) {
$message = "No executable gpg binary found. \n Either install gpg or enable the gnupg extension in PHP.";
throw new NoGPGBinaryFoundException($message);
}
}
public function getSourcesListUrl(): Url {
return new Url('https://phar.io/data/repositories.xml');
}
public function getTrusted(): TrustedCollection {
$trusted = new TrustedCollection();
if ($this->cliOptions->hasOption('trust-gpg-keys')) {
foreach (explode(',', $this->cliOptions->getOption('trust-gpg-keys')) as $id) {
$id = trim($id);
$trusted->add(
strpos($id, '0x') === 0 ? substr($id, 2) : $id
);
}
}
return $trusted;
}
public function getMaxAgeForSourcesList(): DateTimeImmutable {
return $this->now->sub(new DateInterval('P7D'));
}
}