-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
scoper.inc.php
178 lines (153 loc) · 6.32 KB
/
scoper.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
use Isolated\Symfony\Component\Finder\Finder;
$polyfillsBootstraps = array_map(
static fn (SplFileInfo $fileInfo) => $fileInfo->getPathname(),
iterator_to_array(
Finder::create()
->files()
->in(__DIR__.'/vendor/symfony/polyfill-*')
->name('bootstrap*.php'),
false,
),
);
$polyfillsStubs = array_map(
static fn (SplFileInfo $fileInfo) => $fileInfo->getPathname(),
iterator_to_array(
Finder::create()
->files()
->in(__DIR__.'/vendor/symfony/polyfill-*/Resources/stubs')
->name('*.php'),
false,
),
);
$symfonyDeprecationContracts = array_map(
static fn (SplFileInfo $fileInfo) => $fileInfo->getPathname(),
iterator_to_array(
Finder::create()
->files()
->in(__DIR__.'/vendor/symfony/deprecation-contracts')
->name('*.php'),
false,
),
);
return [
'prefix' => '_ContaoManager',
'whitelist' => [
Finder::class,
],
'exclude-namespaces' => [
'Symfony\Polyfill',
'React\Promise',
'/^Composer/',
],
'exclude-constants' => [
'/^SYMFONY\_[\p{L}_]+$/',
'/^COMPOSER(\_[\p{L}_]+)?$/',
],
'exclude-functions' => [
'trigger_deprecation',
],
'exclude-files' => [
...$polyfillsBootstraps,
...$polyfillsStubs,
...$symfonyDeprecationContracts,
],
'patchers' => [
static function (string $filePath, string $prefix, string $contents): string {
if ('src/Reflector.php' !== $filePath) {
return $contents;
}
$originalContents = file_get_contents(__DIR__.'/src/Reflector.php');
$classPosition = mb_strpos($originalContents, 'final class Reflector');
$prefixedClassPosition = mb_strpos($contents, 'final class Reflector');
return sprintf(
'%s%s',
mb_substr($contents, 0, $prefixedClassPosition),
mb_substr($originalContents, $classPosition),
);
},
static function (string $filePath, string $prefix, string $contents): string {
if ('bin/php-scoper' !== $filePath) {
return $contents;
}
return str_replace(
'\\'.$prefix.'\Isolated\Symfony\Component\Finder\Finder::class',
'\Isolated\Symfony\Component\Finder\Finder::class',
$contents,
);
},
static function (string $filePath, string $prefix, string $contents): string {
$files = ['vendor/symfony/dependency-injection/Loader/Configurator/Traits/ParentTrait.php', 'vendor/symfony/dependency-injection/Compiler/ResolveInstanceofConditionalsPass.php'];
if (!\in_array($filePath, $files, true)) {
return $contents;
}
$contents = str_replace(
[
'$definition = \substr_replace($definition, \'53\', 2, 2);',
'$definition = \substr_replace($definition, \'Child\', 44, 0);',
],
[
'$definition = \substr_replace($definition, \''.(53 + strlen($prefix.'\\')).'\', 2, 2);',
'$definition = \substr_replace($definition, \'Child\', '.(44 + strlen($prefix.'\\')).', 0);'
],
$contents
);
return $contents;
},
static function (string $filePath, string $prefix, string $contents): string {
if (!str_starts_with($filePath, 'vendor/composer/composer/src/Composer/Package')) {
return $contents;
}
$contents = str_replace("'$prefix\\\\", "'", $contents);
return $contents;
},
// Fix error templates (e.g. /vendor/symfony/error-handler/Resources/views)
static function (string $filePath, string $prefix, string $contents): string {
if (!str_starts_with($filePath, 'vendor/symfony/error-handler/Resources/')) {
return $contents;
}
return str_replace(
[
"namespace $prefix;",
'echo Symfony\Component\HttpKernel\Kernel::VERSION',
],
[
'',
"echo \\$prefix\Symfony\Component\HttpKernel\Kernel::VERSION"
],
$contents
);
},
// Disable HtmlErrorRenderer::fileExcerpt breaking due to high memory load
static function (string $filePath, string $prefix, string $contents): string {
if ('vendor/symfony/error-handler/ErrorRenderer/HtmlErrorRenderer.php' !== $filePath) {
return $contents;
}
return str_replace('private function fileExcerpt(string $file, int $line, int $srcContext = 3) : string
{', "private function fileExcerpt(string \$file, int \$line, int \$srcContext = 3) : string
{ return '';", $contents);
},
// Fix prod container cache path
static function (string $filePath, string $prefix, string $contents): string {
if ('vendor/symfony/http-kernel/Kernel.php' !== $filePath) {
return $contents;
}
return str_replace("\$buildDir . '/' . \$class . '.php'", "\$buildDir.'/'.str_replace('".$prefix."_', '', \$class).'.php'", $contents);
},
// Fix routes controllers with method argument
static function (string $filePath, string $prefix, string $contents): string {
if (!str_starts_with($filePath, 'api/Resources/cache/')) {
return $contents;
}
return str_replace("'Contao\\\\ManagerApi\\\\", "'$prefix\\\\Contao\\\\ManagerApi\\\\", $contents);
},
// Fix route redirects if API is called with a slash
static function (string $filePath, string $prefix, string $contents): string {
if ('vendor/symfony/framework-bundle/Routing/RedirectableCompiledUrlMatcher.php' !== $filePath) {
return $contents;
}
return str_replace("'Symfony\\\\Bundle\\\\FrameworkBundle\\\\Controller\\\\RedirectController::", "'$prefix\\\\Symfony\\\\Bundle\\\\FrameworkBundle\\\\Controller\\\\RedirectController::", $contents);
},
],
];