|
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 SorterComponent implements \Vaimo\ComposerPatches\Interfaces\DefinitionListLoaderComponentInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Vaimo\ComposerPatches\Utils\FilterUtils |
|
14
|
|
|
*/ |
|
15
|
|
|
private $filterUtils; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->filterUtils = new \Vaimo\ComposerPatches\Utils\FilterUtils(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param array $patches |
|
24
|
|
|
* @param \Composer\Package\PackageInterface[] $packagesByName |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
public function process(array $patches, array $packagesByName) |
|
28
|
|
|
{ |
|
29
|
|
|
$sortKeys = array(PatchDefinition::BEFORE, PatchDefinition::AFTER); |
|
30
|
|
|
|
|
31
|
|
|
foreach ($patches as $patchTarget => $packagePatches) { |
|
32
|
|
|
foreach ($packagePatches as $patchPath => $patchInfo) { |
|
33
|
|
|
$otherPatches = array_diff(array_keys($packagePatches), array($patchPath)); |
|
34
|
|
|
|
|
35
|
|
|
if (!$otherPatches) { |
|
|
|
|
|
|
36
|
|
|
continue; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
foreach ($sortKeys as $sortKey) { |
|
40
|
|
|
if (!$patchInfo[$sortKey]) { |
|
41
|
|
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$filter = $this->filterUtils->composeRegex($patchInfo[$sortKey], '/'); |
|
45
|
|
|
|
|
46
|
|
|
$packagePatches[$patchPath][$sortKey] = preg_grep($filter, $otherPatches); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$patches[$patchTarget] = $this->sortPackagePatches($packagePatches); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $patches; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function sortPackagePatches($packagePatches) |
|
57
|
|
|
{ |
|
58
|
|
|
$patchDependencies = array_fill_keys(array_keys($packagePatches), array()); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($packagePatches as $patchPath => $patchInfo) { |
|
61
|
|
|
$patchDependencies[$patchPath] = array_merge( |
|
62
|
|
|
$patchDependencies[$patchPath], |
|
63
|
|
|
$patchInfo[PatchDefinition::AFTER] |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
foreach ($patchInfo[PatchDefinition::BEFORE] as $beforePath) { |
|
67
|
|
|
$patchDependencies[$beforePath][] = $patchPath; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (!array_filter($patchDependencies)) { |
|
72
|
|
|
return $packagePatches; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$sorter = new \MJS\TopSort\Implementations\StringSort(); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
foreach ($patchDependencies as $path => $depends) { |
|
78
|
|
|
$sorter->add($path, array_unique($depends)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return array_replace( |
|
82
|
|
|
array_flip($sorter->sort()), |
|
83
|
|
|
$packagePatches |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
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.