-
Notifications
You must be signed in to change notification settings - Fork 9
/
scoper.inc.php
128 lines (115 loc) · 3.89 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
<?php
/**
* PHP-Scoper configuration
*
* @package skaut-google-drive-gallery
*/
use Isolated\Symfony\Component\Finder\Finder;
/**
* Safely replaces pattern with replacement in string.
*
* @param string $pattern The pattern to be replaced.
* @param string $replacement The replacement.
* @param string $value The string to replace in.
*
* @return string The string with replacement, if it can be replaced.
*
* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
*/
function safe_replace( $pattern, $replacement, $value ) {
$replacement = mb_ereg_replace( $pattern, $replacement, $value );
if ( false === $replacement || null === $replacement ) {
return $value;
}
return $replacement;
}
/**
* Constructs a finder for composer dependencies.
*
* @return Finder The initialized Finder.
*/
function dependency_finder() {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec
exec( 'composer show --no-dev --name-only', $dependencies );
$finder = Finder::create()->files()->name( array( '*.php', '/LICENSE(.txt)?/' ) )->in( 'vendor' );
foreach ( $dependencies as $dependency ) {
$finder->path( '#^' . $dependency . '/#' );
}
return $finder;
}
// phpcs:enable
return array(
'expose-global-classes' => false,
'expose-global-constants' => true,
'expose-global-functions' => false,
'finders' => array(
dependency_finder()->notPath( '#^google/apiclient-services/#' ),
Finder::create()->files()
->name( 'autoload.php' )
->depth( 0 )
->in( 'vendor/google/apiclient-services' ),
Finder::create()->files()
->name( 'Drive.php' )
->depth( 0 )
->in( 'vendor/google/apiclient-services/src' ),
Finder::create()->files()
->name( array( '*.php', '/LICENSE(.txt)?/' ) )
->path( '#^google/apiclient-services/src/Drive/#' )
->in( 'vendor' ),
Finder::create()->files()
->name( array( '*.php', '/LICENSE(.txt)?/' ) )
->depth( 0 )
->in( 'vendor/composer' ),
Finder::create()->files()
->name( 'autoload.php' )
->depth( 0 )
->in( 'vendor' ),
),
'output-dir' => 'dist/vendor',
'patchers' => array(
static function ( $file_path, $prefix, $contents ) {
$regex_prefix = mb_ereg_replace( '\\\\', '\\\\\\\\', $prefix );
$replace_prefix = mb_ereg_replace( '\\\\', '\\\\', $prefix );
$underscore_prefix = mb_ereg_replace( '\\\\', '_', $prefix );
if ( __DIR__ . '/vendor/composer/autoload_real.php' === $file_path ) {
$contents = safe_replace(
"if \\('Composer\\\\\\\\Autoload\\\\\\\\ClassLoader' === \\\$class\\)",
"if ('{$replace_prefix}\\\\Composer\\\\Autoload\\\\ClassLoader' === \$class)",
$contents
);
$contents = safe_replace(
"\\\\spl_autoload_unregister\\(array\\('ComposerAutoloaderInit",
"\\spl_autoload_unregister(array('{$replace_prefix}\\\\ComposerAutoloaderInit",
$contents
);
$contents = safe_replace(
"\\\$GLOBALS\['__composer_autoload_files'\]",
"\$GLOBALS['__composer_autoload_files_{$underscore_prefix}']",
$contents
);
}
if ( __DIR__ . '/vendor/guzzlehttp/guzzle/src/functions.php' === $file_path ) {
$contents = safe_replace( "\\\\{$replace_prefix}\\\\uri_template\(", "\\uri_template(", $contents );
}
if ( __DIR__ . '/vendor/google/apiclient/src/aliases.php' === $file_path ) {
$contents = safe_replace(
"'{$regex_prefix}\\\\\\\\Google\\\\\\\\(.*?)'\\s+=> 'Google_(.*?)'",
"'{$replace_prefix}\\\\Google\\\\\\1' => '{$replace_prefix}\\\\Google_\\2'",
$contents
);
}
$contents = safe_replace(
"array\('Monolog\\\\\\\\Utils', 'detectAndCleanUtf8'\)",
"array('\\\\{$replace_prefix}\\\\Monolog\\\\Utils', 'detectAndCleanUtf8')",
$contents
);
$contents = safe_replace(
'\\* @return \\\\Google\\\\Client',
"* @return \\{$prefix}\\Google\\Client",
$contents
);
return $contents;
},
),
'prefix' => 'Sgdg\\Vendor',
);