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\Symfony; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Scoper\Scoper; |
18
|
|
|
use Humbug\PhpScoper\Symbol\EnrichedReflector; |
19
|
|
|
use Humbug\PhpScoper\Symbol\SymbolsRegistry; |
20
|
|
|
use PhpParser\Node\Name\FullyQualified; |
21
|
|
|
use function array_filter; |
22
|
|
|
use function func_get_args; |
23
|
|
|
use function preg_match as native_preg_match; |
24
|
|
|
use function preg_match_all as native_preg_match_all; |
25
|
|
|
use function Safe\substr; |
26
|
|
|
use function str_replace; |
27
|
|
|
use function strlen; |
28
|
|
|
use function strpos; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Scopes the Symfony XML configuration files. |
32
|
|
|
*/ |
33
|
|
|
final class XmlScoper implements Scoper |
34
|
|
|
{ |
35
|
|
|
private const XML_EXTENSION_REGEX = '/\.xml$/i'; |
36
|
15 |
|
private const NAMESPACE_PATTERN = '/<prototype.*\snamespace="(?:(?<namespace>(?:[^\\\\]+(?<separator>\\\\(?:\\\\)?))))"/'; |
37
|
|
|
private const SINGLE_CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_\d]+(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_\d]+(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u'; |
38
|
15 |
|
|
39
|
|
|
private Scoper $decoratedScoper; |
40
|
|
|
private string $prefix; |
41
|
|
|
private EnrichedReflector $enrichedReflector; |
42
|
|
|
private SymbolsRegistry $symbolsRegistry; |
43
|
|
|
|
44
|
14 |
|
public function __construct( |
45
|
|
|
Scoper $decoratedScoper, |
46
|
14 |
|
string $prefix, |
47
|
3 |
|
EnrichedReflector $enrichedReflector, |
48
|
|
|
SymbolsRegistry $symbolsRegistry |
49
|
|
|
) { |
50
|
11 |
|
$this->decoratedScoper = $decoratedScoper; |
51
|
11 |
|
$this->prefix = $prefix; |
52
|
|
|
$this->enrichedReflector = $enrichedReflector; |
53
|
11 |
|
$this->symbolsRegistry = $symbolsRegistry; |
54
|
|
|
} |
55
|
|
|
|
56
|
11 |
|
public function scope(string $filePath, string $contents): string |
57
|
|
|
{ |
58
|
11 |
|
if (1 !== native_preg_match(self::XML_EXTENSION_REGEX, $filePath)) { |
59
|
3 |
|
return $this->decoratedScoper->scope(...func_get_args()); |
60
|
|
|
} |
61
|
|
|
|
62
|
8 |
|
$contents = self::scopeClasses( |
63
|
8 |
|
$contents, |
64
|
8 |
|
$this->prefix, |
65
|
8 |
|
$this->enrichedReflector, |
66
|
8 |
|
$this->symbolsRegistry, |
67
|
8 |
|
); |
68
|
|
|
|
69
|
|
|
return self::scopeNamespaces( |
70
|
8 |
|
$contents, |
71
|
8 |
|
$this->prefix, |
72
|
8 |
|
$this->enrichedReflector, |
73
|
8 |
|
$this->symbolsRegistry, |
74
|
8 |
|
); |
75
|
8 |
|
} |
76
|
|
|
|
77
|
|
|
private static function scopeClasses( |
78
|
8 |
|
string $contents, |
79
|
|
|
string $prefix, |
80
|
|
|
EnrichedReflector $enrichedReflector, |
81
|
11 |
|
SymbolsRegistry $symbolsRegistry |
82
|
|
|
): string { |
83
|
11 |
|
if (1 > native_preg_match_all(self::SINGLE_CLASS_PATTERN, $contents, $matches)) { |
84
|
10 |
|
return $contents; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$contents = self::replaceClasses( |
88
|
1 |
|
array_filter($matches['singleClass']), |
89
|
1 |
|
array_filter($matches['singleSeparator']), |
90
|
1 |
|
$prefix, |
91
|
1 |
|
$contents, |
92
|
1 |
|
$enrichedReflector, |
93
|
|
|
$symbolsRegistry, |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
return self::replaceClasses( |
97
|
|
|
array_filter($matches['class']), |
98
|
|
|
array_filter($matches['separator']), |
99
|
|
|
$prefix, |
100
|
8 |
|
$contents, |
101
|
|
|
$enrichedReflector, |
102
|
|
|
$symbolsRegistry, |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private static function scopeNamespaces( |
107
|
8 |
|
string $contents, |
108
|
8 |
|
string $prefix, |
109
|
|
|
EnrichedReflector $enrichedReflector, |
110
|
|
|
SymbolsRegistry $symbolsRegistry |
111
|
8 |
|
): string { |
112
|
|
|
if (1 > native_preg_match_all(self::NAMESPACE_PATTERN, $contents, $matches)) { |
113
|
8 |
|
return $contents; |
114
|
8 |
|
} |
115
|
|
|
|
116
|
8 |
|
return self::replaceClasses( |
117
|
8 |
|
array_filter($matches['namespace']), |
118
|
|
|
array_filter($matches['separator']), |
119
|
8 |
|
$prefix, |
120
|
|
|
$contents, |
121
|
8 |
|
$enrichedReflector, |
122
|
1 |
|
$symbolsRegistry, |
123
|
8 |
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
8 |
|
/** |
127
|
1 |
|
* @param string[] $classes |
128
|
1 |
|
* @param string[] $separators |
129
|
1 |
|
*/ |
130
|
|
|
private static function replaceClasses( |
131
|
|
|
array $classes, |
132
|
|
|
array $separators, |
133
|
|
|
string $prefix, |
134
|
8 |
|
string $contents, |
135
|
|
|
EnrichedReflector $enrichedReflector, |
136
|
8 |
|
SymbolsRegistry $symbolsRegistry |
137
|
|
|
): string { |
138
|
|
|
if ([] === $classes) { |
139
|
|
|
return $contents; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$scopedContents = ''; |
143
|
|
|
|
144
|
|
|
foreach ($classes as $index => $class) { |
145
|
|
|
$separator = $separators[$index]; |
146
|
|
|
|
147
|
|
|
$psr4Service = '"'.$class.$separator.'"'; |
148
|
|
|
|
149
|
|
|
if (false !== strpos($contents, $psr4Service)) { |
150
|
|
|
$offset = strpos($contents, $psr4Service) + strlen($psr4Service); |
151
|
|
|
|
152
|
|
|
$stringToScope = substr($contents, 0, $offset); |
|
|
|
|
153
|
|
|
$contents = substr($contents, $offset); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$prefixedClass = $prefix.$separator.$class; |
156
|
|
|
|
157
|
|
|
$scopedContents .= $enrichedReflector->belongsToExcludedNamespace($class.$separator.'__UnknownService__') |
158
|
|
|
? $stringToScope |
159
|
|
|
: str_replace($class, $prefixedClass, $stringToScope); |
160
|
|
|
|
161
|
|
|
continue; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$offset = strpos($contents, $class) + strlen($class); |
165
|
|
|
|
166
|
|
|
$stringToScope = substr($contents, 0, $offset); |
|
|
|
|
167
|
|
|
$contents = substr($contents, $offset); |
|
|
|
|
168
|
|
|
|
169
|
|
|
$prefixedClass = $prefix.$separator.$class; |
170
|
|
|
|
171
|
|
|
$scopedContents .= $enrichedReflector->belongsToExcludedNamespace($class) |
172
|
|
|
? $stringToScope |
173
|
|
|
: str_replace($class, $prefixedClass, $stringToScope); |
174
|
|
|
|
175
|
|
|
if ($enrichedReflector->isExposedClass($class)) { |
176
|
|
|
$symbolsRegistry->recordClass( |
177
|
|
|
new FullyQualified($class), |
178
|
|
|
new FullyQualified($prefixedClass), |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$scopedContents .= $contents; |
184
|
|
|
|
185
|
|
|
return $scopedContents; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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.