-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.php
97 lines (79 loc) · 2.25 KB
/
Controller.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
<?php
namespace Panada\Resource;
use Panada;
/**
* Handler for controller process.
*
* @author kandar <iskandarsoesman@gmail.com>
* @link http://panadaframework.com/
* @license http://www.opensource.org/licenses/bsd-license.php
* @since version 1.0.0
* @package Resource
*/
trait Controller
{
private $childNamespace;
private $viewCache;
private $viewFile;
private $configMain;
public $config = [];
public function __construct()
{
$this->getChildInfo();
}
public function __get($class)
{
$classNamespace = [
'response' => 'Panada\Http\Response',
'uri' => 'Panada\Http\Uri'
];
try {
return call_user_func($classNamespace[$class].'::getInstance');
}
catch(\Exception $e) {
try {
$this->$class;
}
catch(\Exception $e) {
throw new \ErrorException($e->getMessage(), 0, 1, $e->getTrace()[1]['file'], $e->getTrace()[1]['line']);
}
}
}
private function getChildInfo()
{
$child = get_class($this);
$this->childClass = [
'namespaceArray' => explode( '\\', $child),
'namespaceString' => $child
];
}
public function output($panadaViewfile, $data = [], $fromMainView = false)
{
$this->getChildInfo();
$app = Gear::$appDir;
$panadaFilePath = $app.'view/'.$panadaViewfile;
if( $this->childClass['namespaceArray'][0] == 'Module' && !$fromMainView ) {
$panadaFilePath = $app.$this->childClass['namespaceArray'][0].'/'.$this->childClass['namespaceArray'][1].'/view/'.$panadaViewfile;
}
$this->viewFile = $panadaFilePath;
if( ! empty($data) ){
$this->viewCache = [
'data' => $data,
'prefix' => $this->childClass['namespaceString'],
];
}
// We don't need this variables anymore.
unset($panadaViewFile, $data, $panadaFilePath);
if(! empty($this->viewCache) && $this->viewCache['prefix'] == $this->childClass['namespaceString'] ) {
extract( $this->viewCache['data'], EXTR_SKIP );
}
try{
ob_start();
include $this->viewFile.'.php';
return ob_get_clean();
}
catch(\Exception $e) {
throw $e;
}
}
}