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