forked from LastCallMedia/ComposerExtraFiles
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDownloadsParser.php
80 lines (67 loc) · 2.48 KB
/
DownloadsParser.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
<?php
/*
* This file is part of Composer Extra Files Plugin.
*
* (c) 2017 Last Call Media, Rob Bayliss <rob@lastcallmedia.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace LastCall\DownloadsPlugin;
use Composer\Package\PackageInterface;
use LastCall\DownloadsPlugin\Handler\ArchiveHandler;
use LastCall\DownloadsPlugin\Handler\BaseHandler;
use LastCall\DownloadsPlugin\Handler\FileHandler;
use LastCall\DownloadsPlugin\Handler\PharHandler;
use LastCall\DownloadsPlugin\Handler\GitHandler;
class DownloadsParser
{
/**
* @param \Composer\Package\PackageInterface $package
*
* @return BaseHandler[]
* Each item is a specification of an extra file, with defaults and variables evaluated.
*/
public function parse(PackageInterface $package, $basePath)
{
$extraFiles = [];
$extra = $package->getExtra();
$defaults = isset($extra['downloads']['*']) ? $extra['downloads']['*'] : [];
if (!empty($extra['downloads'])) {
foreach ((array) $extra['downloads'] as $id => $extraFile) {
if ($id === '*') continue;
$extraFile = array_merge($defaults, $extraFile);
$extraFile['id'] = $id;
foreach (['url', 'path'] as $prop) {
if (isset($extraFile[$prop])) {
$extraFile[$prop] = strtr($extraFile[$prop], [
'{$id}' => $extraFile['id'],
'{$version}' => isset($extraFile['version']) ? $extraFile['version'] : '',
]);
}
}
$class = $this->pickClass($extraFile);
$extraFiles[] = new $class($package, $basePath, $extraFile);
}
}
return $extraFiles;
}
public function pickClass($extraFile)
{
$types = [
'archive' => ArchiveHandler::CLASS,
'file' => FileHandler::CLASS,
'phar' => PharHandler::CLASS,
'git' => GitHandler::CLASS,
];
if (isset($extraFile['type'], $types[$extraFile['type']])) {
return $types[$extraFile['type']];
}
$parts = parse_url($extraFile['url']);
$filename = pathinfo($parts['path'], PATHINFO_BASENAME);
if (preg_match('/\.(zip|tar\.gz|tgz)$/', $filename)) {
return $types['archive'];
}
return $types['file'];
}
}