1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Mvc; |
4
|
|
|
|
5
|
|
|
use Win\Helper\Template; |
6
|
|
|
use Win\Html\Seo\Title; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Views |
10
|
|
|
* |
11
|
|
|
* São responsáveis por criar o visual da página |
12
|
|
|
*/ |
13
|
|
|
class View { |
14
|
|
|
|
15
|
|
|
public static $dir = '/app/view/'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Ponteiro para Aplicação Principal |
19
|
|
|
* @var Application |
20
|
|
|
*/ |
21
|
|
|
public $app; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Endereço completo do arquivo .phtml que contem o código HTML |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $file; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Variáveis para serem usadas no arquivo da View |
31
|
|
|
* @var mixed[] |
32
|
|
|
*/ |
33
|
|
|
private $data; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Cria uma View com base no arquivo escolhido |
37
|
|
|
* @param string $file Nome do arquivo da View |
38
|
|
|
* @param mixed[] $data Array de variáveis |
39
|
|
|
*/ |
40
|
|
|
public function __construct($file, $data = []) { |
41
|
|
|
$this->app = Application::app(); |
42
|
|
|
$this->setFile($file); |
43
|
|
|
$this->data = $data; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Adiciona uma variável para usar na View |
48
|
|
|
* @param string $name |
49
|
|
|
* @param mixed $value |
50
|
|
|
*/ |
51
|
|
|
public function addData($name, $value) { |
52
|
|
|
$this->data[$name] = $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Adiciona um array de variáveis para usar na View |
57
|
|
|
* @param mixed[] $data |
58
|
|
|
*/ |
59
|
|
|
public function mergeData(array $data) { |
60
|
|
|
$this->data = array_merge($this->data, $data); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Retorna uma variável da View |
65
|
|
|
* @param string $name |
66
|
|
|
* @return mixed|null |
67
|
|
|
*/ |
68
|
|
|
public function getData($name) { |
69
|
|
|
if (key_exists($name, $this->data)) { |
70
|
|
|
return $this->data[$name]; |
71
|
|
|
} |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getTitle() { |
76
|
|
|
if (empty($this->getData('title'))) { |
77
|
|
|
$this->addData('title', Title::otimize(ucwords(str_replace('-', ' ', $this->app->getPage())))); |
78
|
|
|
} |
79
|
|
|
return $this->getData('title'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Define o arquivo da View |
84
|
|
|
* @param string $file |
85
|
|
|
*/ |
86
|
|
|
private function setFile($file) { |
87
|
|
|
$filePath = BASE_PATH . static::$dir . $file; |
88
|
|
|
|
89
|
|
|
if (!is_null(Template::instance()->getTheme())): |
90
|
|
|
$filePath = Template::instance()->getFilePath(BASE_PATH . static::$dir, $file); |
91
|
|
|
endif; |
92
|
|
|
|
93
|
|
|
$this->file = $filePath . '.phtml'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @return string */ |
97
|
|
|
public function getFile() { |
98
|
|
|
return $this->file; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Retorna TRUE se a View existe |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function exists() { |
106
|
|
|
return (file_exists($this->file)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Se arquivo não existe, define como 404 |
111
|
|
|
*/ |
112
|
|
|
public function validate() { |
113
|
|
|
if (!$this->exists() && $this->app->getPage() !== '404'): |
114
|
|
|
$this->app->pageNotFound(); |
115
|
|
|
endif; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Retorna o HTML da View |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function __toString() { |
123
|
|
|
return $this->toString(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Retorna o HTML da View |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function toString() { |
131
|
|
|
ob_start(); |
132
|
|
|
$this->toHtml(); |
133
|
|
|
return ob_get_clean(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Exibe o conteúdo HTML da View |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function load() { |
141
|
|
|
$this->toHtml(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Exibe o conteúdo HTML da View |
146
|
|
|
*/ |
147
|
|
|
public function toHtml() { |
148
|
|
|
if (isset($this->file) && $this->exists()): |
149
|
|
|
extract($this->data); |
150
|
|
|
include $this->file; |
151
|
|
|
endif; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|