Passed
Branch tests1.5 (1e45fa)
by Wanderson
01:17
created

Application::refresh()   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
use Win\Data\Config;
6
use Win\Request\Header;
7
use Win\Request\Url;
8
9
/**
10
 * Application (WinPHP Framework)
11
 *
12
 * Framework em PHP baseado em MVC
13
 * Esta classe é responsável por incluir as páginas de acordo com a URL e gerenciar a estrutura MVC
14
 * @author winPHP Framework <http://github.com/winframework/winphp/>
15
 * @version 1.2.6
16
 */
17
class Application {
18
19
	protected static $app = null;
20
	private $name;
21
	private $page;
22
	private $homePage = 'index';
23
	private $paramList;
24
25
	/** @var Controller */
26
	public $controller;
27
28
	/** @var View */
29
	public $view;
30
31
	/**
32
	 * Cria a aplicação principal
33
	 * @param mixed[] $config
34
	 */
35
	public function __construct($config = []) {
36
		static::$app = $this;
37
		Config::load($config);
38
		$this->name = (string) Config::get('name', '');
39
40
		$this->setParamList(Url::instance()->getFragments());
41
		$this->controller = ControllerFactory::create($this->getParam(0), $this->getParam(1));
42
43
		Router::instance()->load();
44
		if (Router::instance()->run()):
45
			$this->setParamList(Router::instance()->getCustomUrl());
46
			$this->controller = Router::instance()->createController();
47
		endif;
48
49
		$this->setPage($this->getParam(0));
50
		$this->view = ViewFactory::create($this->getParam(0), $this->paramList);
51
52
		ErrorPage::validate();
53
	}
54
55
	/**
56
	 * Retorna o ponteiro para a aplicação principal
57
	 * @return static
58
	 */
59
	public static function app() {
60
		return static::$app;
61
	}
62
63
	/**
64
	 * Roda a aplicação
65
	 * Executando o Controller e criando o Layout que contem a View
66
	 */
67
	public function run() {
68
		$this->controller->load();
69
		Header::run();
70
		$layout = new Layout($this->controller->layout);
71
		$layout->load();
72
	}
73
74
	/** @return string */
75
	public function getName() {
76
		return $this->name;
77
	}
78
79
	/** @return string */
80
	public function getFullUrl() {
81
		return Url::instance()->getBaseUrl() . Url::instance()->getUrl();
82
	}
83
84
	/** @return string */
85
	public function getBaseUrl() {
86
		return Url::instance()->getBaseUrl();
87
	}
88
89
	/**
90
	 * Retorna a URL Atual
91
	 * @return string
92
	 */
93
	public function getUrl() {
94
		return Url::instance()->format(implode('/', $this->getParamList()));
95
	}
96
97
	/**
98
	 * Retorna a página atual
99
	 * @return string
100
	 */
101
	public function getPage() {
102
		return $this->page;
103
	}
104
105
	/** @param string $page */
106
	public function setPage($page) {
107
		$this->page = $page;
108
	}
109
110
	/**
111
	 * Retorna TRUE se está na página inicial
112
	 * @return boolean
113
	 */
114
	public function isHomePage() {
115
		return (boolean) ($this->page === $this->homePage);
116
	}
117
118
	/**
119
	 * Retorna TRUE se está em alguma página de erro (404, 403, 503, etc)
120
	 * @return boolean
121
	 */
122
	public function isErrorPage() {
123
		return ErrorPage::isErrorPage();
124
	}
125
126
	/**
127
	 * Retorna um todos os parâmetros da URL
128
	 * @return string[]
129
	 */
130
	protected function getParamList() {
131
		return $this->paramList;
132
	}
133
134
	/**
135
	 * Define os parâmetros.
136
	 * Se estiver vazio, utiliza os parâmetros padrão.
137
	 * @param string[] $paramList
138
	 */
139
	private function setParamList($paramList) {
140
		$paramDefaulf = [$this->homePage, 'index'];
141
		$this->paramList = array_replace($paramDefaulf, array_filter($paramList));
142
	}
143
144
	/**
145
	 * Retorna uma parte da URL
146
	 * @param int $position Parte escolhida
147
	 * @return string
148
	 */
149
	public function getParam($position) {
150
		return (key_exists($position, $this->paramList)) ? $this->paramList[$position] : '';
151
	}
152
153
	/** Define a página como 404 */
154
	public function pageNotFound() {
155
		$this->errorPage(404);
156
	}
157
158
	/**
159
	 * Define a página atual como algum erro
160
	 * @param int $errorCode [401, 404, 500, etc]
161
	 */
162
	public function errorPage($errorCode) {
163
		ErrorPage::setError($errorCode);
164
	}
165
166
}
167