This repository was archived by the owner on Sep 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoper.inc.php
More file actions
356 lines (322 loc) · 13.9 KB
/
Copy pathscoper.inc.php
File metadata and controls
356 lines (322 loc) · 13.9 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
// NOTE ON WORDPRESS & PLUGIN INTEGRATIONS
// ---------------------------------------
// This config must keep certain WordPress globals and 3rd-party plugin classes
// unscoped so they continue to match the runtime provided by WordPress.
//
// Guidelines:
// 1) Core WP globals (WP_*/wp_*/get_*/set_*/esc_attr) must never end up as
// Convoworks\WP_* or Convoworks\wp_* at runtime. The WP patcher below:
// - Explicitly de-prefixes WP_User, WP_REST_Request, WP_REST_Response.
// - Uses regex rules to de-prefix any other WP_*/wp_* symbols.
// When adding new WP-related typehints (e.g. WP_Query, WP_Post), verify the
// scoped build and extend the explicit list above if needed.
//
// 2) Other WP plugins with global classes (SSA_*, Simply_Schedule_Appointments,
// Frm*, rtbQuery, rtbBooking, etc.) must have a dedicated patcher that
// reverses any Convoworks prefixing. See the SSA / Formidable / RTB patchers
// below as reference.
//
// 3) When integrating a new plugin or WP feature that introduces new global
// classes/functions, always:
// - Use them as-is (no custom namespace aliases), and
// - Add/adjust a patcher here if php-scoper ends up prefixing them.
declare(strict_types=1);
// Stub for IDE support - Finder is provided by php-scoper at runtime
// Use Symfony's Finder as a fallback for IDE autocomplete during development
if (class_exists('Symfony\Component\Finder\Finder') && !class_exists('Isolated\Symfony\Component\Finder\Finder', false)) {
class_alias('Symfony\Component\Finder\Finder', 'Isolated\Symfony\Component\Finder\Finder');
}
use Isolated\Symfony\Component\Finder\Finder;
$polyfillsBootstraps = array_map(
static function (SplFileInfo $fileInfo) {
return $fileInfo->getPathname();
},
iterator_to_array(
/** @phpstan-ignore-next-line */
Finder::create()
->files()
->in(__DIR__ . '/vendor/symfony/polyfill-*')
->name('bootstrap.php'),
false
)
);
$polyfillsStubs = array_map(
static function (SplFileInfo $fileInfo) {
return $fileInfo->getPathname();
},
iterator_to_array(
/** @phpstan-ignore-next-line */
Finder::create()
->files()
->in(__DIR__ . '/vendor/symfony/polyfill-*/Resources/stubs')
->name('*.php'),
false
)
);
return [
// The prefix configuration. When null, php-scoper will generate a random prefix.
// When set to a non-null string, that exact prefix will be used.
'prefix' => 'Convoworks',
// By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working
// directory. You can however define which files should be scoped by defining a collection of Finders in the
// following configuration key.
//
// For more see: https://github.com/humbug/php-scoper#finders-and-paths
'finders' => [
/** @phpstan-ignore-next-line */
Finder::create()->files()->in('src'),
/** @phpstan-ignore-next-line */
Finder::create()
->files()
->ignoreVCS(true)
->notName('/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/')
->exclude([
'doc',
'test',
'test_old',
'tests',
'Tests',
'vendor-bin',
])
->in('vendor')
],
// Whitelists a list of files. Unlike the other whitelist related features, this one is about completely leaving
// a file untouched.
// Paths are relative to the configuration file unless if they are already absolute
'exclude-files' => array_merge(
[
'convo-plugin.php',
'vendor/php-di/php-di/src/Compiler/Template.php',
'vendor/league/plates/example/templates/layout.php',
],
$polyfillsBootstraps,
$polyfillsStubs
),
// When scoping PHP files, there will be scenarios where some of the code being scoped indirectly references the
// original namespace. These will include, for example, strings or string manipulations. PHP-Scoper has limited
// support for prefixing such strings. To circumvent that, you can define patchers to manipulate the file to your
// heart contents.
//
// For more see: https://github.com/humbug/php-scoper#patchers
'patchers' => [
// static function (string $filePath, string $prefix, string $content): string {
// $content = preg_replace('/a/', 'b', $content);
// return $content;
// },
static function (string $filePath, string $prefix, string $content): string {
// Fix WP classes and functions used
$temp = $content;
$quotedPrefix = preg_quote($prefix, '/');
// Ensure core WP classes such as WP_User, WP_REST_Request and WP_REST_Response are not prefixed
$temp = str_replace(
[
'\\' . $prefix . '\\WP_User',
$prefix . '\\WP_User',
'\\' . $prefix . '\\WP_REST_Request',
$prefix . '\\WP_REST_Request',
'\\' . $prefix . '\\WP_REST_Response',
$prefix . '\\WP_REST_Response',
],
[
'\\WP_User',
'WP_User',
'\\WP_REST_Request',
'WP_REST_Request',
'\\WP_REST_Response',
'WP_REST_Response',
],
$temp
);
$temp = preg_replace(
[
'/\\\\' . $quotedPrefix . '\\\\WP_([A-Za-z_][A-Za-z0-9_]*)/m',
'/\\\\' . $quotedPrefix . '\\\\wp_([A-Za-z_][A-Za-z0-9_]*)/m',
'/' . $quotedPrefix . '\\\\WP_([A-Za-z_][A-Za-z0-9_]*)/m',
'/' . $quotedPrefix . '\\\\wp_([A-Za-z_][A-Za-z0-9_]*)/m',
'/\\\\' . $quotedPrefix . '\\\\get_([A-Za-z_][A-Za-z0-9_]*)/m',
'/\\\\' . $quotedPrefix . '\\\\set_([A-Za-z_][A-Za-z0-9_]*)/m',
'/\\\\' . $quotedPrefix . '\\\\esc_attr/m',
],
[
'\\WP_$1',
'\\wp_$1',
'\\WP_$1',
'\\wp_$1',
'\\get_$1',
'\\set_$1',
'\\esc_attr',
],
$temp
);
// Example of how to extend for specific get_the_* functions if needed:
// $temp = preg_replace(
// '/\\\\' . $quotedPrefix . '\\\\get_the_([A-Za-z_][A-Za-z0-9_]*)/m',
// '\\get_the_$1',
// $temp
// );
if (preg_last_error() === PREG_NO_ERROR) {
$content = $temp;
unset($temp);
} else {
echo "preg_replace encountered an error during WP patcher: [" . preg_last_error() . "][" . preg_last_error_msg() . "]" . PHP_EOL;
}
return $content;
},
static function (string $filePath, string $prefix, string $content): string {
// Fix SSA classes
$content = str_replace(
[
"\\\\$prefix\\\\Simply_Schedule_Appointments",
"$prefix\\\\Simply_Schedule_Appointments",
"\\$prefix\\Simply_Schedule_Appointments",
// Explicitly fix common SSA classes used in the codebase
'\\' . $prefix . '\\SSA_Appointment_Type_Model',
$prefix . '\\SSA_Appointment_Type_Model',
'\\' . $prefix . '\\SSA_Appointment_Type_Object',
$prefix . '\\SSA_Appointment_Type_Object',
// "ssa()"
],
[
"\\\\Simply_Schedule_Appointments",
"Simply_Schedule_Appointments",
"Simply_Schedule_Appointments",
'\\SSA_Appointment_Type_Model',
'SSA_Appointment_Type_Model',
'\\SSA_Appointment_Type_Object',
'SSA_Appointment_Type_Object',
// "\\ssa()"
],
$content
);
$temp = $content;
$quotedPrefix = preg_quote($prefix, '/');
// Fix SSA_* classes - handle both with and without leading backslash
// Similar to how WP_* classes are handled above
$temp = preg_replace(
[
'/\\\\' . $quotedPrefix . '\\\\SSA_([A-Za-z_][A-Za-z0-9_]*)/m',
'/' . $quotedPrefix . '\\\\SSA_([A-Za-z_][A-Za-z0-9_]*)/m',
],
[
'\\SSA_$1',
'SSA_$1',
],
$temp
);
if (preg_last_error() === PREG_NO_ERROR) {
$content = $temp;
unset($temp);
} else {
echo "preg_replace encountered an error during SSA patcher: [" . preg_last_error() . "][" . preg_last_error_msg() . "]" . PHP_EOL;
}
return $content;
},
static function (string $filePath, string $prefix, string $content): string {
// Fix Formidable classes
$content = str_replace(
[
"\\\\$prefix\\\\Frm",
"$prefix\\\\Frm",
"\\$prefix\\Frm",
],
[
"\\\\Frm",
"\\\\Frm",
"\\Frm",
],
$content
);
$temp = $content;
$quotedPrefix = preg_quote($prefix, '/');
$temp = preg_replace(
[
'/\\\\' . $quotedPrefix . '\\\\Frm([A-Za-z_][A-Za-z0-9_]*)/m',
],
'\\Frm$1',
$temp
);
if (preg_last_error() === PREG_NO_ERROR) {
$content = $temp;
unset($temp);
} else {
echo "preg_replace encountered an error during Formidable patcher: [" . preg_last_error() . "][" . preg_last_error_msg() . "]" . PHP_EOL;
}
return $content;
},
static function (string $filePath, string $prefix, string $content): string {
// RTB fix
$content = str_replace(
[
"\\\\$prefix\\\\rtbQuery",
"\\\\$prefix\\\\rtbBooking",
"\\$prefix\\rtbQuery",
"\\$prefix\\rtbBooking",
],
[
"\\\\rtbQuery",
"\\\\rtbBooking",
"\\rtbQuery",
"\\rtbBooking",
],
$content
);
return $content;
},
static function (string $filePath, string $prefix, string $contents): string {
// Guzzle-specific fixes
$contents = str_replace(
[
"GuzzleHttp\\\\ClientInterface::MAJOR_VERSION",
"GuzzleHttp\\\\ClientInterface::VERSION",
],
[
"\\\\$prefix\\\\GuzzleHttp\\\\ClientInterface::MAJOR_VERSION",
"\\\\$prefix\\\\GuzzleHttp\\\\ClientInterface::VERSION",
],
$contents
);
return $contents;
},
static function (string $filePath, string $prefix, string $content): string {
// Ensure zef-dev formatter references scoped Monolog classes while keeping Zef\* unscoped
// Make path check OS-agnostic (Windows/Unix)
if (preg_match('#[\\\\/]+vendor[\\\\/]+zef-dev[\\\\/]+zef-monolog-formatter[\\\\/]#', $filePath)) {
$content = str_replace('Monolog\\\\', "\\\\$prefix\\\\Monolog\\\\", $content);
}
return $content;
},
],
// PHP-Scoper's goal is to make sure that all code for a project lies in a distinct PHP namespace. However, you
// may want to share a common API between the bundled code of your PHAR and the consumer code. For example if
// you have a PHPUnit PHAR with isolated code, you still want the PHAR to be able to understand the
// PHPUnit\Framework\TestCase class.
//
// A way to achieve this is by specifying a list of classes to not prefix with the following configuration key. Note
// that this does not work with functions or constants neither with classes belonging to the global namespace.
//
// Fore more see https://github.com/humbug/php-scoper#whitelist
'exclude-namespaces' => [
// 'PHPUnit\Framework\TestCase', // A specific class
// 'PHPUnit\Framework\*', // The whole namespace
// '*', // Everything
// Note: 'Convo' is intentionally excluded from prefixing to keep the public plugin API namespace stable.
'Convo',
'Psr',
'Symfony\Polyfill',
'Inpsyde\WPRESTStarter',
'Zef',
],
// If `true` then the user defined constants belonging to the global namespace will not be prefixed.
//
// For more see https://github.com/humbug/php-scoper#constants--constants--functions-from-the-global-namespace
'expose-global-constants' => true,
// If `true` then the user defined classes belonging to the global namespace will not be prefixed.
//
// For more see https://github.com/humbug/php-scoper#constants--constants--functions-from-the-global-namespace
'expose-global-classes' => true,
// If `true` then the user defined functions belonging to the global namespace will not be prefixed.
//
// For more see https://github.com/humbug/php-scoper#constants--constants--functions-from-the-global-namespace
'expose-global-functions' => true,
];