forked from PUGX/badge-poser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackageManager.php
160 lines (137 loc) · 4.15 KB
/
PackageManager.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
<?php
namespace PUGX\BadgeBundle\Service;
use Packagist\Api\Client;
use Packagist\Api\Result\Package as ApiPackage;
use PUGX\BadgeBundle\Package\Package;
use PUGX\BadgeBundle\Package\PackageInterface;
use PUGX\BadgeBundle\Exception\UnexpectedValueException;
/**
* This class is intended to load ApiPackage, create, and work with the Package object
*
* Class PackageHandler
* @package PUGX\BadgeBundle\Service
*
*/
class PackageManager
{
private static $modifierRegex = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?';
private static $packageClass;
private $client;
public function __construct(Client $packagistClient, $packageClass = '\PUGX\BadgeBundle\Package\Package')
{
self::$packageClass = $packageClass;
$this->client = $packagistClient;
}
/**
* Factory of Packages.
*
* @return PackageInterface
*/
public static function instantiatePackage()
{
$package = self::$packageClass;
return new $package();
}
/**
* Create a new Package decorated with the Api Package.
*
* @param ApiPackage $apiPackage
*
* @return PackageInterface
*/
public function instantiateAndDecoratePackage(ApiPackage $apiPackage)
{
$package = self::instantiatePackage();
$package->setOriginalObject($apiPackage);
return $package;
}
/**
* Returns package if founded.
*
* @param string $repository
*
* @return Package
*
* @throws UnexpectedValueException
*/
public function fetchPackage($repository)
{
$package = $this->client->get($repository);
if ($package && $package instanceof ApiPackage) {
// create a new Package from the ApiPackage
return $this->instantiateAndDecoratePackage($package);
}
throw new UnexpectedValueException(sprintf('Impossible to found repository "%s"', $repository));
}
/**
* Set the latest Stable and the latest Unstable version from a Package.
*
* @param Package $package
*
* @return Package
*/
public function calculateLatestVersions(Package &$package)
{
$versions = $package->getVersions();
foreach ($versions as $version) {
$functionName = 'Unstable';
if ('stable' == $this->parseStability($version->getVersion())) {
$functionName = 'Stable';
}
if ($version->getVersion() > $package->{'getLatest' . $functionName . 'Version'}()) {
$package->{'setLatest' . $functionName . 'Version'}($version->getVersion());
}
}
return $package;
}
/**
* Returns the stability of a version.
*
* This function is part of Composer.
*
* (c) Nils Adermann <[email protected]>
* Jordi Boggiano <[email protected]>
*
* @param string $version
*
* @return string
*/
public function parseStability($version)
{
$version = preg_replace('{#.+$}i', '', $version);
if ('dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4)) {
return 'dev';
}
preg_match('{' . self::$modifierRegex . '$}i', strtolower($version), $match);
if (!empty($match[3])) {
return 'dev';
}
if (!empty($match[1])) {
if ('beta' === $match[1] || 'b' === $match[1]) {
return 'beta';
}
if ('alpha' === $match[1] || 'a' === $match[1]) {
return 'alpha';
}
if ('rc' === $match[1]) {
return 'RC';
}
}
return 'stable';
}
/**
* Take the Type of the Downloads (total, monthly or daily).
*
* @param Package $package
* @param string $type
*
* @return string
*/
public function getPackageDownloads(Package $package, $type)
{
$statsType = 'get' . ucfirst($type);
if ($package && ($download = $package->getDownloads()) && $download instanceof \Packagist\Api\Result\Package\Downloads) {
return $download->{$statsType}();
}
}
}