Passed
Branch v1.5.1 (4f5540)
by Wanderson
05:07
created

Template::setFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Win\Common;
4
5
use Win\Application;
6
7
/**
8
 * Templates em .PHTML
9
 * Ver arquivos em: "app/templates/"
10
 */
11
class Template
12
{
13
	public static $dir = 'app/templates';
14
	/**
15
	 * Ponteiro para Aplicação Principal
16
	 * @var Application
17
	 */
18
	public $app;
19
20
	/**
21
	 * Endereço completo do arquivo .phtml
22
	 * @var string
23
	 */
24
	protected $file;
25
26
	/**
27
	 * Variáveis para serem usadas no corpo do arquivo
28
	 * @var mixed[]
29
	 */
30
	protected $data = [];
31
32
	/**
33
	 * Layout
34
	 * @var string
35
	 */
36
	private $layout = null;
37
38
	/**
39
	 * Cria um template com base no arquivo escolhido
40
	 * @param string $file Nome do arquivo
41
	 * @param mixed[] $data Array de variáveis
42
	 * @param string $layout Layout
43
	 */
44
	public function __construct($file, $data = [], $layout = null)
45
	{
46
		$this->app = Application::app();
47
		$this->file = BASE_PATH . '/' . static::$dir . "/$file.phtml";
48
		$this->data = $data;
49
		$this->layout = $layout;
50
	}
51
52
	/**
53
	 * Retorna uma variável
54
	 * @param string $name
55
	 * @return mixed|null
56
	 */
57
	public function getData($name)
58
	{
59
		if (key_exists($name, $this->data)) {
60
			return $this->data[$name];
61
		}
62
63
		return null;
64
	}
65
66
	/**
67
	 * Retorna TRUE se o template existe
68
	 * @return bool
69
	 */
70
	public function exists()
71
	{
72
		return file_exists($this->file);
73
	}
74
75
	/**
76
	 * Carrega e retorna o output
77
	 * @return string
78
	 */
79
	public function toHtml()
80
	{
81
		if ($this->layout) {
82
			return (new Layout($this->layout, $this))->toHtml();
83
		}
84
85
		ob_start();
86
		$this->load();
87
		return ob_get_clean();
88
	}
89
90
	/**
91
	 * Carrega e exibe o conteúdo do template
92
	 */
93
	public function load()
94
	{
95
		if (isset($this->file) && $this->exists()) {
96
			extract($this->data);
97
			include $this->file;
98
		}
99
	}
100
}
101