|
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
|
504 |
|
public static function get(string $component, string $key = null) |
|
36
|
|
|
{ |
|
37
|
504 |
|
self::_initialize($component); |
|
38
|
504 |
|
if ($key === null) { |
|
39
|
|
|
return self::$_data[$component]; |
|
40
|
|
|
} |
|
41
|
504 |
|
return self::$_data[$component][$key]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
33 |
|
public static function set(string $component, string $key, $value) |
|
45
|
|
|
{ |
|
46
|
33 |
|
self::_initialize($component); |
|
47
|
33 |
|
self::$_data[$component][$key] = $value; |
|
48
|
33 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Clear static cache |
|
52
|
|
|
* |
|
53
|
|
|
* @internal |
|
54
|
|
|
*/ |
|
55
|
580 |
|
public static function reset() |
|
56
|
|
|
{ |
|
57
|
580 |
|
self::$_data = []; |
|
58
|
580 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Initialize the global data storage |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $component |
|
64
|
|
|
*/ |
|
65
|
505 |
|
private static function _initialize(string $component) |
|
66
|
|
|
{ |
|
67
|
505 |
|
if (!array_key_exists($component, self::$_data)) { |
|
68
|
457 |
|
self::$_data[$component] = [ |
|
69
|
|
|
'active_leaf' => false, |
|
70
|
|
|
'config' => [] |
|
71
|
|
|
]; |
|
72
|
457 |
|
self::_load_configuration($component); |
|
73
|
|
|
} |
|
74
|
505 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Loads the configuration file specified by the component configuration |
|
78
|
|
|
* and constructs a midcom_helper_configuration object out of it. Both |
|
79
|
|
|
* Component defaults and sitegroup-configuration gets merged. The |
|
80
|
|
|
* resulting object is stored under the key 'config' in the |
|
81
|
|
|
* component's data storage area. |
|
82
|
|
|
* |
|
83
|
|
|
* Three files will be loaded in order: |
|
84
|
|
|
* |
|
85
|
|
|
* 1. The component's default configuration, placed in $prefix/config/config.inc |
|
86
|
|
|
* 2. Any systemwide default configuration, currently placed in midcom::get()->config->get('midcom_config_basedir')/midcom/$component/config.inc. |
|
87
|
|
|
* 3. Any site configuration in the snippet midcom::get()->config->get('midcom_sgconfig_basedir')/$component/config. |
|
88
|
|
|
* |
|
89
|
|
|
* @see midcom_helper_configuration |
|
90
|
|
|
*/ |
|
91
|
457 |
|
private static function _load_configuration(string $component) |
|
92
|
|
|
{ |
|
93
|
457 |
|
$data = []; |
|
94
|
457 |
|
$component_path = midcom::get()->componentloader->path_to_snippetpath($component); |
|
95
|
|
|
// Load and parse the global config |
|
96
|
457 |
|
if ($component_data = self::read_array_from_file($component_path . '/config/config.inc')) { |
|
97
|
451 |
|
$data = array_merge($data, $component_data); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Go for the sitewide default |
|
101
|
457 |
|
$data = array_merge($data, self::read_array_from_snippet("conf:/{$component}/config.inc")); |
|
102
|
|
|
|
|
103
|
|
|
// Finally, check the sitegroup config |
|
104
|
457 |
|
$data = array_merge($data, self::read_array_from_snippet(midcom::get()->config->get('midcom_sgconfig_basedir') . "/{$component}/config")); |
|
105
|
|
|
|
|
106
|
457 |
|
self::$_data[$component]['config'] = new midcom_helper_configuration($data); |
|
107
|
457 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Read a file from disk and evaluate its content as array. |
|
111
|
|
|
* This is essentially a simple [$data\n] eval construct. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $filename The name of the file that should be parsed. |
|
114
|
|
|
*/ |
|
115
|
460 |
|
public static function read_array_from_file(string $filename) : array |
|
116
|
|
|
{ |
|
117
|
460 |
|
if (!is_readable($filename)) { |
|
118
|
22 |
|
return []; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
459 |
|
return midcom_helper_misc::parse_config(file_get_contents($filename), $filename); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Read a snippet and evaluate its content as array. |
|
126
|
|
|
* This is essentially a simple [$data\n] eval construct. |
|
127
|
|
|
* |
|
128
|
|
|
* If the snippet does not exist, false is returned. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $snippetpath The full path to the snippet that should be returned. |
|
131
|
|
|
* @see read_array_from_file() |
|
132
|
|
|
*/ |
|
133
|
457 |
|
public static function read_array_from_snippet(string $snippetpath) : array |
|
134
|
|
|
{ |
|
135
|
457 |
|
$code = midcom_helper_misc::get_snippet_content_graceful($snippetpath); |
|
136
|
457 |
|
return midcom_helper_misc::parse_config($code, $snippetpath); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|