|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Mvc; |
|
4
|
|
|
|
|
5
|
|
|
use Win\Helper\Url; |
|
6
|
|
|
use Win\Request\Input; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Application (WinPHP Framework) |
|
10
|
|
|
* |
|
11
|
|
|
* Framework em PHP baseado em MVC |
|
12
|
|
|
* Esta classe é responśavel por incluir as páginas de acordo com a URL e gerenciar a estrutura MVC |
|
13
|
|
|
* @author winPHP Framework <http://github.com/winframework/winphp/> |
|
14
|
|
|
* @version 0.1 |
|
15
|
|
|
*/ |
|
16
|
|
|
class Application { |
|
17
|
|
|
|
|
18
|
|
|
protected static $app = null; |
|
19
|
|
|
|
|
20
|
|
|
/** @var mixed[] */ |
|
21
|
|
|
private $config; |
|
22
|
|
|
private $name; |
|
23
|
|
|
private $page; |
|
24
|
|
|
private $homePage = 'index'; |
|
25
|
|
|
private $errorPageList = [ |
|
26
|
|
|
404 => 'Página não encontrada', |
|
27
|
|
|
401 => 'Não autorizado', |
|
28
|
|
|
403 => 'Acesso negado', |
|
29
|
|
|
500 => 'Erro no Servidor', |
|
30
|
|
|
503 => 'Problemas de Conexão' |
|
31
|
|
|
]; |
|
32
|
|
|
private $url; |
|
33
|
|
|
private $paramList; |
|
34
|
|
|
private $title; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Controller Controller atual */ |
|
37
|
|
|
public $controller; |
|
38
|
|
|
|
|
39
|
|
|
/** @var View View atual */ |
|
40
|
|
|
public $view; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Cria a aplicação principal |
|
44
|
|
|
* @param mixed[] $config Configurações |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($config = []) { |
|
47
|
|
|
static::$app = $this; |
|
48
|
|
|
$this->config = $config; |
|
49
|
|
|
$this->name = $this->getConfig('name', ''); |
|
50
|
|
|
|
|
51
|
|
|
$this->setParamList(Url::instance()->getFragments()); |
|
52
|
|
|
$this->controller = ControllerFactory::create($this->getParam(0), $this->getParam(1)); |
|
53
|
|
|
|
|
54
|
|
|
if (Route::instance()->run()): |
|
55
|
|
|
$this->setParamList(Route::instance()->getCustomUrl()); |
|
56
|
|
|
$this->controller = Route::instance()->createController(); |
|
57
|
|
|
endif; |
|
58
|
|
|
|
|
59
|
|
|
$this->setPage($this->getParam(0)); |
|
60
|
|
|
$this->view = ViewFactory::create($this->getParam(0), $this->paramList); |
|
61
|
|
|
|
|
62
|
|
|
$this->validatePage404(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Retorna um ponteiro para a aplicação principal |
|
67
|
|
|
* @return static |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function app() { |
|
70
|
|
|
return static::$app; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Executa a applicação |
|
75
|
|
|
* chamando o controller e criando o layout que contem a view |
|
76
|
|
|
*/ |
|
77
|
|
|
public function run() { |
|
78
|
|
|
$this->controller->load(); |
|
79
|
|
|
$layout = new Layout($this->controller->layout); |
|
80
|
|
|
$layout->load(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Retorna uma configuração |
|
85
|
|
|
* @param string $key Nome da configuração |
|
86
|
|
|
* @param string $default Valor default, caso esta configuração esteja em branco |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getConfig($key, $default = '') { |
|
89
|
|
|
return (key_exists($key, $this->config)) ? $this->config[$key] : $default; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** @return string */ |
|
93
|
|
|
public function getName() { |
|
94
|
|
|
return $this->name; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** @return string */ |
|
98
|
|
|
public function getFullUrl() { |
|
99
|
|
|
return $this->getBaseUrl() . $this->getUrl(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** @return string */ |
|
103
|
|
|
public function getBaseUrl() { |
|
104
|
|
|
return Url::instance()->getBaseUrl(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Retorna a URL Atual |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getUrl() { |
|
112
|
|
|
if (is_null($this->url)): |
|
113
|
|
|
$this->url = Url::instance()->format(implode('/', $this->getParamList())); |
|
114
|
|
|
endif; |
|
115
|
|
|
return $this->url; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** @return string */ |
|
119
|
|
|
public function getServerName() { |
|
120
|
|
|
return Input::server('SERVER_NAME', FILTER_SANITIZE_STRING); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** @return bolean */ |
|
124
|
|
|
public function isLocalHost() { |
|
125
|
|
|
$localAddress = ['localhost', '127.0.0.1', '::1', null]; |
|
126
|
|
|
return (in_array($this->getServerName(), $localAddress) || strpos($this->getServerName(), '192.168') !== false); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Retorna o nome da página atual |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getPage() { |
|
134
|
|
|
return $this->page; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** @param string $page */ |
|
138
|
|
|
protected function setPage($page) { |
|
139
|
|
|
$this->page = $page; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Retorna o nome da página inicial |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getHomePage() { |
|
147
|
|
|
return $this->homePage; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Retorna TRUE se está na página inicial |
|
152
|
|
|
* @return boolean |
|
153
|
|
|
*/ |
|
154
|
|
|
public function isHomePage() { |
|
155
|
|
|
return (boolean) ($this->page === $this->homePage); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Retorna TRUE se está em alguma página de erro (404, 403, 503, etc) |
|
160
|
|
|
* @return boolean |
|
161
|
|
|
*/ |
|
162
|
|
|
public function isErrorPage() { |
|
163
|
|
|
return (boolean) (key_exists((int) $this->page, $this->errorPageList)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Retorna um array com todos os parametros da URL |
|
168
|
|
|
* @return string[] |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function getParamList() { |
|
171
|
|
|
return $this->paramList; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Define os parâmetros. |
|
176
|
|
|
* Se estiver vazio, utiliza os parâmetros padrão. |
|
177
|
|
|
* @param string[] $paramList |
|
178
|
|
|
*/ |
|
179
|
|
|
private function setParamList($paramList) { |
|
180
|
|
|
$paramDefaulf = [$this->homePage, 'index']; |
|
181
|
|
|
$this->paramList = array_replace($paramDefaulf, array_filter($paramList)); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Retorna uma parte da URL |
|
186
|
|
|
* @param int $position Parte escolhida |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getParam($position) { |
|
190
|
|
|
return (key_exists($position, $this->paramList)) ? $this->paramList[$position] : ''; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Retorna o titulo da página atual |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getTitle() { |
|
198
|
|
|
if (is_null($this->title)): |
|
199
|
|
|
$this->title = ucwords(str_replace('-', ' ', $this->page)); |
|
200
|
|
|
endif; |
|
201
|
|
|
return $this->title; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Define o titulo da página |
|
206
|
|
|
* @param string $title |
|
207
|
|
|
*/ |
|
208
|
|
|
public function setTitle($title) { |
|
209
|
|
|
$this->title = $title; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Chama pageNotFound se o usuario acessar /404 |
|
214
|
|
|
* |
|
215
|
|
|
* Isso garante que todas as funcionalidades de pageNotFound serão executadas |
|
216
|
|
|
* mesmo se a página existente 404 for acessada |
|
217
|
|
|
*/ |
|
218
|
|
|
private function validatePage404() { |
|
219
|
|
|
if ($this->getParam(0) === '404'): |
|
220
|
|
|
$this->pageNotFound(); |
|
221
|
|
|
endif; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** Define a página como 404 */ |
|
225
|
|
|
public function pageNotFound() { |
|
226
|
|
|
$this->errorPage(404); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Define a página como "$errorCode" |
|
231
|
|
|
* @param int $errorCode [401, 404, 500, etc] |
|
232
|
|
|
*/ |
|
233
|
|
|
public function errorPage($errorCode) { |
|
234
|
|
|
if (key_exists($errorCode, $this->errorPageList)): |
|
235
|
|
|
$this->page = (string) $errorCode; |
|
236
|
|
|
$this->view = new View($errorCode); |
|
237
|
|
|
$this->title = $this->errorPageList[$errorCode]; |
|
238
|
|
|
$this->controller = ControllerFactory::create('Error' . $errorCode); |
|
239
|
|
|
http_response_code($errorCode); |
|
240
|
|
|
if ($this->getParam(0) !== $errorCode): |
|
|
|
|
|
|
241
|
|
|
$this->controller->load(); |
|
242
|
|
|
endif; |
|
243
|
|
|
endif; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|