Passed
Push — master ( b97787...c0f601 )
by Jean-Christophe
16:10
created

SimpleViewAsyncController::_includeFileAsString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
ccs 8
cts 9
cp 0.8889
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0123
1
<?php
2
3
namespace Ubiquity\controllers;
4
5
/**
6
 * Default controller displaying php views only with an async server (Swoole, workerman).
7
 * Ubiquity\controllers$ControllerView
8
 * This class is part of Ubiquity
9
 *
10
 * @author jcheron <[email protected]>
11
 * @version 1.0.1
12
 *
13
 */
14
abstract class SimpleViewAsyncController extends SimpleViewController {
15
	protected static $views = [ ];
16
17 1
	protected function _includeFileAsString($filename, $pdata) {
18 1
		$key = \md5 ( \json_encode ( $pdata ) );
19 1
		if (! isset ( self::$views [$filename] [$key] )) {
20 1
			if (isset ( $pdata )) {
21 1
				\extract ( $pdata );
22
			}
23 1
			\ob_start ();
24 1
			include ($filename);
25 1
			return self::$views [$filename] [$key] = \ob_get_clean ();
26
		}
27
		return self::$views [$filename] [$key];
28
	}
29
30
	/**
31
	 * Loads the php view $viewName possibly passing the variables $pdata
32
	 *
33
	 * @param string $viewName The name of the view to load
34
	 * @param mixed $pData Variable or associative array to pass to the view
35
	 *        If a variable is passed, it will have the name **$data** in the view,
36
	 *        If an associative array is passed, the view retrieves variables from the table's key names
37
	 * @param boolean $asString If true, the view is not displayed but returned as a string (usable in a variable)
38
	 * @throws \Exception
39
	 * @return string null or the view content if **$asString** parameter is true
40
	 */
41 1
	public function loadView($viewName, $pData = NULL, $asString = false) {
42 1
		$filename = \ROOT . \DS . 'views' . \DS . $viewName;
43 1
		if ($asString) {
44
			return $this->_includeFileAsString ( $filename, $pData );
45
		}
46 1
		echo $this->_includeFileAsString ( $filename, $pData );
47
	}
48
}