-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtwig-patcher.php
55 lines (44 loc) · 1.55 KB
/
twig-patcher.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
<?php
/**
* This class renames Twig's global "twig_*" functions so that they have their own "dreitier_nadi_" prefix
* @issue #185
*/
define("VENDOR_REPACKAGED_DIR", dirname(__FILE__) . "/vendor-repackaged");
define("TWIG_DIR", "twig/twig/src");
define("PREFIX", "dreitier_nadi_");
$patchedFiles = [];
$iterator = new RecursiveDirectoryIterator(VENDOR_REPACKAGED_DIR . '/' . TWIG_DIR);
foreach (new RecursiveIteratorIterator($iterator) as $file) {
// only pick PHP files which are not PHPUnit test cases
if (
$file->isFile()
&& !$file->isDir()
&& $file->getExtension() == 'php'
) {
$path = $file->getRealpath();
$content = file_get_contents($path);
// find each twig_* function
if (preg_match_all("/(?<preambel>.*)(?<function>twig\_([\w|\_]*))+/", $content, $r)) {
$alreadyRemapped = [];
foreach ($r['function'] as $idx => $functionName) {
$preambel = $r['preambel'][$idx];
# echo $preambel . PHP_EOL;
# echo $functionName . PHP_EOL;
// do not map already mapped files
if (str_ends_with($preambel, PREFIX) || isset($alreadyRemapped[$functionName])) {
#echo "-> Already mapped" . PHP_EOL;
continue;
}
$remappedFunction = PREFIX . '_' . $functionName;
# echo $remappedFunction . PHP_EOL;
$content = str_replace($functionName, $remappedFunction, $content);
$alreadyRemapped[$functionName] = true;
}
file_put_contents($path, $content);
if (!str_ends_with($file->getFilename(), "TestCase.php")) {
$patchedFiles[] = $path;
}
}
}
}
include_once(VENDOR_REPACKAGED_DIR . "/autoload.php");