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\Whitelist; |
18
|
|
|
use stdClass; |
19
|
|
|
use function array_map; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @private |
23
|
|
|
*/ |
24
|
|
|
final class AutoloadPrefixer |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param stdClass $contents Decoded JSON |
28
|
|
|
* @param string $prefix |
29
|
|
|
* |
30
|
8 |
|
* @return array Prefixed decoded JSON |
31
|
|
|
*/ |
32
|
8 |
|
public static function prefixPackageAutoloads(stdClass $contents, string $prefix, Whitelist $whitelist): stdClass |
33
|
8 |
|
{ |
34
|
|
|
if (isset($contents->autoload)) { |
35
|
|
|
$contents->autoload = self::prefixAutoloads($contents->autoload, $prefix, $whitelist); |
36
|
8 |
|
} |
37
|
3 |
|
|
38
|
|
|
if (isset($contents->{'autoload-dev'})) { |
39
|
|
|
$contents->{'autoload-dev'} = self::prefixAutoloads($contents->{'autoload-dev'}, $prefix, $whitelist); |
40
|
8 |
|
} |
41
|
|
|
|
42
|
|
|
if (isset($contents->extra, $contents->extra->laravel, $contents->extra->laravel->providers)) { |
43
|
8 |
|
$contents->extra->laravel->providers = self::prefixLaravelProviders($contents->extra->laravel->providers, $prefix, $whitelist); |
44
|
|
|
} |
45
|
8 |
|
|
46
|
1 |
|
return $contents; |
47
|
|
|
} |
48
|
|
|
|
49
|
7 |
|
private static function prefixAutoloads(stdClass $autoload, string $prefix, Whitelist $whitelist): stdClass |
50
|
5 |
|
{ |
51
|
|
|
if (false === isset($autoload->{'psr-4'}) && false === isset($autoload->{'psr-0'})) { |
52
|
7 |
|
return $autoload; |
53
|
|
|
} |
54
|
7 |
|
|
55
|
7 |
|
if (isset($autoload->{'psr-0'})) { |
56
|
|
|
$autoload->{'psr-4'} = self::mergePSR0And4( |
57
|
|
|
(array) $autoload->{'psr-0'}, |
58
|
7 |
|
(array) ($autoload->{'psr-4'} ?? new stdClass()) |
59
|
|
|
); |
60
|
|
|
} |
61
|
7 |
|
unset($autoload->{'psr-0'}); |
62
|
|
|
|
63
|
7 |
|
if (isset($autoload->{'psr-4'})) { |
64
|
|
|
$autoload->{'psr-4'} = self::prefixAutoload((array) $autoload->{'psr-4'}, $prefix, $whitelist); |
65
|
7 |
|
} |
66
|
7 |
|
|
67
|
|
|
return $autoload; |
68
|
|
|
} |
69
|
7 |
|
|
70
|
|
|
private static function prefixAutoload(array $autoload, string $prefix, Whitelist $whitelist): array |
71
|
|
|
{ |
72
|
5 |
|
$loader = []; |
73
|
|
|
|
74
|
5 |
|
foreach ($autoload as $namespace => $paths) { |
75
|
|
|
$newNamespace = $whitelist->belongsToWhitelistedNamespace($namespace) |
76
|
5 |
|
? $namespace |
77
|
1 |
|
: sprintf('%s\\%s', $prefix, $namespace) |
78
|
|
|
; |
79
|
|
|
|
80
|
5 |
|
$loader[$newNamespace] = $paths; |
81
|
|
|
} |
82
|
5 |
|
|
83
|
2 |
|
return $loader; |
84
|
|
|
} |
85
|
2 |
|
|
86
|
|
|
private static function mergePSR0And4(array $psr0, array $psr4): array |
87
|
3 |
|
{ |
88
|
|
|
foreach ($psr0 as $namespace => $path) { |
89
|
|
|
//Append backslashes, if needed, since psr-0 does not require this |
90
|
5 |
|
if ('\\' !== substr($namespace, -1)) { |
91
|
|
|
$namespace .= '\\'; |
92
|
|
|
} |
93
|
5 |
|
|
94
|
|
|
$path = self::updatePSR0Path($path, $namespace); |
95
|
5 |
|
|
96
|
|
|
if (!isset($psr4[$namespace])) { |
97
|
5 |
|
$psr4[$namespace] = $path; |
98
|
4 |
|
|
99
|
1 |
|
continue; |
100
|
|
|
} |
101
|
|
|
$psr4[$namespace] = self::mergeNamespaces($namespace, $path, $psr4); |
102
|
4 |
|
} |
103
|
|
|
|
104
|
4 |
|
return $psr4; |
105
|
|
|
} |
106
|
2 |
|
|
107
|
2 |
|
private static function updatePSR0Path($path, $namespace) |
108
|
1 |
|
{ |
109
|
|
|
$namespaceForPsr = str_replace('\\', '/', $namespace); |
110
|
|
|
|
111
|
2 |
|
if (!is_array($path)) { |
112
|
2 |
|
if ('/' !== substr($path, -1)) { |
113
|
|
|
$path .= '/'; |
114
|
|
|
} |
115
|
2 |
|
|
116
|
|
|
$path .= $namespaceForPsr.'/'; |
117
|
|
|
|
118
|
|
|
return $path; |
119
|
|
|
} |
120
|
|
|
foreach ($path as $key => $item) { |
121
|
|
|
if ('/' !== substr($item, -1)) { |
122
|
|
|
$item .= '/'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$item .= $namespaceForPsr.'/'; |
126
|
|
|
$path[$key] = $item; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $path; |
130
|
|
|
} |
131
|
3 |
|
|
132
|
|
|
/** |
133
|
|
|
* Deals with the 4 possible scenarios: |
134
|
3 |
|
* PSR0 | PSR4 |
135
|
1 |
|
* array | |
136
|
|
|
* string | |
137
|
|
|
* or simply the namepace not existing as a psr-4 entry. |
138
|
2 |
|
* |
139
|
1 |
|
* @param string $psr0Namespace |
140
|
|
|
* @param string|array $psr0Path |
141
|
1 |
|
* @param string|array $psr4 |
142
|
|
|
* |
143
|
|
|
* @return string|array |
144
|
|
|
*/ |
145
|
2 |
|
private static function mergeNamespaces(string $psr0Namespace, $psr0Path, $psr4) |
146
|
1 |
|
{ |
147
|
|
|
// Both strings |
148
|
1 |
|
if (is_string($psr4[$psr0Namespace]) && is_string($psr0Path)) { |
149
|
|
|
return [$psr4[$psr0Namespace], $psr0Path]; |
150
|
|
|
} |
151
|
1 |
|
//psr-4 is string, and psr-0 is array |
152
|
1 |
View Code Duplication |
if (is_string($psr4[$psr0Namespace]) && is_array($psr0Path)) { |
|
|
|
|
153
|
|
|
$psr0Path[] = $psr4[$psr0Namespace]; |
154
|
|
|
|
155
|
|
|
return $psr0Path; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
//psr-4 is array and psr-0 is string |
159
|
|
View Code Duplication |
if (is_array($psr4[$psr0Namespace]) && is_string($psr0Path)) { |
|
|
|
|
160
|
|
|
$psr4[$psr0Namespace][] = $psr0Path; |
161
|
|
|
|
162
|
|
|
return $psr4[$psr0Namespace]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (is_array($psr4[$psr0Namespace]) && is_array($psr0Path)) { |
166
|
|
|
return array_merge($psr4[$psr0Namespace], $psr0Path); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $psr0Path; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
private static function prefixLaravelProviders(array $providers, string $prefix, Whitelist $whitelist): array |
173
|
|
|
{ |
174
|
|
|
return array_map( |
175
|
|
|
function (string $provider) use ($prefix, $whitelist): string { |
176
|
|
|
return $whitelist->belongsToWhitelistedNamespace($provider) |
177
|
|
|
? $provider |
178
|
|
|
: sprintf('%s\\%s', $prefix, $provider) |
179
|
|
|
; |
180
|
|
|
}, |
181
|
|
|
$providers |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.