Passed
Branch master (989b89)
by Wanderson
01:21
created

Block::getFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Mvc;
4
5
/**
6
 * Blocos
7
 * Ver arquivos em: "app/block/"
8
 * São pequenos arquivos .phtml que são chamados em views, emails, modulos, etc
9
 */
10
class Block {
11
12
	public static $dir = '/app/block';
13
14
	/**
15
	 * Ponteiro para Aplicação Principal
16
	 * @var Application
17
	 */
18
	public $app;
19
20
	/**
21
	 * Endereço completo do arquivo .phtml que contem o código HTML
22
	 * @var string
23
	 */
24
	protected $file;
25
26
	/**
27
	 * Variáveis para serem usadas no arquivo da View
28
	 * @var mixed[]
29
	 */
30
	protected $data = [];
31
32
	/**
33
	 * Cria uma View com base no arquivo escolhido
34
	 * @param string $file Nome do arquivo da View
35
	 * @param mixed[] $data Array de variáveis
36
	 */
37
	public function __construct($file, $data = []) {
38
		$this->app = Application::app();
39
		$this->setFile($file);
40
		$this->data = $data;
41
	}
42
43
	/**
44
	 * Adiciona uma variável para usar na View
45
	 * @param string $name
46
	 * @param mixed $value
47
	 */
48
	public function addData($name, $value) {
49
		$this->data[$name] = $value;
50
	}
51
52
	/**
53
	 * Retorna uma variável da View
54
	 * @param string $name
55
	 * @return mixed|null
56
	 */
57
	public function getData($name) {
58
		if (key_exists($name, $this->data)) {
59
			return $this->data[$name];
60
		}
61
		return null;
62
	}
63
64
	/**
65
	 * Define o arquivo da View
66
	 * @param string $file
67
	 */
68
	protected function setFile($file) {
69
		$filePath = static::$dir . DIRECTORY_SEPARATOR . $file;
70
71
		if (!is_null(Template::instance()->getTheme())) {
72
			$filePath = Template::instance()->getFilePath($file);
73
		}
74
75
		$this->file = BASE_PATH . $filePath . '.phtml';
76
	}
77
78
	/** @return string */
79
	public function getFile() {
80
		return $this->file;
81
	}
82
83
	/**
84
	 * Retorna TRUE se a View existe
85
	 * @return boolean
86
	 */
87
	public function exists() {
88
		return (file_exists($this->file));
89
	}
90
91
	/**
92
	 * Retorna o HTML da View
93
	 * @return string
94
	 */
95
	public function __toString() {
96
		return $this->toString();
97
	}
98
99
	/**
100
	 * Retorna o HTML da View
101
	 * @return string
102
	 */
103
	public function toString() {
104
		ob_start();
105
		$this->toHtml();
106
		return ob_get_clean();
107
	}
108
109
	/**
110
	 * Exibe o conteúdo HTML da View
111
	 */
112
	public function toHtml() {
113
		$this->load();
114
	}
115
116
	/**
117
	 * Exibe o conteúdo HTML da View
118
	 * @return string
119
	 */
120
	public function load() {
121
		if (isset($this->file) && $this->exists()) {
122
			extract($this->data);
123
			include $this->file;
124
		}
125
	}
126
127
}
128