/
view_config_helper.php
executable file
·101 lines (92 loc) · 2.73 KB
/
view_config_helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace PMVC\PlugIn\view_config_helper;
${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\view_config_helper';
\PMVC\initPlugIn(['controller'=>null]);
/**
* @parameters funciton callback
* @parameters funciton .env
*/
class view_config_helper extends \PMVC\PlugIn
{
private $_configs;
public function init()
{
\PMVC\callPlugin(
'dispatcher',
'attach',
[
$this,
\PMVC\Event\WILL_PROCESS_HEADER
]
);
}
public function &getAllViewConfigs()
{
$dot = \PMVC\plug('dotenv');
$configs = [];
$globalView = \PMVC\getOption('VIEW');
if ($globalView) {
$configs = array_replace_recursive($configs, $globalView);
\PMVC\option('set', 'VIEW', null);
}
$i18n = \PMVC\getOption('I18N');
if ($i18n) {
$configs = array_replace_recursive($configs, ['I18N'=>$i18n]);
\PMVC\option('set', 'I18N', null);
}
$dotView = \PMVC\get($this, '.env', '.env.view');
if ($dot->fileExists($dotView)) {
$configs = array_replace_recursive(
$configs,
$dot->getUnderscoreToArray($dotView)
);
}
/*Clean disable section*/
$sections =& \PMVC\get($configs, 'section', []);
$features = \PMVC\getOption('features');
foreach ($sections as $sectionKey=>$section) {
if (empty($section['shouldRender'])) {
unset($sections[$sectionKey]);
continue;
}
$shouldRender = $section['shouldRender'];
if (isset($features[$shouldRender]) &&
empty($features[$shouldRender])
) {
unset($sections[$sectionKey]);
}
}
if ($this['callback']) {
$this['callback']($configs);
}
return $configs;
}
public function toView()
{
if (!empty($this->_configs)) {
return $this->_configs;
}
$this->_configs =& $this->getAllViewConfigs();
if (!$this['getConfigOnly'] &&
\PMVC\getOption(_VIEW_ENGINE) !== 'json'
) {
$view = \PMVC\plug('view');
$view->set($this->_configs);
if (empty($view->get('htmlTitle'))) {
$view->set(
'htmlTitle',
\PMVC\value($this->_configs, ['I18N', 'htmlTitle'])
);
}
}
return $this->_configs;
}
public function onWillProcessHeader($subject)
{
$subject->detach($this);
if (\PMVC\getOption(_VIEW_ENGINE) === 'json') {
return;
}
$this->toView();
}
}