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\Scoper; |
||
18 | use InvalidArgumentException; |
||
19 | use stdClass; |
||
20 | use function array_map; |
||
21 | use function gettype; |
||
22 | use function is_array; |
||
23 | use function preg_match as native_preg_match; |
||
24 | use function Safe\json_decode; |
||
25 | use function Safe\json_encode; |
||
26 | use function Safe\sprintf; |
||
27 | use const JSON_PRETTY_PRINT; |
||
28 | 3 | use const JSON_THROW_ON_ERROR; |
|
29 | |||
30 | 3 | final class InstalledPackagesScoper implements Scoper |
|
31 | { |
||
32 | private const COMPOSER_INSTALLED_FILE_PATTERN = '/composer(\/|\\\\)installed\.json$/'; |
||
33 | |||
34 | private Scoper $decoratedScoper; |
||
35 | private AutoloadPrefixer $autoloadPrefixer; |
||
36 | |||
37 | public function __construct( |
||
38 | 3 | Scoper $decoratedScoper, |
|
39 | AutoloadPrefixer $autoloadPrefixer |
||
40 | 3 | ) { |
|
41 | 1 | $this->decoratedScoper = $decoratedScoper; |
|
42 | $this->autoloadPrefixer = $autoloadPrefixer; |
||
43 | } |
||
44 | 2 | ||
45 | /** |
||
46 | 2 | * Scopes PHP and JSON files related to Composer. |
|
47 | */ |
||
48 | 2 | public function scope(string $filePath, string $contents): string |
|
49 | 2 | { |
|
50 | 2 | if (1 !== native_preg_match(self::COMPOSER_INSTALLED_FILE_PATTERN, $filePath)) { |
|
51 | return $this->decoratedScoper->scope($filePath, $contents); |
||
52 | } |
||
53 | |||
54 | 2 | $decodedJson = self::decodeContents($contents); |
|
55 | |||
56 | 2 | if (!isset($decodedJson->packages) || !is_array($decodedJson->packages)) { |
|
57 | 2 | throw new InvalidArgumentException('Expected the decoded JSON to contain the list of installed packages'); |
|
58 | } |
||
59 | |||
60 | 2 | $decodedJson->packages = $this->prefixLockPackages($decodedJson->packages); |
|
61 | |||
62 | return json_encode( |
||
63 | $decodedJson, |
||
64 | JSON_PRETTY_PRINT, |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | private static function decodeContents(string $contents): stdClass |
||
69 | { |
||
70 | $decodedJson = json_decode($contents, false, 512, JSON_THROW_ON_ERROR); |
||
71 | |||
72 | if ($decodedJson instanceof stdClass) { |
||
73 | return $decodedJson; |
||
74 | } |
||
75 | |||
76 | throw new InvalidArgumentException( |
||
77 | sprintf( |
||
0 ignored issues
–
show
|
|||
78 | 'Expected the decoded JSON to be an stdClass instance, got "%s" instead', |
||
79 | gettype($decodedJson), |
||
80 | ), |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param array<string, stdClass> $packages |
||
86 | * |
||
87 | * @return array<string, stdClass> |
||
88 | */ |
||
89 | private function prefixLockPackages(array $packages): array |
||
90 | { |
||
91 | return array_map( |
||
92 | fn (stdClass $package) => $this->autoloadPrefixer->prefixPackageAutoloadStatements($package), |
||
93 | $packages, |
||
94 | ); |
||
95 | } |
||
96 | } |
||
97 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.