summarylogtreecommitdiffstats
path: root/php-makefile-patcher.php
blob: 7a071eeba66d97b9cf2b92a20d8c9bba4f45c0f8 (plain)
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
<?php
if (!isset($argv, $argc)) {
    echo "No command line args provided\n";
    exit(1);
}
if ($argc < 2) {
    echo "Not all command line args provided\n";
    exit(2);
}
$filename = $argv[1];
$fileContent = @file_get_contents($filename);
if (!strlen($fileContent)) {
    echo "No file contents of $filename\n";
    exit(3);
}
$matches = array();
$match = preg_match("/^(?P<line>(?P<definition>PHP_MODULES[\s\t]+=[\s\t]+)(?P<expression>.*)$)/m", $fileContent, $matches);
if (!$match) {
    echo "No PHP_MODULES in $filename\n";
    exit (4);
}
$expression = explode(' ', $matches['expression']);
function sortByPrio($a, $b) {
    $aPrio = 999;
    $bPrio = 999;
    $priorities = array('/openssl/i'=> 0, '@\/xml\.@i'=>1, '@\/pdo\.@i'=>2, '@\/dom\.@i'=>3, '/mysqlnd/i'=>4);
    foreach ($priorities as $regex => $prio) {
        if (preg_match($regex, $a) && $prio < $aPrio) {
            $aPrio = $prio;
        }
        if (preg_match($regex, $b) && $prio < $bPrio) {
            $bPrio = $prio;
        }
    }
    if ($aPrio == $bPrio) {
        return 0;
    }
    return $aPrio > $bPrio ? 1: -1;
}
usort($expression, 'sortByPrio');
$expression = $matches['definition'].join (' ', $expression)."\n";
$fileContent = str_replace($matches['line'], $expression, $fileContent);
if (!file_put_contents($filename, $fileContent)) {
    echo "Failed to write to $filename\n";
    exit(5);
}
exit(0);