-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathDistConfig.php
165 lines (138 loc) · 4.23 KB
/
DistConfig.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
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Packeton\Service;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
class DistConfig
{
public const HOSTNAME_PLACEHOLDER = '__host_unset__';
public const FLAG_MIRROR = 'mirror';
public const FLAG_REPLACE = 'replace';
/**
* @param RouterInterface $router
* @param array $config
*/
public function __construct(
private readonly RouterInterface $router,
#[Autowire('%packeton_archive_opts%')]
private readonly array $config
) {
}
public function generateTargetDir(string $name)
{
$intermediatePath = \preg_replace('#[^a-z0-9-_/]#i', '-', $name);
return \sprintf('%s/%s', $this->config['basedir'], $intermediatePath);
}
public function buildName(string $packageName, string $reference, string $version): string
{
$intermediatePath = \preg_replace('#[^a-z0-9-_/]#i', '-', $packageName);
$filename = str_replace('/', '-', $version . '-' . $reference) . '.' . $this->getArchiveFormat();
return $intermediatePath . '/' . $filename;
}
public function resolvePath(?string $keyName = null): string
{
if ($dir = $this->getDistDir()) {
return $dir . '/' . $keyName;
}
throw new \InvalidArgumentException('archive_options[basedir] option can not be empty');
}
/**
* @return string|null
*/
public function getDistDir(): ?string
{
return $this->config['basedir'] ?? null;
}
/**
* @return string
*/
public function getArchiveFormat(): string
{
return $this->config['format'] ?? 'zip';
}
/**
* @param string $name
* @param string $reference
* @param string $version
*
* @return string
*/
public function generateDistFileName(string $name, string $reference, string $version): string
{
$targetDir = $this->generateTargetDir($name);
$fileName = $this->getFileName($reference, $version);
return $targetDir . '/' . $fileName . '.' . $this->getArchiveFormat();
}
/**
* @param string $reference
* @param string $version
* @return string
*/
public function getFileName(string $reference, string $version): string
{
$fileName = $version . '-' . $reference;
return str_replace('/', '-', $fileName);
}
/**
* @param string $fileName
* @return string
*/
public function guessesVersion(string $fileName)
{
$fileName = explode('-', $fileName);
$pathCount = count($fileName);
if ($pathCount > 1) {
unset($fileName[$pathCount - 1]);
}
$fileName = implode('-', $fileName);
return preg_replace('/(ticket|feature|fix)-/i', '$1/', $fileName);
}
/**
* @return bool
*/
public function isIncludeArchiveChecksum(): bool
{
return $this->config['include_archive_checksum'] ?? false;
}
/**
* Generate link to download from dist
*
* @param string $name
* @param string $reference
* @param string $format
*
* @return string
*/
public function generateRoute(string $name, string $reference, ?string $format = null): string
{
$hostName = ($this->config['endpoint'] ?? null) ? rtrim($this->config['endpoint'], '/') : self::HOSTNAME_PLACEHOLDER;
$format ??= '.' . $this->getArchiveFormat();
if ($format && !str_starts_with($format, '.')) {
$format = '.' . $format;
}
$uri = $this->router->generate(
'download_dist_package',
['package' => $name, 'hash' => $reference . $format]
);
return $hostName . $uri;
}
public function archiveEnabled(): bool
{
return !empty($this->config['flags']);
}
public function mirrorEnabled(): bool
{
return in_array(self::FLAG_MIRROR, $this->config['flags'] ?? []);
}
public function replaceEnabled(): bool
{
return in_array(self::FLAG_REPLACE, $this->config['flags'] ?? []);
}
/**
* @return bool
*/
public function isPreBuild(): bool
{
return $this->config['prebuild_zipball'] ?? false;
}
}