Completed
Push — master ( e4df89...205b3c )
by Kenji
02:19
created

Installer.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Part of ci-phpunit-test
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2015 Kenji Suzuki
8
 * @link       https://github.com/kenjis/ci-phpunit-test
9
 */
10
11
class Installer
12
{
13
    const TEST_FOLDER = 'tests';
14
15
    private $silent = false;
16
17
    public function __construct($silent = false)
18
    {
19
        $this->silent = $silent;
20
    }
21
22 View Code Duplication
    public static function install($app = 'application')
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        self::recursiveCopy(
25
            dirname(__FILE__) . '/application/tests',
26
            $app . '/' . static::TEST_FOLDER
27
        );
28
        self::fixPath($app);
29
    }
30
31
    /**
32
     * Fix paths in Bootstrap.php
33
     */
34
    private static function fixPath($app = 'application')
35
    {
36
        $file = $app . '/' . static::TEST_FOLDER . '/Bootstrap.php';
37
        $contents = file_get_contents($file);
38
        
39
        if (! file_exists('system')) {
40
            if (file_exists('vendor/codeigniter/framework/system')) {
41
                $contents = str_replace(
42
                    '$system_path = \'../../system\';',
43
                    '$system_path = \'../../vendor/codeigniter/framework/system\';',
44
                    $contents
45
                );
46
            } else {
47
                throw new Exception('Can\'t find "system" folder.');
48
            }
49
        }
50
        
51
        if (! file_exists('index.php')) {
52
            if (file_exists('public/index.php')) {
53
                // CodeIgniter 3.0.6 and after
54
                $contents = str_replace(
55
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);",
56
                    "define('FCPATH', realpath(dirname(__FILE__).'/../../public').DIRECTORY_SEPARATOR);",
57
                    $contents
58
                );
59
                // CodeIgniter 3.0.5 and before
60
                $contents = str_replace(
61
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');",
62
                    "define('FCPATH', realpath(dirname(__FILE__).'/../../public').'/');",
63
                    $contents
64
                );
65
            } elseif (file_exists($app . '/public/index.php')) {
66
                // CodeIgniter 3.0.6 and after
67
                $contents = str_replace(
68
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);",
69
                    "define('FCPATH', realpath(dirname(__FILE__).'/../public').DIRECTORY_SEPARATOR);",
70
                    $contents
71
                );
72
                // CodeIgniter 3.0.5 and before
73
                $contents = str_replace(
74
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');",
75
                    "define('FCPATH', realpath(dirname(__FILE__).'/../public').'/');",
76
                    $contents
77
                );
78
                if ($app != 'application') {
79
                    $contents = str_replace(
80
                        "\$application_folder = '../../application';",
81
                        "\$application_folder = '../../{$app}';",
82
                        $contents
83
                    );
84
                }
85
            } else {
86
                throw new Exception('Can\'t find "index.php".');
87
            }
88
        }
89
        
90
        file_put_contents($file, $contents);
91
    }
92
93 View Code Duplication
    public static function update($app = 'application')
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $target_dir = $app . '/' . static::TEST_FOLDER . '/_ci_phpunit_test';
96
        self::recursiveUnlink($target_dir);
97
        self::recursiveCopy(
98
            dirname(__FILE__) . '/application/tests/_ci_phpunit_test',
99
            $target_dir
100
        );
101
    }
102
103
    /**
104
     * Recursive Copy
105
     *
106
     * @param string $src
107
     * @param string $dst
108
     */
109
    private static function recursiveCopy($src, $dst)
110
    {
111
        @mkdir($dst, 0755);
112
        
113
        $iterator = new \RecursiveIteratorIterator(
114
            new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS),
115
            \RecursiveIteratorIterator::SELF_FIRST
116
        );
117
        
118
        foreach ($iterator as $file) {
119
            if ($file->isDir()) {
120
                @mkdir($dst . '/' . $iterator->getSubPathName());
121
            } else {
122
                $success = copy($file, $dst . '/' . $iterator->getSubPathName());
123
                if ($success) {
124
                    if (! $this->silent) {
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
125
                        echo 'copied: ' . $dst . '/' . $iterator->getSubPathName() . PHP_EOL;
126
                    }
127
                }
128
            }
129
        }
130
    }
131
132
    /**
133
     * Recursive Unlink
134
     *
135
     * @param string $dir
136
     */
137 View Code Duplication
    private static function recursiveUnlink($dir)
138
    {
139
        $iterator = new \RecursiveIteratorIterator(
140
            new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
141
            \RecursiveIteratorIterator::CHILD_FIRST
142
        );
143
        
144
        foreach ($iterator as $file) {
145
            if ($file->isDir()) {
146
                rmdir($file);
147
            } else {
148
                unlink($file);
149
            }
150
        }
151
        
152
        rmdir($dir);
153
    }
154
}
155