Passed
Push — master ( 5a0590...0c08dd )
by Andreas
49:13 queued 24:34
created

midcom_baseclasses_components_configuration   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 129
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 3
A _initialize() 0 7 1
A reset() 0 3 1
A set() 0 7 2
A read_array_from_snippet() 0 4 1
A read_array_from_file() 0 7 2
A _load_configuration() 0 16 2
1
<?php
2
/**
3
 * @package midcom.baseclasses
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Configuration class for components.
11
 *
12
 * @package midcom.baseclasses
13
 */
14
class midcom_baseclasses_components_configuration
15
{
16
    /**
17
     * Component specific global data storage area, which should
18
     * be used for stuff like default configurations etc. thus avoiding the
19
     * pollution of the global namespace. Each component has its own array
20
     * in the global one, allowing storage of arbitrary data indexed by arbitrary
21
     * keys in there. The component-specific arrays are indexed by their
22
     * name.
23
     *
24
     * Note, that this facility is quite a different thing to the component
25
     * context from midcom_application, even if it has many similar applications.
26
     * The component context is only available and valid for components, which
27
     * are actually handling a request. This data storage area is static to the
28
     * complete component and shared over all subrequests and therefore suitable
29
     * to hold default configurations, -schemas and the like.
30
     *
31
     * @var array
32
     */
33
    private static $_data = [];
34
35 500
    public static function get(string $component, $key = null)
36
    {
37 500
        if (!array_key_exists($component, self::$_data)) {
38 478
            self::_initialize($component);
39
        }
40
41 500
        if ($key === null) {
42 14
            return self::$_data[$component];
43
        }
44 500
        return self::$_data[$component][$key];
45
    }
46
47 33
    public static function set(string $component, string $key, $value)
48
    {
49 33
        if (!array_key_exists($component, self::$_data)) {
50 24
            self::_initialize($component);
51
        }
52
53 33
        self::$_data[$component][$key] = $value;
54 33
    }
55
56
    /**
57
     * Clear static cache
58
     *
59
     * @internal
60
     */
61 735
    public static function reset()
62
    {
63 735
        self::$_data = [];
64 735
    }
65
66
    /**
67
     * Initialize the global data storage
68
     *
69
     * @param string $component
70
     */
71 489
    private static function _initialize(string $component)
72
    {
73 489
        self::$_data[$component] = [
74
            'active_leaf' => false,
75
            'config' => []
76
        ];
77 489
        self::_load_configuration($component);
78 489
    }
79
80
    /**
81
     * Loads the configuration file specified by the component configuration
82
     * and constructs a midcom_helper_configuration object out of it. Both
83
     * Component defaults and sitegroup-configuration gets merged. The
84
     * resulting object is stored under the key 'config' in the
85
     * component's data storage area.
86
     *
87
     * Three files will be loaded in order:
88
     *
89
     * 1. The component's default configuration, placed in $prefix/config/config.inc
90
     * 2. Any systemwide default configuration, currently placed in midcom::get()->config->get('midcom_config_basedir')/midcom/$component/config.inc.
91
     * 3. Any site configuration in the snippet midcom::get()->config->get('midcom_sgconfig_basedir')/$component/config.
92
     *
93
     * @see midcom_helper_configuration
94
     */
95 489
    private static function _load_configuration(string $component)
96
    {
97 489
        $data = [];
98 489
        $component_path = midcom::get()->componentloader->path_to_snippetpath($component);
99
        // Load and parse the global config
100 489
        if ($component_data = self::read_array_from_file($component_path . '/config/config.inc')) {
101 483
            $data = array_merge($data, $component_data);
102
        }
103
104
        // Go for the sitewide default
105 489
        $data = array_merge($data, self::read_array_from_snippet("conf:/{$component}/config.inc"));
106
107
        // Finally, check the sitegroup config
108 489
        $data = array_merge($data, self::read_array_from_snippet(midcom::get()->config->get('midcom_sgconfig_basedir') . "/{$component}/config"));
109
110 489
        self::$_data[$component]['config'] = new midcom_helper_configuration($data);
111 489
    }
112
113
    /**
114
     * Read a file from disk and evaluate its content as array.
115
     * This is essentially a simple [$data\n] eval construct.
116
     *
117
     * @param string $filename The name of the file that should be parsed.
118
     * @return Array The read data
119
     */
120 492
    public static function read_array_from_file(string $filename) : array
121
    {
122 492
        if (!file_exists($filename)) {
123 22
            return [];
124
        }
125
126 491
        return midcom_helper_misc::parse_config(file_get_contents($filename));
127
    }
128
129
    /**
130
     * Read a snippet and evaluate its content as array.
131
     * This is essentially a simple [$data\n] eval construct.
132
     *
133
     * If the snippet does not exist, false is returned.
134
     *
135
     * @param string $snippetpath The full path to the snippet that should be returned.
136
     * @return Array The read data or false on failure.
137
     * @see read_array_from_file()
138
     */
139 489
    public static function read_array_from_snippet(string $snippetpath) : array
140
    {
141 489
        $code = midcom_helper_misc::get_snippet_content_graceful($snippetpath);
142 489
        return midcom_helper_misc::parse_config($code);
143
    }
144
}
145