-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
View.php
144 lines (135 loc) · 3.31 KB
/
View.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
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
<?php
/**
* This file is part of the alphaz Framework.
*
* @author Muhammad Umer Farooq (Malik) <mumerfarooqlablnet01@gmail.com>
*
* @link https://github.com/alphazframework/framework
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @since 1.0.0
*
* @license MIT
*/
namespace alphaz\View;
use alphaz\Common\Minify;
use alphaz\Contracts\View\View as ViewContract;
use alphaz\http\Response;
class View implements ViewContract
{
/**
* Is component.
*
* @since 1.0.0
*
* @var bool
*/
protected static $isCom = false;
/**
* File.
*
* @since 1.0.0
*
* @var string
*/
protected static $file = '';
/**
* Set file.
*
* @param $file file with path.
*
* @since 1.0.0
*
* @return void
*/
protected static function setFile($file)
{
if (self::$isCom === false) {
$incFile = route('views').'/'.$file.'.php';
if (file_exists($incFile)) {
self::$file = $incFile;
}
} else {
$incFile = route('com').$file.'.php';
if (file_exists($incFile)) {
self::$file = $incFile;
}
}
}
/**
* Render a view template.
*
* @param (string) $file Name of files.
* @param (array) $args Attributes.
*
* @since 1.0.0
*
* @return void
*/
public static function renderTemplate($file, $args = [])
{
if (!empty($file)) {
self::setFile($file);
extract($args, EXTR_SKIP);
if (file_exists(self::$file)) {
ob_start();
require_once self::$file;
} else {
throw new \Exception("Sorry, view file {$file} not exists", 404);
}
} else {
throw new \Exception('Sorry, file much be provided', 404);
}
}
/**
* Rander the view.
*
* @param (string) $file Name of files
* @param (array) $args Attributes.
* @param (bool) $minify Is code should be minify
* @param (array) $headers Custom headers.
*
* @since 1.0.0
*
* @return mixed
*/
public static function view($file, array $args = [], bool $minify = false, array $headers = [], $code = 200)
{
$headers['Content-Type'] = 'text/html';
if ($minify === true) {
$minify = new Minify();
self::renderTemplate($file, $args);
$config = [
'code' => $code,
'body' => $minify->htmlMinify(ob_get_clean(), 'code'),
'headers' => [
$headers,
],
];
$response = new Response($config);
$response->send();
} else {
self::renderTemplate($file, $args);
$config = [
'code' => $code,
'body' => ob_get_clean(),
'headers' => [
$headers,
],
];
$response = new Response($config);
$response->send();
}
}
/**
* Compile.
*
* @todo future
*
* @return void
*/
public function compile()
{
}
}