1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the humbug/php-scoper package. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
9
|
|
|
* Pádraic Brady <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\Scoper\Composer; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Scoper; |
18
|
|
|
use Humbug\PhpScoper\Whitelist; |
19
|
|
|
use InvalidArgumentException; |
20
|
|
|
use stdClass; |
21
|
|
|
use function array_map; |
22
|
|
|
use function gettype; |
23
|
|
|
use function is_array; |
24
|
|
|
use function preg_match as native_preg_match; |
25
|
|
|
use function Safe\json_decode; |
26
|
|
|
use function Safe\json_encode; |
27
|
|
|
use function Safe\sprintf; |
28
|
3 |
|
use const JSON_PRETTY_PRINT; |
29
|
|
|
use const JSON_THROW_ON_ERROR; |
30
|
3 |
|
|
31
|
|
|
final class InstalledPackagesScoper implements Scoper |
32
|
|
|
{ |
33
|
|
|
private static string $filePattern = '/composer(\/|\\\\)installed\.json$/'; |
34
|
|
|
|
35
|
|
|
private Scoper $decoratedScoper; |
36
|
|
|
|
37
|
|
|
public function __construct(Scoper $decoratedScoper) |
38
|
3 |
|
{ |
39
|
|
|
$this->decoratedScoper = $decoratedScoper; |
40
|
3 |
|
} |
41
|
1 |
|
|
42
|
|
|
/** |
43
|
|
|
* Scopes PHP and JSON files related to Composer. |
44
|
2 |
|
*/ |
45
|
|
|
public function scope(string $filePath, string $contents, string $prefix, array $patchers, Whitelist $whitelist): string |
46
|
2 |
|
{ |
47
|
|
|
if (1 !== native_preg_match(self::$filePattern, $filePath)) { |
48
|
2 |
|
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist); |
49
|
2 |
|
} |
50
|
2 |
|
|
51
|
|
|
$decodedJson = self::decodeContents($contents); |
52
|
|
|
|
53
|
|
|
if (!isset($decodedJson->packages) || !is_array($decodedJson->packages)) { |
54
|
2 |
|
throw new InvalidArgumentException('Expected the decoded JSON to contain the list of installed packages'); |
55
|
|
|
} |
56
|
2 |
|
|
57
|
2 |
|
$decodedJson->packages = self::prefixLockPackages( |
58
|
|
|
$decodedJson->packages, |
59
|
|
|
$prefix, |
60
|
2 |
|
$whitelist, |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return json_encode( |
64
|
|
|
$decodedJson, |
65
|
|
|
JSON_PRETTY_PRINT |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private static function decodeContents(string $contents): stdClass |
70
|
|
|
{ |
71
|
|
|
$decodedJson = json_decode($contents, false, 512, JSON_THROW_ON_ERROR); |
72
|
|
|
|
73
|
|
|
if ($decodedJson instanceof stdClass) { |
74
|
|
|
return $decodedJson; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new InvalidArgumentException( |
78
|
|
|
sprintf( |
79
|
|
|
'Expected the decoded JSON to be an stdClass instance, got "%s" instead', |
80
|
|
|
gettype($decodedJson), |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param array<string, stdClass> $packages |
87
|
|
|
* |
88
|
|
|
* @return array<string, stdClass> |
89
|
|
|
*/ |
90
|
|
|
private static function prefixLockPackages(array $packages, string $prefix, Whitelist $whitelist): array |
91
|
|
|
{ |
92
|
|
|
return array_map( |
93
|
|
|
static fn (stdClass $package) => AutoloadPrefixer::prefixPackageAutoloadStatements( |
94
|
|
|
$package, |
95
|
|
|
$prefix, |
96
|
|
|
$whitelist, |
97
|
|
|
), |
98
|
|
|
$packages, |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|