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
|
|
|
} |