|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Bluz Framework Component |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Bluz PHP Team |
|
6
|
|
|
* @link https://github.com/bluzphp/framework |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Bluz\View; |
|
12
|
|
|
|
|
13
|
|
|
use Bluz\Auth\AbstractRowEntity; |
|
14
|
|
|
use Bluz\Common\Container; |
|
15
|
|
|
use Bluz\Common\Helper; |
|
16
|
|
|
use Bluz\Common\Options; |
|
17
|
|
|
use Bluz\Proxy\Logger; |
|
18
|
|
|
use Bluz\Response\ResponseTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* View - simple template engine with native PHP syntax |
|
22
|
|
|
* |
|
23
|
|
|
* @package Bluz\View |
|
24
|
|
|
* @author Anton Shevchuk |
|
25
|
|
|
* @author ErgallM |
|
26
|
|
|
* @link https://github.com/bluzphp/framework/wiki/View |
|
27
|
|
|
* |
|
28
|
|
|
* @method string ahref(string $text, mixed $href, array $attributes = []) |
|
29
|
|
|
* @method string api(string $module, string $method, $params = []) |
|
30
|
|
|
* @method string attributes(array $attributes = []) |
|
31
|
|
|
* @method string baseUrl(string $file = null) |
|
32
|
|
|
* @method string checkbox($name, $value = null, $checked = false, array $attributes = []) |
|
33
|
|
|
* @method string|bool controller(string $controller = null) |
|
34
|
|
|
* @method string|View dispatch($module, $controller, $params = []) |
|
35
|
|
|
* @method string exception(\Exception $exception) |
|
36
|
|
|
* @method string gravatar($email, $size = 80, $default = 'mm', $rate = 'g') |
|
37
|
|
|
* @method bool hasModule(string $module) |
|
38
|
|
|
* @method string|null headScript(string $src = null, array $attributes = []) |
|
39
|
|
|
* @method string|null headScriptBlock(string $code = null) |
|
40
|
|
|
* @method string|null headStyle(string $href = null, string $media = 'all') |
|
41
|
|
|
* @method string|bool module(string $module = null) |
|
42
|
|
|
* @method string partial($__template, $__params = []) |
|
43
|
|
|
* @method string partialLoop($template, $data = [], $params = []) |
|
44
|
|
|
* @method string radio($name, $value = null, $checked = false, array $attributes = []) |
|
45
|
|
|
* @method string redactor($selector, array $settings = []) |
|
46
|
|
|
* @method string script(string $src, array $attributes = []) |
|
47
|
|
|
* @method string scriptBlock(string $code) |
|
48
|
|
|
* @method string select($name, array $options = [], $selected = null, array $attributes = []) |
|
49
|
|
|
* @method string style(string $href, $media = 'all') |
|
50
|
|
|
* @method string styleBlock(string $code, $media = 'all') |
|
51
|
|
|
* @method string|null url(string $module, string $controller, array $params = [], bool $checkAccess = false) |
|
52
|
|
|
* @method AbstractRowEntity|null user() |
|
53
|
|
|
* @method void widget($module, $widget, $params = []) |
|
54
|
|
|
*/ |
|
55
|
|
|
class View implements ViewInterface, \JsonSerializable |
|
56
|
|
|
{ |
|
57
|
|
|
use Container\Container; |
|
58
|
|
|
use Container\JsonSerialize; |
|
59
|
|
|
use Container\MagicAccess; |
|
60
|
|
|
use Options; |
|
61
|
|
|
use Helper; |
|
62
|
|
|
use ResponseTrait; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string base url |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $baseUrl; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string path to template |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $path; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var array paths to partial |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $partialPath = []; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var string template name |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $template; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Create view instance, initial default helper path |
|
86
|
|
|
* |
|
87
|
|
|
* @throws \Bluz\Common\Exception\CommonException |
|
88
|
|
|
*/ |
|
89
|
50 |
|
public function __construct() |
|
90
|
|
|
{ |
|
91
|
|
|
// initial default helper path |
|
92
|
50 |
|
$this->addHelperPath(__DIR__ . '/Helper/'); |
|
93
|
50 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Render like string |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
3 |
|
public function __toString() |
|
101
|
|
|
{ |
|
102
|
3 |
|
ob_start(); |
|
103
|
|
|
try { |
|
104
|
3 |
|
if (!file_exists($this->path . DIRECTORY_SEPARATOR . $this->template) |
|
105
|
3 |
|
|| !is_file($this->path . DIRECTORY_SEPARATOR . $this->template) |
|
106
|
|
|
) { |
|
107
|
1 |
|
throw new ViewException("Template `{$this->template}` not found"); |
|
108
|
|
|
} |
|
109
|
3 |
|
extract($this->container, EXTR_SKIP); |
|
110
|
3 |
|
include $this->path . DIRECTORY_SEPARATOR . $this->template; |
|
111
|
1 |
|
} catch (\Exception $e) { |
|
112
|
|
|
// save error to log |
|
113
|
1 |
|
Logger::exception($e); |
|
114
|
|
|
// clean output |
|
115
|
1 |
|
ob_clean(); |
|
116
|
|
|
} |
|
117
|
3 |
|
return ob_get_clean(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* {@inheritdoc} |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getPath() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->path; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $path |
|
134
|
|
|
* |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
11 |
|
public function setPath($path) |
|
138
|
|
|
{ |
|
139
|
11 |
|
$this->path = $path; |
|
140
|
11 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function getTemplate() |
|
148
|
|
|
{ |
|
149
|
1 |
|
return $this->template; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* {@inheritdoc} |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $file |
|
156
|
|
|
* |
|
157
|
|
|
* @return void |
|
158
|
|
|
*/ |
|
159
|
9 |
|
public function setTemplate($file) |
|
160
|
|
|
{ |
|
161
|
9 |
|
$this->template = $file; |
|
162
|
9 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Add partial path for use inside partial and partialLoop helpers |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $path |
|
168
|
|
|
* |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
32 |
|
public function addPartialPath($path) |
|
172
|
|
|
{ |
|
173
|
32 |
|
$this->partialPath[] = $path; |
|
174
|
32 |
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|