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.0 |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
abstract class SimpleViewAsyncController extends SimpleViewController { |
15
|
|
|
protected static $views = [ ]; |
16
|
|
|
|
17
|
|
|
protected function _includeFileAsString($filename, $pData) { |
18
|
|
|
if (isset ( $pData )) { |
19
|
|
|
\extract ( $pData ); |
20
|
|
|
} |
21
|
|
|
if (! isset ( self::$views [$filename] )) { |
22
|
|
|
\ob_start (); |
23
|
|
|
include ($filename); |
24
|
|
|
return self::$views [$filename] = \ob_get_clean (); |
25
|
|
|
} |
26
|
|
|
return eval ( '?>' . self::$views [$filename] ); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Loads the php view $viewName possibly passing the variables $pdata |
31
|
|
|
* |
32
|
|
|
* @param string $viewName The name of the view to load |
33
|
|
|
* @param mixed $pData Variable or associative array to pass to the view |
34
|
|
|
* If a variable is passed, it will have the name **$data** in the view, |
35
|
|
|
* If an associative array is passed, the view retrieves variables from the table's key names |
36
|
|
|
* @param boolean $asString If true, the view is not displayed but returned as a string (usable in a variable) |
37
|
|
|
* @throws \Exception |
38
|
|
|
* @return string null or the view content if **$asString** parameter is true |
39
|
|
|
*/ |
40
|
|
|
public function loadView($viewName, $pData = NULL, $asString = false) { |
41
|
|
|
$filename = \ROOT . \DS . 'views' . \DS . $viewName; |
42
|
|
|
if ($asString) { |
43
|
|
|
return $this->_includeFileAsString ( $filename, $pData ); |
44
|
|
|
} |
45
|
|
|
echo $this->_includeFileAsString ( $filename, $pData ); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|