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
|
|
|
* @var \Vaimo\ComposerChangelogs\Resolvers\RealPackageResolver |
24
|
|
|
*/ |
25
|
|
|
private $realPackageResolver; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Composer\Package\RootPackageInterface $rootPackage |
29
|
|
|
* @param \Composer\Repository\WritableRepositoryInterface $packageRepository |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
\Composer\Package\RootPackageInterface $rootPackage, |
33
|
|
|
\Composer\Repository\WritableRepositoryInterface $packageRepository |
34
|
|
|
) { |
35
|
|
|
$this->rootPackage = $rootPackage; |
36
|
|
|
$this->packageRepository = $packageRepository; |
37
|
|
|
|
38
|
|
|
$this->realPackageResolver = new \Vaimo\ComposerChangelogs\Resolvers\RealPackageResolver(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getByName($query) |
42
|
|
|
{ |
43
|
|
|
/** @var \Composer\Repository\RepositoryInterface[] $repositories */ |
44
|
|
|
$repositories = array( |
45
|
|
|
new \Composer\Repository\ArrayRepository(array($this->rootPackage)), |
46
|
|
|
$this->packageRepository |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$matchesGroups = array(); |
50
|
|
|
|
51
|
|
|
foreach ($repositories as $repositoryId => $item) { |
52
|
|
|
$matchesGroups[$repositoryId] = $item->search($query); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!array_filter($matchesGroups)) { |
56
|
|
|
throw new PackageResolverException( |
57
|
|
|
sprintf('No packages for query %s', $query) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$matches = array_reduce($matchesGroups, 'array_merge', array()); |
62
|
|
|
|
63
|
|
|
$exactMatches = array_filter($matches, function (array $match) use ($query) { |
64
|
|
|
return $match['name'] === $query; |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
if ($exactMatches) { |
|
|
|
|
68
|
|
|
$matches = $exactMatches; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (count($matches) > 1) { |
72
|
|
|
$exception = new PackageResolverException( |
73
|
|
|
sprintf('Multiple packages found for query %s:', $query) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$exception->setExtraInfo(array_map(function ($match) { |
77
|
|
|
return $match['name']; |
78
|
|
|
}, $matches)); |
79
|
|
|
|
80
|
|
|
throw $exception; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$repositoryKey = key(array_filter($matchesGroups)); |
84
|
|
|
|
85
|
|
|
$repository = $repositories[$repositoryKey]; |
86
|
|
|
$firstMatch = reset($matches); |
87
|
|
|
|
88
|
|
|
$package = $repository->findPackage($firstMatch['name'], '*'); |
89
|
|
|
|
90
|
|
|
if (!$package) { |
91
|
|
|
throw new PackageResolverException( |
92
|
|
|
sprintf('No packages for query %s', $query) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $package; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.