PHP Classes

File: patch.php

Recommend this page to a friend!
  Classes of Punto Waskito   PHP CRUD API   patch.php   Download  
File: patch.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: PHP CRUD API
Provide API to manipulate database table records
Author: By
Last change:
Date: 3 years ago
Size: 1,522 bytes
 

Contents

Class file image Download
<?php

// patch files for PHP 7.0 compatibility

function patchDir(string $base, string $dir): int
{
   
$count = 0;
   
$entries = scandir($dir);
    foreach (
$entries as $entry) {
        if (
$entry === '.' || $entry === '..') {
            continue;
        }
       
$filename = "$base/$dir/$entry";
        if (
is_dir($filename)) {
           
$count += patchDir($base, "$dir/$entry");
        }
    }
    foreach (
$entries as $entry) {
       
$filename = "$base/$dir/$entry";
        if (
is_file($filename)) {
            if (
substr($entry, -4) != '.php') {
                continue;
            }
           
$patched = $original = file_get_contents($filename);
           
$patched = preg_replace('/\):\s*(\?[a-zA-Z]+|void)\s*\n/', ") /*:$1*/\n", $patched);
           
$patched = preg_replace('/([\(,])\s*(\?[a-zA-Z]+|void)\s+\$/', "$1 /*$2*/ \$", $patched);
           
$patched = preg_replace('/(private|public|protected) const/', "/*$1*/ const", $patched);
            if (
$patched && $patched != $original) {
               
file_put_contents($filename, $patched);
               
$count++;
            }
        }
    }
    return
$count;
}

function
patch(string $base, array $dirs)
{
   
$start = microtime(true);
   
$count = 0;
    foreach (
$dirs as $dir) {
       
$count += patchDir($base, $dir);
    }
   
$end = microtime(true);
   
$time = ($end - $start) * 1000;
    if (
$count) {
       
fwrite(STDERR, sprintf("%d files patched in %d ms\n", $count, $time));
    }
}

patch(__DIR__, ['vendor']);