cloneFileFolder()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 30
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 1
1
<?php
2
/*
3
4
Usage:
5
6
    Copy clone.php in <xoops_root>
7
    Change current working directory to <xoops_root>
8
    Update mappings as per new modulename.
9
10
    php -q clone.php
11
12
*/
13
14
// ##########################################################
15
//  Define your mapping here
16
// ##########################################################
17
$patterns = [
18
    // first one must be module directory name
19
    'oledrion' => 'bouquins',
20
    'OLEDRION' => 'BOUQUINS',
21
    'Oledrion' => 'Bouquins',
22
];
23
24
$patKeys   = array_keys($patterns);
25
$patValues = array_values($patterns);
26
27
// work around for PHP < 5.0.x
28
if (!function_exists('file_put_contents')) {
29
    /**
30
     * @param      $filename
31
     * @param      $data
32
     * @param bool $file_append
33
     */
34
    function file_put_contents($filename, $data, $file_append = false)
35
    {
36
        $fp = fopen($filename, (!$file_append ? 'w+' : 'a+'));
37
        if (!$fp) {
0 ignored issues
show
introduced by
$fp is of type resource, thus it always evaluated to false.
Loading history...
38
            trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
39
40
            return;
41
        }
42
        fwrite($fp, $data);
43
        fclose($fp);
44
    }
45
}
46
47
// recursive clonning script
48
/**
49
 * @param $path
50
 */
51
function cloneFileFolder($path)
52
{
53
    global $patKeys;
54
    global $patValues;
55
56
    $newPath = str_replace($patKeys[0], $patValues[0], $path);
57
58
    if (is_dir($path)) {
59
        // create new dir
60
        if (!mkdir($newPath) && !is_dir($newPath)) {
61
            throw new \RuntimeException(sprintf('Directory "%s" was not created', $newPath));
62
        }
63
64
        // check all files in dir, and process it
65
        if (false !== ($handle = opendir($path))) {
66
            while (false !== ($file = readdir($handle))) {
67
                if ('.' !== $file && '..' !== $file) {
68
                    cloneFileFolder("$path/$file");
69
                }
70
            }
71
            closedir($handle);
72
        }
73
    } else {
74
        if (preg_match('/(.jpg|.gif|.png|.zip)$/i', $path)) {
75
            copy($path, $newPath);
76
        } else {
77
            // file, read it
78
            $content = file_get_contents($path);
79
            $content = str_replace($patKeys, $patValues, $content);
80
            file_put_contents($newPath, $content);
81
        }
82
    }
83
}
84
85
cloneFileFolder('modules/oledrion');
86
87
echo "Happy cloning...\n";
88
echo 'check directory modules/' . $patterns['oledrion'] . " for cloned module \n";
89
echo "Consider modifying new module by editing language/english/modinfo.php and assets/images/oledrion_logo.png manually (if you care)\n";
90