Completed
Push — master ( cfe8fe...f9982c )
by Rougin
04:59
created

Tools   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 97.87%

Importance

Changes 10
Bugs 4 Features 0
Metric Value
wmc 20
c 10
b 4
f 0
lcom 0
cbo 0
dl 0
loc 212
ccs 92
cts 94
cp 0.9787
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hasLayout() 0 7 2
C ignite() 0 96 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
        // Removes the index.php from $config['index_page'].
44 24
        if (strpos($config, '$config[\'index_page\'] = \'index.php\';') !== false) {
45 24
            array_push($search, '$config[\'index_page\'] = \'index.php\';');
46 24
            array_push($replace, '$config[\'index_page\'] = \'\';');
47 24
        }
48
49
        // Adds an encryption key from the configuration.
50 24
        if (strpos($config, '$config[\'encryption_key\'] = \'\';') !== false) {
51 24
            array_push($search, '$config[\'encryption_key\'] = \'\';');
52 24
            array_push($replace, '$config[\'encryption_key\'] = \''.md5('rougin').'\';');
53 24
        }
54
55 24
        $config = str_replace($search, $replace, $config);
56 24
        file_put_contents(APPPATH . 'config/config.php', $config);
57
58
        // Gets data from application/config/autoload.php
59 24
        $autoload = file_get_contents(APPPATH . 'config/autoload.php');
60 24
        $lines = explode(PHP_EOL, $autoload);
61
62
        // Gets the currently included libraries.
63 24
        $pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
64
65 24
        preg_match_all($pattern, $lines[60], $match);
66
67 24
        $libraries = explode(', ', end($match[1]));
68
69
        // Includes "session" library.
70 24
        if ( ! in_array('\'session\'', $libraries)) {
71 24
            array_push($libraries, '\'session\'');
72 24
        }
73
74 24
        $libraries = array_filter($libraries);
75
76
        // Includes the added libraries all back to autoload.php.
77 24
        $pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
78 24
        $replacement = '$autoload[\'libraries\'] = array('.implode(', ', $libraries).');';
79
80 24
        $lines[60] = preg_replace($pattern, $replacement, $lines[60]);
81
82
        // Gets the currently included helpers
83 24
        $pattern = '/\$autoload\[\'helper\'\] = array\((.*?)\)/';
84
85 24
        preg_match_all($pattern, $lines[85], $match);
86
87 24
        $defaultHelpers = [ '\'form\'', '\'url\'', ];
88 24
        $helpers = explode(', ', end($match[1]));
89
90 24
        foreach ($defaultHelpers as $helper) {
91 24
            if ( ! in_array($helper, $helpers)) {
92 24
                array_push($helpers, $helper);
93 24
            }    
94 24
        }
95
96 24
        $helpers = array_filter($helpers);
97
98
        // Include the added helpers all back to autoload.php
99 24
        $pattern = '/\$autoload\[\'helpers\'\] = array\([^)]*\);/';
100 24
        $replacement = '$autoload[\'helpers\'] = array('.implode(', ', $helpers).');';
101
102 24
        preg_replace($pattern, $replacement, $lines[60]);
103
104 24
        file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
105
106
        // Creates a new .htaccess file if it does not exists.
107 24
        if ( ! file_exists('.htaccess')) {
108 24
            $template = __DIR__ . '/../Templates/Htaccess.template';
109
110 24
            $file = fopen('.htaccess', 'wb');
111 24
            $contents = file_get_contents($template);
112
113 24
            file_put_contents('.htaccess', $contents);
114 24
            chmod('.htaccess', 0777);
115 24
            fclose($file);
116 24
        }
117
118
        // Creates a configuration for the Pagination library.
119 24
        if ( ! file_exists(APPPATH . 'config/pagination.php')) {
120 24
            $pagination = APPPATH . 'config/pagination.php';
121 24
            $template = __DIR__ . '/../Templates/Pagination.template';
122
123 24
            $file = fopen($pagination, 'wb');
124 24
            $contents = file_get_contents($template);
125
126 24
            file_put_contents($pagination, $contents);
127 24
            chmod($pagination, 0664);
128 24
            fclose($file);
129 24
        }
130 24
    }
131
132
    /**
133
     * Checks whether the command is enabled or not in the current environment.
134
     *
135
     * @return bool
136
     */
137 6
    public static function isCommandEnabled()
138
    {
139 6
        return self::isWildfireEnabled() || self::isDoctrineEnabled();
140
    }
141
142
    /**
143
     * Checks if Doctrine exists.
144
     *
145
     * @return bool
146
     */
147 6
    public static function isDoctrineEnabled()
148
    {
149 6
        return file_exists(APPPATH . 'libraries/Doctrine.php');
150
    }
151
152
    /**
153
     * Checks if Wildfire exists.
154
     *
155
     * @return bool
156
     */
157 6
    public static function isWildfireEnabled()
158
    {
159 6
        return file_exists(APPPATH . 'libraries/Wildfire.php');
160
    }
161
162
    /**
163
     * Strips the table schema from the table name.
164
     * 
165
     * @param  string $table
166
     * @return string
167
     */
168 12
    public static function stripTableSchema($table)
169
    {
170 12
        return (strpos($table, '.') !== false)
171 12
            ? substr($table, strpos($table, '.') + 1)
172 12
            : $table;
173
    }
174
175
    /**
176
     * Removes the specified library in the application.
177
     * 
178
     * @param  string $type
179
     * @return string
180
     */
181 6
    public static function removeLibrary($type)
182
    {
183 6
        $autoload = file_get_contents(APPPATH . 'config/autoload.php');
184
185 6
        $lines = explode(PHP_EOL, $autoload);
186 6
        $pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
187
188 6
        preg_match_all($pattern, $lines[60], $match);
189
190 6
        $libraries = explode(', ', end($match[1]));
191
192 6
        if (in_array('\'' . $type . '\'', $libraries)) {
193 6
            $position = array_search('\'' . $type . '\'', $libraries);
194
195 6
            unset($libraries[$position]);
196
197 6
            $libraries = array_filter($libraries);
198
199 6
            $pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
200 6
            $replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
201
202 6
            $lines[60] = preg_replace($pattern, $replacement, $lines[60]);
203
204 6
            file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
205 6
        }
206
207 6
        if ($type == 'doctrine') {
208 3
            system('composer remove doctrine/orm');
209 3
        }
210
211 6
        unlink(APPPATH . 'libraries/' . ucfirst($type) . '.php');
212
213 6
        return ucfirst($type) . ' is now successfully removed!';
214
    }
215
216
    /**
217
     * Strips the table schema from the table name.
218
     * 
219
     * @param  string $table
220
     * @return string
221
     */
222
    public static function strip_table_schema($table)
223
    {
224
        return self::stripTableSchema($table);
225
    }
226
}
227