|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Arch; |
|
4
|
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
|
6
|
|
|
use Ffcms\Core\Exception\NativeException; |
|
7
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
|
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
9
|
|
|
use Ffcms\Core\Traits\DynamicGlobal; |
|
10
|
|
|
use Ffcms\Core\Template\Variables; |
|
11
|
|
|
|
|
12
|
|
|
class Controller |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
use DynamicGlobal; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string $layout |
|
19
|
|
|
*/ |
|
20
|
|
|
public $layout = 'main'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string $response |
|
24
|
|
|
*/ |
|
25
|
|
|
public $response; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->before(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function before() {} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Compile output |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __destruct() |
|
39
|
|
|
{ |
|
40
|
|
|
// allow use and override after() method |
|
41
|
|
|
$this->after(); |
|
42
|
|
|
$this->make(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Build variables and display output html |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function make() |
|
49
|
|
|
{ |
|
50
|
|
|
// if layout is not required and this is just standalone app |
|
51
|
|
|
if ($this->layout === null) { |
|
52
|
|
|
$content = $this->response; |
|
53
|
|
|
} else { |
|
54
|
|
|
$layoutPath = App::$Alias->currentViewPath . '/layout/' . $this->layout . '.php'; |
|
55
|
|
|
if (!File::exist($layoutPath)) { |
|
56
|
|
|
throw new NativeException('Layout not founded: {root}' . Str::replace(root, '', $layoutPath)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$body = $this->response; |
|
|
|
|
|
|
60
|
|
|
// pass global data to config viewer |
|
61
|
|
|
if (App::$Debug !== null) { |
|
62
|
|
|
App::$Debug->bar->getCollector('config')->setData(['Global Vars' => Variables::instance()->getGlobalsArray()]); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
ob_start(); |
|
66
|
|
|
include_once($layoutPath); |
|
67
|
|
|
$content = ob_get_contents(); |
|
68
|
|
|
ob_end_clean(); |
|
69
|
|
|
|
|
70
|
|
|
// add debug bar |
|
71
|
|
|
if (App::$Debug !== null) { |
|
72
|
|
|
$content = str_replace( |
|
73
|
|
|
['</body>', '</head>'], |
|
74
|
|
|
[App::$Debug->renderOut() . '</body>', App::$Debug->renderHead() . '</head>'], |
|
75
|
|
|
$content); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// display content and layout if exist |
|
81
|
|
|
App::$Response->setContent($content); |
|
82
|
|
|
App::$Response->send(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function after() {} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set single global variable |
|
89
|
|
|
* @param string $var |
|
90
|
|
|
* @param string $value |
|
91
|
|
|
* @param bool $html |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setGlobalVar($var, $value, $html = false) |
|
94
|
|
|
{ |
|
95
|
|
|
Variables::instance()->setGlobal($var, $value, $html); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set global variables as array key=>value |
|
100
|
|
|
* @param $array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setGlobalVarArray(array $array) |
|
103
|
|
|
{ |
|
104
|
|
|
Variables::instance()->setGlobalArray($array); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Special method to set response of action execution |
|
109
|
|
|
* @param string $response |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setResponse($response) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->response = $response; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.