-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewsController.php
163 lines (143 loc) · 4.12 KB
/
ViewsController.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Colibri\Controller;
use Colibri\Config\Config;
use Colibri\View\Layout;
use Colibri\View\PhpTemplate;
/**
* Base Controller for Views.
* Extends from this class & place code for actions methods.
* Corresponding template wil be used automatically.
*
* @property bool $showProfilerInfoOnDebug ...
* @property bool $showAppDevToolsOnDebug ...
*/
abstract class ViewsController extends Base
{
/**
* @var PhpTemplate
*/
protected $template = null;
/**
* @var bool tells to core to use template or not
*/
protected $useTemplate = true;
/**
* @var bool
*/
protected $useLayout = true;
/**
* @var bool
*/
protected $_showProfilerInfoOnDebug = true;
/**
* @var bool
*/
protected $_showAppDevToolsOnDebug = true;
/**
* @var string
*/
private $divisionPath = null;
/**
* @var string
*/
private $divisionPrefix = null;
/**
* @var string
*/
private $divisionPostfix = null;
/**
* Initialize private $divisionPath, $divisionPrefix, $divisionPostfix.
*/
protected function init()
{
parent::init();
$this->divisionPath = $this->_module . '/' . Config::divisions($this->_division);
$this->divisionPrefix = ($this->_division === '' ? '' : $this->_division . '_');
$this->divisionPostfix = ($this->_division === '' ? '' : '.' . $this->_division);
}
/**
* Initialize template for called method.
*/
public function setUp()
{
$this->template = new PhpTemplate();
}
/**
* Sets template variables.
*
* @param array $variables
*
* @return \Colibri\Controller\ViewsController
*/
protected function view(array $variables)
{
$this->template->setVars($variables);
return $this;
}
/**
* @return $this
*/
protected function withoutLayout()
{
$this->useLayout = false;
return $this;
}
/**
* Compile template. Prepare ::$response.
*
* @throws \Exception
*/
public function tearDown()
{
if ( ! $this->useTemplate) {
return;
}
if ($this->template->getFilename() === null) {
$tplPath = sprintf(MODULE_VIEWS, $this->divisionPath);
$tplName = $tplPath . $this->_method . '.php';
if (file_exists($tplName)) {
$this->template->load($tplName);
}
}
if ( ! $this->useLayout) {
if ($this->template->getFilename() === null) {
throw new \Exception('template not loaded.');
}
$this->_response = $this->template->compile();
return;
}
$this->setUpLayout();
$this->_response = Layout::compile(
$this->template->getFilename() !== null
? $this->template->compile()
: (DEBUG ? 'DEBUG Info: template not autoloaded.<br/> no such file: ' . str_replace(ROOT, '', $tplName) : '')
);
}
/**
* Prepare layout with automatically added consts, js, js-mgr, css.
*/
private function setUpLayout()
{
if (Layout::filename()) {
return;
}
Layout::filename('layout' . $this->divisionPostfix . '.php');
Layout::addJsMgr('layout' . ($this->_division === '' ? '' : '_' . $this->_division));
Layout::addCss('layout' . $this->divisionPostfix . '.css');
$fileBaseName = $this->_module . '_' . $this->_method;
// add default js manager
$jsPath = $this->divisionPath . '/js/managers/';
$jsName = $this->divisionPrefix . $fileBaseName;
$fileName = MODULES . $jsPath . $jsName . '_mgr.js';
if (file_exists($fileName)) {
Layout::addJsMgr($jsName, MOD . $jsPath);
}
// add default css
$cssPath = $this->divisionPath . '/css/';
$cssName = $this->divisionPrefix . $fileBaseName . '.css';
$fileName = MODULES . $cssPath . $cssName;
if (file_exists($fileName)) {
Layout::addCss($cssName, MOD . $cssPath);
}
}
}