-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathphp-scoper.php
124 lines (113 loc) · 4.29 KB
/
php-scoper.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
<?php
declare(strict_types=1);
use Isolated\Symfony\Component\Finder\Finder;
//use Symfony\Component\Finder\Finder;
////////////////////
// Configuration
////////////////////
$apiFolder = getcwd() . '/inc/api/';
$whitelistFolders = ['vendor/symfony/polyfill*'];
$whiteListNamespaces = [
// Do not scope polyfills
'Symfony\\Polyfill\\*'
];
////////////////////
// Obtain original namespace (must be the first entry in autoload.psr-4)
$composerJson = json_decode(file_get_contents('composer.json'), true);
$psr4 = array_keys($composerJson['autoload']['psr-4'])[0];
// Obtain stubs generated by grunt task "php:scope"
$stubsJson = json_decode(file_get_contents('php-scoper.php.json'), true);
// Whitelist all available monorepo-plugins
$whiteListPlugins = glob('../../../*', GLOB_ONLYDIR);
foreach ($whiteListPlugins as $key => $plugin) {
$composerJson = json_decode(file_get_contents($plugin . '/composer.json'), true);
$whiteListPlugins[$key] = array_keys($composerJson['autoload']['psr-4'])[0] . '*';
}
// Expand all files from folders to absolute file pathes
$whitelistFoldersFiles = [];
foreach ($whitelistFolders as $whitelistFolder) {
try {
$whitelistFoldersFiles = array_merge(
$whitelistFoldersFiles,
array_map(
'realpath',
array_keys(
iterator_to_array(
Finder::create()
->files()
->ignoreUnreadableDirs()
->in($whitelistFolder)
)
)
)
);
} catch (Exception $e) {
// Silence is golden.
}
}
return [
'prefix' => $psr4 . 'Vendor',
'finders' => [
Finder::create()
->files()
->in('inc'),
Finder::create()
->files()
->notName('/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/')
->exclude(['doc', 'test', 'test_old', 'tests', 'Tests', 'vendor-bin'])
->in('vendor'),
Finder::create()->append(['composer.json'])
],
'patchers' => [
/**
* Allow to remove namespace for a file, when the defined functions should be globally available.
* This is useful for plugin APIs.
*/
function ($filePath, $prefix, $content) use ($apiFolder) {
if (strpos($filePath, $apiFolder, 0) === 0) {
$prefixDoubleSlashed = str_replace('\\', '\\\\', $prefix);
return preg_replace(sprintf('/^namespace %s;$/m', $prefixDoubleSlashed), '', $content, 1);
}
return $content;
},
/**
* This callback removes un-prefix all classes and functions obtained from stubs because
* they are available globally in our WP ecosystem.
*/
function ($filePath, $prefix, $content) use ($stubsJson) {
$prefixDoubleSlashed = str_replace('\\', '\\\\', $prefix);
$quotes = ['\'', '"', '`'];
foreach ($stubsJson as $identifier) {
$identifierDoubleSlashed = str_replace('\\', '\\\\', $identifier);
$content = str_replace($prefix . '\\' . $identifier, $identifier, $content); // "PREFIX\foo()", or "foo extends nativeClass"
// Replace in strings, e. g. "if( function_exists('PREFIX\\foo') )"
foreach ($quotes as $quote) {
$content = str_replace(
$quote . $prefixDoubleSlashed . '\\\\' . $identifierDoubleSlashed . $quote,
$quote . $identifierDoubleSlashed . $quote,
$content
);
}
}
return $content;
}
],
'whitelist' => array_merge($whiteListPlugins, $whiteListNamespaces),
'files-whitelist' => array_merge(
$whitelistFoldersFiles,
array_map(
'realpath',
array_keys(
iterator_to_array(
Finder::create()
->files()
->in('inc/base/others')
->notName('start.php')
)
)
)
),
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => false
];