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; |
18
|
|
|
use Humbug\PhpScoper\Whitelist; |
19
|
|
|
use PhpParser\Node\Name\FullyQualified; |
20
|
|
|
use function func_get_args; |
21
|
|
|
use function preg_match_all; |
22
|
|
|
use function str_replace; |
23
|
|
|
use function strlen; |
24
|
|
|
use function strpos; |
25
|
|
|
use function substr; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Scopes the Symfony XML configuration files. |
29
|
|
|
*/ |
30
|
|
|
final class XmlScoper implements Scoper |
31
|
|
|
{ |
32
|
|
|
private const FILE_PATH_PATTERN = '/.*\.xml$/i'; |
33
|
|
|
|
34
|
|
|
private $decoratedScoper; |
35
|
|
|
|
36
|
15 |
|
public function __construct(Scoper $decoratedScoper) |
37
|
|
|
{ |
38
|
15 |
|
$this->decoratedScoper = $decoratedScoper; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
14 |
|
public function scope(string $filePath, string $contents, string $prefix, array $patchers, Whitelist $whitelist): string |
45
|
|
|
{ |
46
|
14 |
|
if (1 !== preg_match(self::FILE_PATH_PATTERN, $filePath)) { |
47
|
3 |
|
return $this->decoratedScoper->scope(...func_get_args()); |
48
|
|
|
} |
49
|
|
|
|
50
|
11 |
|
$contents = $this->scopeClasses($contents, $prefix, $whitelist); |
51
|
11 |
|
$contents = $this->scopeNamespaces($contents, $prefix, $whitelist); |
52
|
|
|
|
53
|
11 |
|
return $contents; |
54
|
|
|
} |
55
|
|
|
|
56
|
11 |
|
private function scopeClasses(string $contents, string $prefix, Whitelist $whitelist): string |
57
|
|
|
{ |
58
|
11 |
|
if (1 > preg_match_all('/(?:(?<singleClass>(?:[\p{L}_\d]+(?<singleSeparator>\\\\(?:\\\\)?){1})):)|(?<class>(?:[\p{L}_\d]+(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u', $contents, $matches)) { |
59
|
3 |
|
return $contents; |
60
|
|
|
} |
61
|
|
|
|
62
|
8 |
|
$contents = $this->replaceClasses( |
63
|
8 |
|
array_filter($matches['singleClass']), |
64
|
8 |
|
array_filter($matches['singleSeparator']), |
65
|
8 |
|
$prefix, |
66
|
8 |
|
$contents, |
67
|
8 |
|
$whitelist |
68
|
|
|
); |
69
|
|
|
|
70
|
8 |
|
$contents = $this->replaceClasses( |
71
|
8 |
|
array_filter($matches['class']), |
72
|
8 |
|
array_filter($matches['separator']), |
73
|
8 |
|
$prefix, |
74
|
8 |
|
$contents, |
75
|
8 |
|
$whitelist |
76
|
|
|
); |
77
|
|
|
|
78
|
8 |
|
return $contents; |
79
|
|
|
} |
80
|
|
|
|
81
|
11 |
|
private function scopeNamespaces(string $contents, string $prefix, Whitelist $whitelist): string |
82
|
|
|
{ |
83
|
11 |
|
if (1 > preg_match_all('/\<prototype.*\snamespace="(?:(?<namespace>(?:[^\\\\]+(?<separator>\\\\(?:\\\\)?){1})))"/', $contents, $matches)) { |
84
|
10 |
|
return $contents; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $this->replaceClasses( |
88
|
1 |
|
array_filter($matches['namespace']), |
89
|
1 |
|
array_filter($matches['separator']), |
90
|
1 |
|
$prefix, |
91
|
1 |
|
$contents, |
92
|
1 |
|
$whitelist |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string[] $classes |
98
|
|
|
* @param string[] $separators |
99
|
|
|
*/ |
100
|
8 |
|
private function replaceClasses( |
101
|
|
|
array $classes, |
102
|
|
|
array $separators, |
103
|
|
|
string $prefix, |
104
|
|
|
string $contents, |
105
|
|
|
Whitelist $whitelist |
106
|
|
|
): string { |
107
|
8 |
|
if ([] === $classes) { |
108
|
8 |
|
return $contents; |
109
|
|
|
} |
110
|
|
|
|
111
|
8 |
|
$scopedContents = ''; |
112
|
|
|
|
113
|
8 |
|
foreach ($classes as $index => $class) { |
114
|
8 |
|
$offset = strpos($contents, $class) + strlen($class); |
115
|
|
|
|
116
|
8 |
|
$stringToScope = substr($contents, 0, $offset); |
117
|
8 |
|
$contents = substr($contents, $offset); |
118
|
|
|
|
119
|
8 |
|
$prefixedClass = $prefix.$separators[$index].$class; |
120
|
|
|
|
121
|
8 |
|
$scopedContents .= $whitelist->belongsToWhitelistedNamespace($class) |
122
|
1 |
|
? $stringToScope |
123
|
8 |
|
: str_replace($class, $prefixedClass, $stringToScope) |
124
|
|
|
; |
125
|
|
|
|
126
|
8 |
|
if ($whitelist->isSymbolWhitelisted($class) || $whitelist->isGlobalWhitelistedClass($class)) { |
127
|
1 |
|
$whitelist->recordWhitelistedClass( |
128
|
1 |
|
new FullyQualified($class), |
129
|
1 |
|
new FullyQualified($prefixedClass) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
8 |
|
$scopedContents .= $contents; |
135
|
|
|
|
136
|
8 |
|
return $scopedContents; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|