1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Mvc; |
4
|
|
|
|
5
|
|
|
use Win\Helper\Template; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Views |
9
|
|
|
* |
10
|
|
|
* São responsáveis por criar o visual da página |
11
|
|
|
*/ |
12
|
|
|
class View { |
13
|
|
|
|
14
|
|
|
public static $dir = '/app/view/'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Ponteiro para Aplicação Principal |
18
|
|
|
* @var Application |
19
|
|
|
*/ |
20
|
|
|
public $app; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Endereço completo do arquivo .phtml que contem o código html |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $file = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Variáveis para serem usadas no arquivo da view |
30
|
|
|
* @var mixed[] |
31
|
|
|
*/ |
32
|
|
|
private $data; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Cria uma View com base no arquivo escolhido |
36
|
|
|
* @param string $file Nome do arquivo da view |
37
|
|
|
* @param mixed[] $data Array de variaveis |
38
|
|
|
*/ |
39
|
|
|
public function __construct($file, $data = []) { |
40
|
|
|
$this->app = Application::app(); |
41
|
|
|
$this->setFile($file); |
42
|
|
|
$this->data = $data; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Adiciona uma variavel para usar na view |
47
|
|
|
* @param string $name |
48
|
|
|
* @param mixed $value |
49
|
|
|
*/ |
50
|
|
|
public function addData($name, $value) { |
51
|
|
|
$this->data[$name] = $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Define o arquivo da view |
56
|
|
|
* @param string $file |
57
|
|
|
*/ |
58
|
|
|
private function setFile($file) { |
59
|
|
|
$filePath = BASE_PATH . static::$dir . $file; |
60
|
|
|
|
61
|
|
|
if (!is_null(Template::instance()->getTheme())): |
62
|
|
|
$filePath = Template::instance()->getFilePath(BASE_PATH . static::$dir, $file); |
63
|
|
|
endif; |
64
|
|
|
|
65
|
|
|
if (file_exists($filePath . '.phtml')): |
66
|
|
|
$this->file = $filePath . '.phtml'; |
67
|
|
|
endif; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retorna true se a view existe |
72
|
|
|
* @return boolean |
73
|
|
|
*/ |
74
|
|
|
public function exists() { |
75
|
|
|
return (file_exists($this->file)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Se arquivo nao existe, define como 404 |
80
|
|
|
*/ |
81
|
|
|
public function validate() { |
82
|
|
|
if (!$this->exists() && $this->app->getPage() !== '404'): |
83
|
|
|
$this->app->pageNotFound(); |
84
|
|
|
endif; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Retorna o html da view |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function __toString() { |
92
|
|
|
return $this->toString(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Retorna o html da view |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function toString() { |
100
|
|
|
ob_start(); |
101
|
|
|
$this->toHtml(); |
102
|
|
|
return ob_get_clean(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Carrega o arquivo da view, imprimindo o resultado html |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function load() { |
110
|
|
|
$this->toHtml(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Carrega o arquivo da view, imprimindo o resultado html |
115
|
|
|
*/ |
116
|
|
|
public function toHtml() { |
117
|
|
|
if (!is_null($this->file)): |
118
|
|
|
extract($this->data); |
119
|
|
|
include $this->file; |
120
|
|
|
endif; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|