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 gettype; |
21
|
|
|
use function preg_match as native_preg_match; |
22
|
|
|
use function Safe\json_decode; |
23
|
|
|
use function Safe\json_encode; |
24
|
|
|
use function Safe\sprintf; |
25
|
|
|
use const JSON_PRETTY_PRINT; |
26
|
|
|
use const JSON_THROW_ON_ERROR; |
27
|
|
|
|
28
|
|
|
final class JsonFileScoper implements Scoper |
29
|
|
|
{ |
30
|
10 |
|
private Scoper $decoratedScoper; |
31
|
|
|
private AutoloadPrefixer $autoloadPrefixer; |
32
|
10 |
|
|
33
|
|
|
public function __construct( |
34
|
|
|
Scoper $decoratedScoper, |
35
|
|
|
AutoloadPrefixer $autoloadPrefixer |
36
|
|
|
) { |
37
|
|
|
$this->decoratedScoper = $decoratedScoper; |
38
|
|
|
$this->autoloadPrefixer = $autoloadPrefixer; |
39
|
|
|
} |
40
|
10 |
|
|
41
|
|
|
/** |
42
|
10 |
|
* Scopes PHP and JSON files related to Composer. |
43
|
1 |
|
*/ |
44
|
|
|
public function scope(string $filePath, string $contents): string |
45
|
|
|
{ |
46
|
9 |
|
if (1 !== native_preg_match('/composer\.json$/', $filePath)) { |
47
|
|
|
return $this->decoratedScoper->scope($filePath, $contents); |
48
|
9 |
|
} |
49
|
|
|
|
50
|
|
|
$decodedJson = self::decodeContents($contents); |
51
|
|
|
|
52
|
|
|
$decodedJson = $this->autoloadPrefixer->prefixPackageAutoloadStatements($decodedJson); |
53
|
|
|
|
54
|
|
|
return json_encode( |
55
|
|
|
$decodedJson, |
56
|
|
|
JSON_PRETTY_PRINT, |
57
|
9 |
|
); |
58
|
|
|
} |
59
|
9 |
|
|
60
|
9 |
|
private static function decodeContents(string $contents): stdClass |
61
|
9 |
|
{ |
62
|
|
|
$decodedJson = json_decode($contents, false, 512, JSON_THROW_ON_ERROR); |
63
|
|
|
|
64
|
|
|
if ($decodedJson instanceof stdClass) { |
65
|
|
|
return $decodedJson; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
throw new InvalidArgumentException( |
69
|
|
|
sprintf( |
|
|
|
|
70
|
|
|
'Expected the decoded JSON to be an stdClass instance, got "%s" instead', |
71
|
|
|
gettype($decodedJson), |
72
|
|
|
), |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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.