|
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; |
|
16
|
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Scoper; |
|
18
|
|
|
|
|
19
|
|
|
final class ComposerScoper implements Scoper |
|
20
|
|
|
{ |
|
21
|
|
|
private $decoratedScoper; |
|
22
|
|
|
|
|
23
|
3 |
|
public function __construct(Scoper $decoratedScoper) |
|
24
|
|
|
{ |
|
25
|
3 |
|
$this->decoratedScoper = $decoratedScoper; |
|
26
|
3 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Scopes PHP and JSON files related to Composer. |
|
30
|
|
|
* |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
3 |
|
public function scope(string $filePath, string $prefix): string |
|
34
|
|
|
{ |
|
35
|
3 |
|
if (preg_match('/composer\.lock$/', $filePath)) { |
|
36
|
1 |
|
return file_get_contents($filePath); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
if (1 !== preg_match('/composer\.json$/', $filePath)) { |
|
40
|
1 |
|
return $this->decoratedScoper->scope($filePath, $prefix); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
$decodedJson = json_decode( |
|
44
|
|
|
file_get_contents($filePath), |
|
45
|
1 |
|
true |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
1 |
|
if (isset($decodedJson['autoload'])) { |
|
49
|
1 |
|
$decodedJson['autoload'] = $this->prefixAutoloaders($decodedJson['autoload'], $prefix); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
if (isset($decodedJson['autoload-dev'])) { |
|
53
|
1 |
|
$decodedJson['autoload-dev'] = $this->prefixAutoloaders($decodedJson['autoload-dev'], $prefix); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return json_encode( |
|
57
|
|
|
$decodedJson, |
|
58
|
1 |
|
JSON_PRETTY_PRINT |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
private function prefixAutoloaders(array $autoloader, string $prefix): array |
|
63
|
|
|
{ |
|
64
|
1 |
|
if (isset($autoloader['psr-4'])) { |
|
65
|
1 |
|
$autoloader['psr-4'] = $this->prefixAutoloader($autoloader['psr-4'], $prefix); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
return $autoloader; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
private function prefixAutoloader(array $autoloader, string $prefix): array |
|
72
|
|
|
{ |
|
73
|
1 |
|
$loader = []; |
|
74
|
|
|
|
|
75
|
1 |
|
foreach ($autoloader as $namespace => $paths) { |
|
76
|
1 |
|
$loader[sprintf('%s\\%s', $prefix, $namespace)] = $paths; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
return $loader; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|