Passed
Branch tests1.5 (af713c)
by Wanderson
01:19
created

Application::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 12
rs 9.9666
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.3
16
 */
17
class Application {
18
19
	protected static $instance = null;
20
	private $name;
21
	private $page;
22
	private $homePage = 'index';
23
	private $params;
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::$instance = $this;
37
		Config::instance()->load($config);
38
		$this->name = (string) Config::instance()->get('name', '');
39
		$this->init();
40
	}
41
42
	/**
43
	 * Inicia com as configurações básicas
44
	 */
45
	protected function init() {
46
		$this->setParams(Url::instance()->getSegments());
47
		$this->controller = ControllerFactory::create($this->getParam(0), $this->getParam(1));
48
49
		Router::instance()->load();
50
		if (Router::instance()->run()):
51
			$this->setParams(Router::instance()->getCustomUrl());
52
			$this->controller = Router::instance()->createController();
53
		endif;
54
55
		$this->setPage($this->getParam(0));
56
		$this->view = ViewFactory::create($this->getParam(0), $this->getParam(1));
57
	}
58
59
	/**
60
	 * Retorna o ponteiro para a aplicação principal
61
	 * @return static
62
	 */
63
	public static function app() {
64
		return static::$instance;
65
	}
66
67
	/**
68
	 * Roda a aplicação
69
	 * Executando o Controller e criando o Layout que contem a View
70
	 */
71
	public function run() {
72
		$this->controller->load();
73
		Header::instance()->run();
74
		$layout = new Layout($this->controller->layout);
75
		$layout->load();
76
	}
77
78
	/** @return string */
79
	public function getName() {
80
		return $this->name;
81
	}
82
83
	/** @return string */
84
	public function getFullUrl() {
85
		return Url::instance()->getBaseUrl() . Url::instance()->getUrl();
86
	}
87
88
	/** @return string */
89
	public function getBaseUrl() {
90
		return Url::instance()->getBaseUrl();
91
	}
92
93
	/**
94
	 * Retorna a URL Atual
95
	 * @return string
96
	 */
97
	public function getUrl() {
98
		return Url::instance()->format(implode('/', $this->getParams()));
99
	}
100
101
	/**
102
	 * Retorna a página atual
103
	 * @return string
104
	 */
105
	public function getPage() {
106
		return (string) $this->page;
107
	}
108
109
	/** @param string $page */
110
	public function setPage($page) {
111
		$this->page = (string) $page;
112
	}
113
114
	/**
115
	 * Retorna TRUE se está na página inicial
116
	 * @return boolean
117
	 */
118
	public function isHomePage() {
119
		return (boolean) ($this->page === $this->homePage);
120
	}
121
122
	/**
123
	 * Retorna TRUE se está em alguma página de erro (404, 403, 503, etc)
124
	 * @return boolean
125
	 */
126
	public function isErrorPage() {
127
		return HttpException::isErrorCode($this->getPage());
128
	}
129
130
	/**
131
	 * Retorna um todos os parâmetros da URL
132
	 * @return string[]
133
	 */
134
	protected function getParams() {
135
		return $this->params;
136
	}
137
138
	/**
139
	 * Define os parâmetros
140
	 * Se estiver vazio, utiliza os parâmetros padrão.
141
	 * @param string[] $params
142
	 */
143
	private function setParams($params) {
144
		$paramDefaulf = [$this->homePage, 'index'];
145
		$this->params = array_replace($paramDefaulf, array_filter($params));
146
	}
147
148
	/**
149
	 * Retorna uma parte da URL
150
	 * @param int $position Parte escolhida
151
	 * @return string
152
	 */
153
	public function getParam($position) {
154
		return (key_exists($position, $this->params)) ? $this->params[$position] : '';
155
	}
156
157
	/**
158
	 * Define a página como 404
159
	 * @codeCoverageIgnore
160
	 */
161
	public function pageNotFound() {
162
		$this->errorPage(404);
163
	}
164
165
	/**
166
	 * Define a página atual como algum erro
167
	 * @param int $code
168
	 * @param string $message
169
	 * @throws HttpException
170
	 */
171
	public function errorPage($code, $message = '') {
172
		throw new HttpException($code, $message);
173
	}
174
175
}
176