|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright © Vaimo Group. All rights reserved. |
|
4
|
|
|
* See LICENSE_VAIMO.txt for license details. |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Vaimo\ComposerPatches\Patch\DefinitionList\LoaderComponents; |
|
7
|
|
|
|
|
8
|
|
|
use Vaimo\ComposerPatches\Patch\Definition as PatchDefinition; |
|
9
|
|
|
|
|
10
|
|
|
class TargetsResolverComponent implements \Vaimo\ComposerPatches\Interfaces\DefinitionListLoaderComponentInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Vaimo\ComposerPatches\Package\InfoResolver |
|
14
|
|
|
*/ |
|
15
|
|
|
private $packageInfoResolver; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Vaimo\ComposerPatches\Patch\File\Loader |
|
19
|
|
|
*/ |
|
20
|
|
|
private $patchFileLoader; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Vaimo\ComposerPatches\Patch\File\Analyser |
|
24
|
|
|
*/ |
|
25
|
|
|
private $patchFileAnalyser; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
private $gracefulMode; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Vaimo\ComposerPatches\Package\InfoResolver $packageInfoResolver |
|
36
|
|
|
* @param bool $gracefulMode |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
\Vaimo\ComposerPatches\Package\InfoResolver $packageInfoResolver, |
|
40
|
|
|
$gracefulMode = false |
|
41
|
|
|
) { |
|
42
|
|
|
$this->packageInfoResolver = $packageInfoResolver; |
|
43
|
|
|
$this->gracefulMode = $gracefulMode; |
|
44
|
|
|
|
|
45
|
|
|
$this->patchFileLoader = new \Vaimo\ComposerPatches\Patch\File\Loader(); |
|
46
|
|
|
$this->patchFileAnalyser = new \Vaimo\ComposerPatches\Patch\File\Analyser(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $patches |
|
51
|
|
|
* @param \Composer\Package\PackageInterface[] $packagesByName |
|
52
|
|
|
* @return array |
|
53
|
|
|
* @throws \Vaimo\ComposerPatches\Exceptions\LoaderException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function process(array $patches, array $packagesByName) |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($patches as $patchTarget => $packagePatches) { |
|
58
|
|
|
$targets = $this->collectTargets($packagesByName, $packagePatches); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($targets as $index => $items) { |
|
61
|
|
|
$patches[$patchTarget][$index][PatchDefinition::TARGETS] = $items; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $patches; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function collectTargets($packagesByName, $packagePatches) |
|
69
|
|
|
{ |
|
70
|
|
|
$result = array(); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($packagePatches as $index => $info) { |
|
73
|
|
|
$targets = isset($info[PatchDefinition::TARGETS]) |
|
74
|
|
|
? $info[PatchDefinition::TARGETS] |
|
75
|
|
|
: array(); |
|
76
|
|
|
|
|
77
|
|
|
if (!in_array(PatchDefinition::BUNDLE_TARGET, $targets, true)) { |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (count($targets) > 1) { |
|
82
|
|
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$path = $info[PatchDefinition::PATH]; |
|
86
|
|
|
$source = $info[PatchDefinition::SOURCE]; |
|
87
|
|
|
|
|
88
|
|
|
if (!file_exists($path)) { |
|
89
|
|
|
throw $this->createError('patch file not found', $source); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$paths = $this->patchFileAnalyser->getAllPaths( |
|
93
|
|
|
$this->patchFileLoader->loadWithNormalizedLineEndings($path) |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
$bundleTargets = $this->packageInfoResolver->resolveNamesFromPaths($packagesByName, $paths); |
|
97
|
|
|
|
|
98
|
|
|
if (!$bundleTargets && !$this->gracefulMode) { |
|
99
|
|
|
throw $this->createError('zero matches', $source); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$result[$index] = array_unique($bundleTargets); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $result; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function createError($reason, $source) |
|
109
|
|
|
{ |
|
110
|
|
|
return new \Vaimo\ComposerPatches\Exceptions\LoaderException( |
|
111
|
|
|
sprintf('Could not resolve targets (%s): %s ', $reason, $source) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|