1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © Vaimo Group. All rights reserved. |
4
|
|
|
* See LICENSE_VAIMO.txt for license details. |
5
|
|
|
*/ |
6
|
|
|
namespace Vaimo\ComposerChangelogs\Repositories; |
7
|
|
|
|
8
|
|
|
use Vaimo\ComposerChangelogs\Exceptions\PackageResolverException; |
9
|
|
|
|
10
|
|
|
class PackageRepository |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Composer\Package\RootPackageInterface |
14
|
|
|
*/ |
15
|
|
|
private $rootPackage; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Composer\Repository\WritableRepositoryInterface |
19
|
|
|
*/ |
20
|
|
|
private $packageRepository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \Composer\Package\RootPackageInterface $rootPackage |
24
|
|
|
* @param \Composer\Repository\WritableRepositoryInterface $packageRepository |
25
|
|
|
*/ |
26
|
|
|
public function __construct( |
27
|
|
|
\Composer\Package\RootPackageInterface $rootPackage, |
28
|
|
|
\Composer\Repository\WritableRepositoryInterface $packageRepository |
29
|
|
|
) { |
30
|
|
|
$this->rootPackage = $rootPackage; |
31
|
|
|
$this->packageRepository = $packageRepository; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getByName($query) |
35
|
|
|
{ |
36
|
|
|
/** @var \Composer\Repository\RepositoryInterface[] $repositories */ |
37
|
|
|
$repositories = array( |
38
|
|
|
new \Composer\Repository\ArrayRepository(array($this->rootPackage)), |
39
|
|
|
$this->packageRepository |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$matchesGroups = array(); |
43
|
|
|
|
44
|
|
|
foreach ($repositories as $repositoryId => $item) { |
45
|
|
|
$matchesGroups[$repositoryId] = $item->search($query); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!array_filter($matchesGroups)) { |
49
|
|
|
throw new PackageResolverException( |
50
|
|
|
sprintf('No packages for query %s', $query) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$matches = array_reduce($matchesGroups, 'array_merge', array()); |
55
|
|
|
|
56
|
|
|
$exactMatches = array_filter($matches, function (array $match) use ($query) { |
57
|
|
|
return $match['name'] === $query; |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
if (!empty($exactMatches)) { |
61
|
|
|
$matches = $exactMatches; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (count($matches) > 1) { |
65
|
|
|
$exception = new PackageResolverException( |
66
|
|
|
sprintf('Multiple packages found for query %s:', $query) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$exception->setExtraInfo(array_map(function ($match) { |
70
|
|
|
return $match['name']; |
71
|
|
|
}, $matches)); |
72
|
|
|
|
73
|
|
|
throw $exception; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$repositoryKey = key(array_filter($matchesGroups)); |
77
|
|
|
|
78
|
|
|
$repository = $repositories[$repositoryKey]; |
79
|
|
|
$firstMatch = reset($matches); |
80
|
|
|
|
81
|
|
|
$package = $repository->findPackage($firstMatch['name'], '*'); |
82
|
|
|
|
83
|
|
|
if (!$package) { |
84
|
|
|
throw new PackageResolverException( |
85
|
|
|
sprintf('No packages for query %s', $query) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $package; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|