Completed
Push — master ( f3af33...267f53 )
by Rougin
03:52
created

Tools   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 97.94%

Importance

Changes 12
Bugs 4 Features 0
Metric Value
wmc 20
c 12
b 4
f 0
lcom 0
cbo 0
dl 0
loc 219
ccs 95
cts 97
cp 0.9794
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hasLayout() 0 7 2
C ignite() 0 103 8
A isCommandEnabled() 0 4 2
A isDoctrineEnabled() 0 4 1
A isWildfireEnabled() 0 4 1
A stripTableSchema() 0 6 2
B removeLibrary() 0 34 3
A strip_table_schema() 0 4 1
1
<?php
2
3
namespace Rougin\Combustor\Common;
4
5
use Rougin\Combustor\Common\File;
6
7
/**
8
 * Tools
9
 *
10
 * Provides a list of multi-purpose functions for Combustor.
11
 * 
12
 * @package Combustor
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class Tools
16
{
17
    /**
18
     * Checks whether the header and footer file exists.
19
     *
20
     * @return bool
21
     */
22 6
    public static function hasLayout()
23
    {
24 6
        $header = APPPATH . 'views/layout/header.php';
25 6
        $footer = APPPATH . 'views/layout/footer.php';
26
27 6
        return file_exists($header) && file_exists($footer);
28
    }
29
30
    /**
31
     * "Ignites" the post installation process.
32
     * 
33
     * @return void
34
     */
35 24
    public static function ignite()
36
    {
37
        // Gets data from application/config/config.php
38 24
        $config = file_get_contents(APPPATH . 'config/config.php');
39
40 24
        $search = ['$config[\'composer_autoload\'] = FALSE;'];
41 24
        $replace = ['$config[\'composer_autoload\'] = realpath(\'vendor\') . \'/autoload.php\';'];
42
43
        // Replaces configuration found in config.php
44
        $configs = [
45
            [
46 24
                'search' => '$config[\'index_page\'] = \'index.php\';',
47
                'replacement' => '$config[\'index_page\'] = \'\';'
48 24
            ],
49
            [
50 24
                'search' => '$config[\'encryption_key\'] = \'\';',
51
                'replacement' => '$config[\'encryption_key\'] = \'md5(\'rougin\')\';'
52 24
            ]
53 24
        ];
54
55 24
        foreach ($configs as $row) {
56 24
            if (strpos($config, $row['search']) !== false) {
57 24
                array_push($search, $row['search']);
58 24
                array_push($replace, $row['replacement']);
59 24
            }
60 24
        }
61
62 24
        $config = str_replace($search, $replace, $config);
63 24
        file_put_contents(APPPATH . 'config/config.php', $config);
64
65
        // Gets data from application/config/autoload.php
66 24
        $autoload = file_get_contents(APPPATH . 'config/autoload.php');
67 24
        $lines = explode(PHP_EOL, $autoload);
68
69
        // Gets the currently included libraries.
70 24
        $pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
71
72 24
        preg_match_all($pattern, $lines[60], $match);
73
74 24
        $libraries = explode(', ', end($match[1]));
75
76
        // Includes "session" library.
77 24
        if ( ! in_array('\'session\'', $libraries)) {
78 24
            array_push($libraries, '\'session\'');
79 24
        }
80
81 24
        $libraries = array_filter($libraries);
82
83
        // Includes the added libraries all back to autoload.php.
84 24
        $pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
85 24
        $replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
86
87 24
        $lines[60] = preg_replace($pattern, $replacement, $lines[60]);
88
89
        // Gets the currently included helpers
90 24
        $pattern = '/\$autoload\[\'helper\'\] = array\((.*?)\)/';
91
92 24
        preg_match_all($pattern, $lines[85], $match);
93
94 24
        $defaultHelpers = [ '\'form\'', '\'url\'', ];
95 24
        $helpers = explode(', ', end($match[1]));
96
97 24
        foreach ($defaultHelpers as $helper) {
98 24
            if ( ! in_array($helper, $helpers)) {
99 24
                array_push($helpers, $helper);
100 24
            }
101 24
        }
102
103 24
        $helpers = array_filter($helpers);
104
105
        // Include the added helpers all back to autoload.php
106 24
        $pattern = '/\$autoload\[\'helpers\'\] = array\([^)]*\);/';
107 24
        $replacement = '$autoload[\'helpers\'] = array(' . implode(', ', $helpers) . ');';
108
109 24
        preg_replace($pattern, $replacement, $lines[60]);
110
111 24
        file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
112
113
        // Creates a new .htaccess file if it does not exists.
114 24
        if ( ! file_exists('.htaccess')) {
115 24
            $template = __DIR__ . '/../Templates/Htaccess.template';
116
117 24
            $file = fopen('.htaccess', 'wb');
118 24
            $contents = file_get_contents($template);
119
120 24
            file_put_contents('.htaccess', $contents);
121 24
            chmod('.htaccess', 0777);
122 24
            fclose($file);
123 24
        }
124
125
        // Creates a configuration for the Pagination library.
126 24
        if ( ! file_exists(APPPATH . 'config/pagination.php')) {
127 24
            $pagination = APPPATH . 'config/pagination.php';
128 24
            $template = __DIR__ . '/../Templates/Pagination.template';
129
130 24
            $file = fopen($pagination, 'wb');
131 24
            $contents = file_get_contents($template);
132
133 24
            file_put_contents($pagination, $contents);
134 24
            chmod($pagination, 0664);
135 24
            fclose($file);
136 24
        }
137 24
    }
138
139
    /**
140
     * Checks whether the command is enabled or not in the current environment.
141
     *
142
     * @return bool
143
     */
144 6
    public static function isCommandEnabled()
145
    {
146 6
        return self::isWildfireEnabled() || self::isDoctrineEnabled();
147
    }
148
149
    /**
150
     * Checks if Doctrine exists.
151
     *
152
     * @return bool
153
     */
154 3
    public static function isDoctrineEnabled()
155
    {
156 3
        return file_exists(APPPATH . 'libraries/Doctrine.php');
157
    }
158
159
    /**
160
     * Checks if Wildfire exists.
161
     *
162
     * @return bool
163
     */
164 6
    public static function isWildfireEnabled()
165
    {
166 6
        return file_exists(APPPATH . 'libraries/Wildfire.php');
167
    }
168
169
    /**
170
     * Strips the table schema from the table name.
171
     * 
172
     * @param  string $table
173
     * @return string
174
     */
175 12
    public static function stripTableSchema($table)
176
    {
177 12
        return (strpos($table, '.') !== false)
178 12
            ? substr($table, strpos($table, '.') + 1)
179 12
            : $table;
180
    }
181
182
    /**
183
     * Removes the specified library in the application.
184
     * 
185
     * @param  string $type
186
     * @return string
187
     */
188 6
    public static function removeLibrary($type)
189
    {
190 6
        $autoload = file_get_contents(APPPATH . 'config/autoload.php');
191
192 6
        $lines = explode(PHP_EOL, $autoload);
193 6
        $pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
194
195 6
        preg_match_all($pattern, $lines[60], $match);
196
197 6
        $libraries = explode(', ', end($match[1]));
198
199 6
        if (in_array('\'' . $type . '\'', $libraries)) {
200 6
            $position = array_search('\'' . $type . '\'', $libraries);
201
202 6
            unset($libraries[$position]);
203
204 6
            $libraries = array_filter($libraries);
205
206 6
            $pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
207 6
            $replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
208
209 6
            $lines[60] = preg_replace($pattern, $replacement, $lines[60]);
210
211 6
            file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
212 6
        }
213
214 6
        if ($type == 'doctrine') {
215 3
            system('composer remove doctrine/orm');
216 3
        }
217
218 6
        unlink(APPPATH . 'libraries/' . ucfirst($type) . '.php');
219
220 6
        return ucfirst($type) . ' is now successfully removed!';
221
    }
222
223
    /**
224
     * Strips the table schema from the table name.
225
     * 
226
     * @param  string $table
227
     * @return string
228
     */
229
    public static function strip_table_schema($table)
230
    {
231
        return self::stripTableSchema($table);
232
    }
233
}
234