Completed
Branch TEMP/gza_copy_hack (387094)
by Juliette
07:19
created

PHPCompatibility_Scripts_Install::remove_copy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
1
<?php
2
3
/**
4
 * Install functions
5
 *
6
 * Used through auto-scripts from Composer and vendor/bin/phpcompat_(en|dis)able/update
7
 *
8
 */
9
10
class PHPCompatibility_Scripts_Install
11
{
12
13
    /**
14
     * Files which need to be copied over and live in the root directory of this project.
15
     *
16
     * @var array
17
     */
18
    public static $root_dir_files = array(
19
        'ruleset.xml',
20
        'Sniff.php',
21
    );
22
23
24
    public static function enable()
25
    {
26
        echo "(Re-)Enabling PHPCompatibility\n";
27
        self::make_copy();
28
    }
29
30
31
    public static function disable()
32
    {
33
        self::remove_copy();
34
    }
35
36
37
    public static function update()
38
    {
39
        self::disable();
40
        self::enable();
41
    }
42
43
44
    protected static function make_copy()
45
    {
46
        $srcDir = dirname(__DIR__);
47
        $copy = dirname(__DIR__).DIRECTORY_SEPARATOR.'PHPCompatibility';
48
49
        if ( file_exists ($copy)) {
50
            echo "Copy workaround is already in place\n";
51
            return;
52
        }
53
54
        if (mkdir($copy) === true) {
55
            foreach (self::$root_dir_files as $filename) {
56
                copy($srcDir.DIRECTORY_SEPARATOR.$filename, $copy.DIRECTORY_SEPARATOR.$filename);
57
            }
58
59
            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
60
                $copy = str_replace('/', DIRECTORY_SEPARATOR, $copy);
61
                $srcDir = str_replace('/', DIRECTORY_SEPARATOR, $srcDir);
62
                passthru('xcopy "'.$srcDir .DIRECTORY_SEPARATOR.'Sniffs" "'.$copy.DIRECTORY_SEPARATOR.'Sniffs" /S /E /I');
63
            } else {
64
                passthru('cp -r "'.$srcDir .DIRECTORY_SEPARATOR.'Sniffs" "'.$copy.DIRECTORY_SEPARATOR.'Sniffs"');
65
            }
66
            echo "Created copy workaround\n";
67
        } else {
68
            echo "Failed to create the $copy directory\n";
69
        }
70
    }
71
72
73
    protected static function remove_copy()
74
    {
75
        $copy = dirname(__DIR__).DIRECTORY_SEPARATOR.'PHPCompatibility';
76
77
        if ( ! file_exists ($copy)) {
78
            echo "No copy workaround to remove\n";
79
            return;
80
        }
81
82
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
83
            $copy = str_replace('/', DIRECTORY_SEPARATOR, $copy);
84
            passthru('rmdir /S /Q "'.$copy.'"');
85
        } else {
86
            passthru('rm -rf "'.$copy.'"');
87
        }
88
        echo "Copy workaround removed\n";
89
    }
90
91
92
}
93