Completed
Push — master ( 0f0d6a...e070b7 )
by Rougin
05:03
created

Tools::strip_table_schema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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