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
|
|
|
* Build variables and display output html |
37
|
|
|
*/ |
38
|
|
|
public function getOutput() |
39
|
|
|
{ |
40
|
|
|
$this->after(); |
41
|
|
|
|
42
|
|
|
// if layout is not required and this is just standalone app |
43
|
|
|
if ($this->layout === null) { |
44
|
|
|
$content = $this->response; |
45
|
|
|
} else { |
46
|
|
|
$layoutPath = App::$Alias->currentViewPath . '/layout/' . $this->layout . '.php'; |
47
|
|
|
if (!File::exist($layoutPath)) { |
48
|
|
|
throw new NativeException('Layout not founded: ' . $layoutPath); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$body = $this->response; |
52
|
|
|
// pass global data to config viewer |
53
|
|
|
if (App::$Debug !== null) { |
54
|
|
|
App::$Debug->bar->getCollector('config')->setData(['Global Vars' => Variables::instance()->getGlobalsArray()]); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// cleanup buffer from random shits after exception throw'd |
58
|
|
|
ob_clean(); |
59
|
|
|
// start buffering to render layout |
60
|
|
|
ob_start(); |
61
|
|
|
include($layoutPath); |
62
|
|
|
$content = ob_get_clean(); // read buffer content & stop buffering |
63
|
|
|
|
64
|
|
|
// set custom css library's not included on static call |
65
|
|
|
$cssIncludeCode = App::$View->showCodeLink('css'); |
66
|
|
|
if (!Str::likeEmpty($cssIncludeCode)) { |
|
|
|
|
67
|
|
|
$content = Str::replace('</head>', $cssIncludeCode . '</head>', $content); |
68
|
|
|
} |
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
|
|
|
return $content; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function after() {} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set single global variable |
87
|
|
|
* @param string $var |
88
|
|
|
* @param string $value |
89
|
|
|
* @param bool $html |
90
|
|
|
*/ |
91
|
|
|
public function setGlobalVar($var, $value, $html = false) |
92
|
|
|
{ |
93
|
|
|
Variables::instance()->setGlobal($var, $value, $html); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set global variables as array key=>value |
98
|
|
|
* @param $array |
99
|
|
|
*/ |
100
|
|
|
public function setGlobalVarArray(array $array) |
101
|
|
|
{ |
102
|
|
|
Variables::instance()->setGlobalArray($array); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Special method to set response of action execution |
107
|
|
|
* @param string $response |
108
|
|
|
*/ |
109
|
|
|
public function setResponse($response) |
110
|
|
|
{ |
111
|
|
|
$this->response = $response; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: