Controler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
* @package      fwolflib
4
* @subpackage	mvc
5
* @copyright    Copyright 2008-2010,, 2018 Fwolf
6
* @author       Fwolf <[email protected]>
7
*/
8
9
10
require_once(dirname(__FILE__) . '/fwolflib.php');
11
require_once(FWOLFLIB . 'func/request.php');
12
require_once(FWOLFLIB . 'func/string.php');
13
require_once(FWOLFLIB . 'func/url.php');
14
15
16
/*
17
// In subclass or subclass for an app,
18
// the bottom layer class should define P2R first at here, and in this way:
19
if (!defined('P2R')) define('P2R', './');
20
// Then you can use P2R to require some app libs.
21
*/
22
23
/**
24
 * Controler class in MVC
25
 *
26
 * 控制系统的哪些功能被调用,主要是对应主系统和子系统根下的index.php文件,这些文件始终是用户调用的文件,对于每个子系统也是这样。同理,全局常量P2R在Contoler中定义。
27
 *
28
 * 主系统根下的index.php通过`\$_GET['m']`(module)来确定应该引用哪个子系统的index.php,相当于“进入某个子系统”。
29
 *
30
 * 子系统根下的index.php通过`\$_GET['a']`(action)来调用哪个功能相应的Page,类似的功能可以是几个action都在一个页面中处理。
31
 *
32
 * 主系统和子系统下的index.php要做到都可以被单独调用。
33
 *
34
 * 如果子系统的功能比较多,还可以再设计一层Controler,用于实现功能的选择;也可以采用统一放入子系统的Controler中集中控制的方式。特殊情况下,也可以从主系统根直接调用功能Page。
35
 *
36
 * 一般项目中,Controler只起到了用户命令的分流作用,处理较少,大量的页面生成、参数转换和传递都放到了View中。也正因为如此,如果启用了缓存机制,打算将缓存放在Controler中实现。
37
 *
38
 * 这里所说的module和action主要是针对系统逻辑的,不要和MVC中的概念混淆。
39
 *
40
 * @deprecated  Use Fwlib\Mvc\AbstractControler
41
 * @package		fwolflib
42
 * @subpackage	mvc
43
 * @copyright	Copyright 2008-2010, Fwolf
44
 * @author		Fwolf <[email protected]>
45
 * @since		2008-04-06
46
 * @see			Module
47
 * @see			View
48
 */
49
abstract class Controler extends Fwolflib {
0 ignored issues
show
Deprecated Code introduced by
The class Fwolflib has been deprecated with message: Use classes in Fwlib namespace, see PSR-0/1/2

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
50
	/**
51
	 * Run end time, used to caculate run time length
52
	 * @var	float
53
	 * @see $fTimeStart
54
	 */
55
	protected $fTimeEnd = 0;
56
57
	/**
58
	 * Run start time, used to caculate run time length
59
	 *
60
	 * Can only count to time when echo output.
61
	 * @var	float
62
	 * @see $fTimeEnd
63
	 */
64
	protected $fTimeStart = 0;
65
66
	/**
67
	 * View object
68
	 * @var	object
69
	 * @see ViewDisp()
70
	 */
71
	protected $oView = null;
72
73
	/**
74
	 * Action parameter, the view command to determin what to display
75
	 * @var string	// $_GET['a'], means which action user prefered of the module
76
	 */
77
	protected $sAction = '';
78
79
	/**
80
	 * Current module param
81
	 * @var	string	// $_GET['m'], means which module user prefered
82
	 */
83
	protected $sModule = '';
84
85
86
	abstract public function ViewErrorDisp($msg);	// Display view show error msg
87
	abstract public function Go();	// User call starter function
88
89
90
	/**
91
	 * contruct
92
	 */
93
	public function __construct() {
94
		parent::__construct();
95
96
		// Record run start time first
97
		$this->fTimeStart = microtime(true);
98
99
		// Get major parameters
100
		$this->sModule = GetGet('m');
0 ignored issues
show
Deprecated Code introduced by
The function GetGet() has been deprecated with message: Use Fwlib\Util\HttpUtil::getGet()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
101
		$this->sAction = GetGet('a');
0 ignored issues
show
Deprecated Code introduced by
The function GetGet() has been deprecated with message: Use Fwlib\Util\HttpUtil::getGet()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
102
103
	} // end of func __construct
104
105
106
	/**
107
	 * Get run time length and db query times etc.
108
	 *
109
	 * Need fwolflib::View,
110
	 * and use global var $i_db_query_times set in fwolflib::Adodb::CountDbQueryTimes
111
	 *
112
	 * Assign action is done in View::GenFooter.
113
	 *
114
	 * Cost about 0.05 more second when have db query.
115
	 *
116
	 * Eg: Processed in 0.054994 seconds, 6 db queries.
117
	 *
118
	 * @param	object	&$view	View object
119
	 * @global	int		$i_db_query_times
120
	 * @return	string
121
	 * @see	View::GenFooter
122
	 */
123
	public function GetDebugInfo(&$view) {
124
		global $i_db_query_times;
125
126
		// Record run end time
127
		$this->fTimeEnd = microtime(true);
128
		// Generate info str, time used
129
		$s = '';
130
		$time_used = $this->fTimeEnd - $this->fTimeStart;
131
		$time_used = round($time_used, 4);
132
		$s .= "<p>Processed in $time_used seconds";
133
		// Db query times
134
		if (isset($i_db_query_times))
135
			$s .= ", $i_db_query_times db queries";
136
137
		// Cache, Notice: this msg is delayed if cache on.
138
		if (true == $view->bCacheOn) {
139
			$key = $view->CacheKey();
140
			$s .= ', cache lifetime: ' . $view->CacheLifetime($key);
141
142
			// Refresh link, avoid robot claw
143
			$s .= ' <a href="javascript:';
144
			$s_t = SetUrlParam(GetSelfUrl(true), 'cache', 0);
0 ignored issues
show
Deprecated Code introduced by
The function GetSelfUrl() has been deprecated with message: Use Fwlib\Util\HttpUtil::getSelfUrl()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
145
			// Avoid robot claw, break it to several part
146
			$ar = preg_split('/(\:|\/|\.|\?|\&)/', $s_t, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
147
			$i = count($ar);	// Impossible =0
148
			for ($j = 0; $j < $i; $j++)
149
				// fcr = footer cache refresh
150
				$s .= "fcr$j='{$ar[$i - $j - 1]}';";
151
			$s .= 'fcr=';
152
			for ($j = 0; $j < $i; $j++)
153
				$s .= 'fcr' . ($i - $j - 1) . '+';
154
			$s .= '\'\';location.href=fcr;';
155
			$s .= '">R</a>';
156
		}
157
158
		$s .= ".</p>\n";
159
160
		// Add Cache log of get operate
161
		if (!empty(Cache::$aLogGet)) {
162
			$s = substr($s, 0, strlen($s) - 6);
163
			$s .= ', cache <span style="cursor: pointer;"
164
				onclick="javascript:
165
				var obj=getElementById(\'fwlib_debuginfo_cache_logget\');
166
				if (\'none\'==obj.style.display || \'\'==obj.style.display)
167
					{obj.style.display=\'block\';}
168
				else
169
					{obj.style.display=\'none\';};">
170
				logget</span>.</p>' . "\n";
171
			$s .= '<ul id="fwlib_debuginfo_cache_logget"
172
				style="display: none;">' . "\n";
173
			foreach (Cache::$aLogGet as $v) {
174
                $v['key'] = filter_var($v['key'], FILTER_SANITIZE_STRING );
175
                $v['success'] = filter_var($v['success'], FILTER_SANITIZE_STRING );
176
				$s .= '	<li style="text-align: left">'
177
					. (($v['success']) ? '√' : '×') . ': '
178
					. $v['key'] . '</li>' . "\n";
179
			}
180
			$s .= '</ul>' . "\n";
181
		}
182
183
		return $s;
184
	} // end of func GetDebugInfo
185
186
187
	/**
188
	 * Call a view class, display it's output
189
	 *
190
	 * Result echo out directly
191
	 * @param	string	$view	View define class file
192
	 * @param	string	$class	View class name, if obmit, will remove '_'&'-'
193
	 * 								in filename and use ucfirst($view) as class
194
	 * 								name.
195
	 * 							Auto remove beginning `v-` from $view is
196
	 * 							optional, if you use 'v-view.php' naming style,
197
	 * 							it will auto happen.
198
	 * @see	$oView
199
	 */
200
	protected function ViewDisp($view, $class = '')
201
	{
202
		// Check file existence
203
		if (file_exists($view))
204
			require_once($view);
205
		else
206
			$this->ViewErrorDisp("View define file $view not found!");
207
208
		// From ..../page_a.php, get 'page_a'.
209
		$s_view = substr($view, strrpos($view, '/') + 1);
210
		$s_view = substr($s_view, 0, strrpos($s_view, '.'));
211
212
		// Remove 'v-' from 'v-view.php', optional
213
		if ('v-' == substr($s_view, 0, 2))
214
			$s_view = substr($s_view, 2);
215
216
		// Then, 'page_a' to 'PageA'
217
		// Replace '-' in view name to '_'
218
		if (empty($class))
219
			$class = 'View' . StrUnderline2Ucfirst($s_view, true);
0 ignored issues
show
Deprecated Code introduced by
The function StrUnderline2Ucfirst() has been deprecated with message: Use Fwlib\Util\StringUtil::toStudlyCaps()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
220
221
		if (class_exists($class))
222
		{
223
			$this->oView = new $class($this);
224
			//$p->oCtl = $this;	// Set caller object	// Moved to __contruct of View class, transfer $this when do new().
225
226
			if ($this->oView->bCacheOn)
227
				echo $this->oView->CacheGetOutput();
228
			else
229
				echo $this->oView->GetOutput();
230
		}
231
		else
232
		{
233
			// Display error
234
			$this->ViewErrorDisp("View class $class not found!");
235
		}
236
	} // end of func ViewDisp
237
238
239
} // end of class Controler
240
241
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
242