Repository navigation
Expand file tree
/
Copy pathView.php
More file actions
154 lines (144 loc) · 4.44 KB
/
Copy pathView.php
File metadata and controls
154 lines (144 loc) · 4.44 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* @author : JIHAD SINNAOUR
* @package : FloatPHP
* @subpackage: Kernel Component
* @version : 1.0.0
* @category : PHP framework
* @copyright : (c) 2017 - 2021 JIHAD SINNAOUR <mail@jihadsinnaour.com>
* @link : https://www.floatphp.com
* @license : MIT License
*
* This file if a part of FloatPHP Framework
*/
namespace FloatPHP\Kernel;
use FloatPHP\Classes\Html\Hooks;
use FloatPHP\Classes\Auth\Session;
use FloatPHP\Classes\Security\Tokenizer;
use FloatPHP\Classes\Filesystem\Json;
use FloatPHP\Classes\Filesystem\Stringify;
class View extends BaseOptions
{
/**
* @access protected
* @var array $callables
*/
protected $callables = false;
/**
* Define custom callables
*
* @access protected
* @param array $callables
* @return void
*/
protected function setCallables($callables = [])
{
$this->callables = $callables;
}
/**
* Render view
*
* @access protected
* @param array $content
* @param string $template
* @return void
*/
protected function render($content = [], $template = 'system/default')
{
echo $this->assign($content, $template);
}
/**
* Aassign content to view
*
* @access protected
* @param array $content
* @param string $template
* @return string
* @todo translate function
* @todo hook functions
*/
protected function assign($content = [], $template = 'system/default')
{
// Set View environment
$env = Template::getEnvironment($this->getOverridePath($template),[
'cache' => $this->getCachePath(),
'debug' => $this->isDebug()
]);
// Set custom callables
if ($this->callables) {
foreach ($this->callables as $name => $callable) {
$env->addFunction(Template::extend($name, $callable));
}
}
// Add view global functions
$env->addFunction(Template::extend('dump', function ($var){
var_dump($var);
}));
$env->addFunction(Template::extend('isLoggedIn', function (){
$session = new Session();
return $session->isRegistered();
}));
$env->addFunction(Template::extend('isDebug', function (){
return $this->isDebug();
}));
$env->addFunction(Template::extend('getConfig', function ($config){
return $this->getConfig($config);
}));
$env->addFunction(Template::extend('getRoot', function (){
return $this->getRoot();
}));
$env->addFunction(Template::extend('getBaseUrl', function (){
return $this->getBaseUrl();
}));
$env->addFunction(Template::extend('getAssetUrl', function (){
return $this->getAssetUrl();
}));
$env->addFunction(Template::extend('getLoginUrl', function (){
return $this->getLoginUrl();
}));
$env->addFunction(Template::extend('getAdminUrl', function (){
return $this->getAdminUrl();
}));
$env->addFunction(Template::extend('getToken', function (){
return $this->getToken();
}));
$env->addFunction(Template::extend('JSONdecode', function ($json){
return Json::decode($json);
}));
$env->addFunction(Template::extend('JSONencode', function ($array){
return Json::encode($array);
}));
$env->addFunction(Template::extend('exit', function (){
exit;
}));
$env->addFunction(Template::extend('serialize', function ($data){
return Stringify::serialize($data);
}));
$env->addFunction(Template::extend('unserialize', function ($string){
return Stringify::unserialize($string);
}));
$env->addFunction(Template::extend('applyFilter', function ($tag, $args = []){
$hooks = Hooks::getInstance();
return $hooks->applyFilter($tag, $args);
}));
$env->addFunction(Template::extend('doAction', function ($tag, $args = []){
$hooks = Hooks::getInstance();
$hooks->doAction($tag, $args);
}));
// Return rendered view
$view = $env->load("{$template}{$this->getViewExtension()}");
return $view->render($content);
}
/**
* Get view path
*
* @access protected
* @param string $template
* @return string
* @todo Override
*/
protected function getOverridePath($template)
{
return $this->getViewPath();
}
}