Cms_hooks::build_hooks()   C
last analyzed

Complexity

Conditions 12
Paths 36

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 36
nop 0
dl 0
loc 55
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
class Cms_hooks
8
{
9
10
    private $hooks_file = NULL;
11
12
    public function __construct() {
13
        $ci = & get_instance();
14
15
        if (!$ci->config->item('is_installed') && !function_exists('get_hook')) {
16
17
            function get_hook() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
                return FALSE;
19
            }
20
21
            return FALSE;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
22
        }
23
24
        $this->hooks_file = BASEPATH . 'cache/hooks' . EXT;
25
26
        if (!file_exists($this->hooks_file) OR $ci->config->item('rebuild_hooks_tree') === TRUE) {
27
            $this->build_hooks();
28
        }
29
30
        if (file_exists($this->hooks_file)) {
31
            include $this->hooks_file;
32
        } else {
33
            show_error('Ошибка загрузки файла хуков.');
34
        }
35
    }
36
37
    public function build_hooks() {
38
        $ci = \CI::$APP;
39
40
        $ci->load->library('lib_xml');
41
42
        $xml = '<?xml version="1.0" encoding="UTF-8"?><hooks>';
43
44
        // Get all installed modules
45
        $ci->db->select('name');
46
47
        $modules = $ci->db->get('components');
48
49
        if ($modules) {
50
            $modules = $modules->result_array();
51
        } else {
52
            show_error($ci->db->_error_message());
53
        }
54
55
        $modules[]['name'] = 'core';
56
57
        // Search for hooks.xml in all installed modules
58
        foreach ($modules as $m) {
59
            $xml_file = getModulePath($m['name']) . '/hooks.xml';
60
            if (file_exists($xml_file)) {
61
                $xml .= file_get_contents($xml_file);
62
            }
63
        }
64
65
        $xml .= "\n</hooks>";
66
67
        $parser = xml_parser_create();
68
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
69
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
70
        xml_parse_into_struct($parser, $xml, $vals);
71
        xml_parser_free($parser);
72
73
        $tmp = [];
74
75
        foreach ($vals as $k => $v) {
76
            if (isset($v['type']) && isset($v['value']) && isset($v['attributes'])) {
77
                $hookId = trim($v['attributes']['id']);
78
79
                if (empty($tmp[$hookId])) {
80
                    $tmp[$hookId] = '';
81
                }
82
83
                $hookValue = trim($v['value']);
84
                if ($v['type'] === 'complete' && !empty($hookValue) && !empty($hookId)) {
85
                    $tmp[$hookId] .= $hookValue;
86
                }
87
            }
88
        }
89
90
        $this->create_hooks_file($tmp);
91
    }
92
93
    private function create_hooks_file($hooks_arr = []) {
94
        $ci = & get_instance();
95
        $ci->load->helper('file');
96
97
        $tmp = '';
98
99
        if (count($hooks_arr) > 0) {
100
            foreach ($hooks_arr as $k => $v) {
101
                $tmp = $tmp . '\'' . $k . '\'' . ' => \'' . str_replace("'", "\'", $v) . '\',' . "\n";
102
            }
103
        }
104
105
        $tmp = str_replace("\\\'", "\'", $tmp);
106
107
        $template = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
108
109
function get_hook(\$hook_id)
110
{
111
\$cms_hooks = array (
112
    $tmp
113
);
114
115
    if (isset(\$cms_hooks[\$hook_id]))
116
    {
117
        return \$cms_hooks[\$hook_id];
118
    }
119
    else
120
    {
121
       return FALSE;
122
    }
123
}
124
125
";
126
        mkdir('./system/cache/templates_c', 0777, TRUE);
127
128
        write_file($this->hooks_file, $template);
129
    }
130
131
}